springboot dubbo 整合简单的示例(在xml里暴露接口,多种方式启动)
按这个示例,可以用springboot启动dubbo,也可以在本地启动dubbo,或者服务提供者用springboot启动,消费者在本地启动,或者提供者在本地启动,消费者用springboot启动
1 工具
eclipse mar jkd1.8 maven3.5.3 springboot2.1.0 dubbo2.0.0(dubbo用阿里官方版本)
2 创建项目
- 父项目--主要是添加公用的依赖pom.xml
Maven项目,只有pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath />
</parent>
<groupId>com.springboot</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>../example</module>
<module>../dubboconsumer</module>
<module>../dubbotest</module>
</modules>
<dependencies>
<!--dubbo-springBoot依赖 -->
<dependency><!--dubbo的官方版本 用这个 -->
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- <dependency>dubbo其他版本
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency> -->
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</project>
|
- 创建公用的接口项目,供服务提供者和服务消费者调用
Pom文件不用配置,只有一个接口,服务提供者和服务消费者依赖该项目,实现该接口
package com.springboot.dubboAPI;
public interface IBookService {
String readBooks();
}
|
- 服务提供者
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springboot</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.srpingboot</groupId>
<artifactId>example</artifactId>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion><!--排除springboot自动加载的tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope><!--设置在打包时不包含tomcat的包 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
|
不用application.properties文件
添加dubbo-provider.xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用配置,不要与提供方相同 -->
<dubbo:application name="springboot-dubbo-provider"/>
<!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.181.128:2181" timeout="60000" />
<!-- 用dubbo协议在20882端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20882" />
<!-- 当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值 -->
<dubbo:provider timeout="30000" threadpool="fixed" threads="500" accepts="1000" />
<!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。-->
<dubbo:consumer check="false" />
<!-- 使用注解方式暴露接口,会自动扫描package下所有包中dubbo相关的注解,这样就不用在xml中再针对每个服务接口配置dubbo:service interface-->
<!-- <dubbo:annotation package="com.srpingboot.example"/> -->
<!-- 声明需要暴露的服务接口 -->
<!-- <dubbo:service interface="com.springboot.dubboAPI.IBookService" ref="bookServiceImpl" /> -->
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.springboot.dubboAPI.IBookService" ref="iBookService" />
<!-- 和本地bean一样实现服务 -->
<bean id="iBookService" class="com.srpingboot.example.BookServiceImpl" />
</beans>
|
实现类
package com.srpingboot.example;
import com.alibaba.dubbo.config.annotation.Service;
import com.springboot.dubboAPI.IBookService;
//@Service
@org.springframework.stereotype.Service
public class BookServiceImpl implements IBookService {
@Override
public String readBooks() {
return "I like read book";
}
@Override
public void buyBooks() {
}
}
|
注意该类有两个service注解,一个是alibaba的,一个是spring的,大家要注意区分。
启动类1--通过springboot启动dubbo
package com.srpingboot.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
//@EnableDubboConfiguration
@SpringBootApplication
@ImportResource(value = {"classpath:dubbo-provider.xml"})
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
|
启动类2--通过ClassPathXmlApplicationContext在本地启动dubbo服务
package com.srpingboot.example;
import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;
public class LuncherProvider {
public static void main(String[] args) throws InterruptedException, IOException {
String configLocation = "dubbo-provider.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
context.start();
System.in.read();
}
}
|
这里需要注意,加入System.in.read()是起阻塞作用的,否则由于pom中没有加spring-boot-starter-web依赖,启动时没有tomcat容器,会自动退出。另一种解决方式就是加入spring-boot-starter-web依赖。
(4) 服务消费者
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springboot</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubboconsumer</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion><!--排除springboot自动加载的tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope><!--设置在打包时不包含tomcat的包 -->
</dependency>
</dependencies>
<build>
<finalName>dubboconsumer</finalName>
</build>
</project>
|
application.properties文件
## 避免和提供者项目端口冲突
server.port=8082
|
dubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用配置,不要与提供方相同 -->
<dubbo:application name="springboot-dubbo-consumer"/>
<!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.181.128:2181" timeout="60000" />
<!-- 监控中心配置,protocol="registry",表示从注册中心发现服务地址 -->
<dubbo:monitor protocol="registry"/>
<!-- 使用注解方式创建远程服务代理-->
<!-- <dubbo:annotation package="com.srpingboot.example"/> -->
<!-- 声明需要暴露的服务接口 -->
<dubbo:reference id="iBookService" interface="com.springboot.dubboAPI.IBookService" />
</beans>
|
controller
package com.srpingboot.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.springboot.dubboAPI.IBookService;
@RestController
public class DubboController {
// @Reference
// private IBookService iBookService;
@Autowired
private IBookService iBookService;
@GetMapping("test")
public String testDubbo() {
return iBookService.readBooks();
}
}
|
启动类1 (跟服务提供者一样)
package com.srpingboot.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
//@EnableDubboConfiguration
@ImportResource("classpath:dubbo-consumer.xml")
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
|
启动类2--下边这个启动后只执行一次调用
package com.srpingboot.example;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springboot.dubboAPI.IBookService;
public class LocalConsumer {
public static void main(String[] args) {
LocalConsumer localConsumer=new LocalConsumer();
localConsumer.start();
} private void start() {
String configLocation = "dubbo-consumer.xml";
// ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
context.start();
IBookService ds = context.getBean("iBookService",IBookService.class);
// String[] names = context.getBeanDefinitionNames();
// System.out.print("Beans:");
// for (String string : names) {
// System.out.println(string);
// }
String readBooks = ds.readBooks();
System.out.println(readBooks+11111);
}}
|
启动测试
先启动zookeeper,再启动provider,然后启动consumer
访问consumer : http://localhost:8082/test