spring-boot-starter-dubbo的maven项目托管在https://github.com/xionghuiCoder/spring-boot-starter-dubbo;同时也可以在https://www.oschina.net/p/spring-boot-starter-dubbo上了解它的简单介绍。
spring-boot-starter-dubbo是dubbo的spring boot starter,它可以无缝地对接spring boot和dubbo,方便大家使用dubbo组件。
需要注意的是spring-boot-starter-dubbo支持的jdk版本为1.6或者1.6+。
如何发布dubbo服务
- 添加spring-boot-starter-dubbo依赖:
-
<dependency> <groupId>com.alibaba</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>
如果注册中心使用zookeeper的话需要添加zkclient依赖:
-
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>
- 在application.properties内添加dubbo的相关配置信息,demo配置如下:
-
# dubbo配置 spring.dubbo.appname=provider-test spring.dubbo.protocol=dubbo # 此处也可使用其它协议,比如zookeeper://xxxx:xx spring.dubbo.registry=multicast://224.0.0.0:1111 spring.dubbo.port=20800 spring.dubbo.group=test spring.dubbo.version=1.0.0
- 接下来在Spring Boot Application上添加@EnableDubboConfiguration,表示要开启dubbo功能。
-
/** * Provider启动类 <br /> * * 如果没有web容器,需要hold住服务,否则进程会退出,参考以下代码: * * <pre> * synchronized (DubboProviderLauncher.class) { * while (true) { * try { * DubboProviderLauncher.class.wait(); * } catch (InterruptedException e) { * // swallow it * } * } * } * </pre> * * @author xionghui * @email xionghui.xh@alibaba-inc.com * @since 1.0.0 */ @SpringBootApplication @EnableDubboConfiguration public class DubboProviderLauncher { public static void main(String[] args) { SpringApplication.run(DubboProviderLauncher.class, args); } }
- 编写hello服务,只需要在发布的服务实现上添加@Service注解 ,其中interfaceClass是要发布服务的接口。
-
import com.alibaba.dubbo.config.annotation.Service; @Component @Service(interfaceClass = IHelloService.class) public class HelloServiceImpl implements IHelloService { @Override public String hello() { return "hi, you!"; } }
- 启动Spring Boot应用。
- 如果provider使用了web容器,可以使用http://localhost:port/dubbo/offline来下线所有服务;该接口会返回一个数组,数组里面是下线服务的详细信息:
-
[ { side: "provider", methods: "hello", dubbo: "2.5.3", threads: "200", pid: "19919", interface: "org.test.IHelloService", version: "1.0.0", revision: "1.0.0", path: "org.test.IHelloService", protocol: "dubbo", application: "provider-test", port: "20800", host: "192.168.0.10", anyhost: "true", group: "test", timestamp: "1484403167472" } ]
还可以使用http://localhost:port/dubbo/online来上线所有服务;该接口会返回一个数组,数组里面是上线服务的详细信息:
-
[ { side: "provider", methods: "hello", dubbo: "2.5.3", threads: "200", pid: "19919", interface: "org.test.IHelloService", version: "1.0.0", revision: "1.0.0", path: "org.test.IHelloService", protocol: "dubbo", application: "provider-test", port: "20800", host: "192.168.0.10", anyhost: "true", group: "test", timestamp: "1484403167472" } ]
如何消费Dubbo服务
- 添加依赖:
-
<dependency> <groupId>com.alibaba</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>
- 同样,如果注册中心使用zookeeper的话需要添加zkclient依赖:
-
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency>
- 在application.properties添加dubbo的相关配置信息,demo配置如下:
-
# dubbo配置 spring.dubbo.appname=consumer-test spring.dubbo.protocol=dubbo # 此处也可使用其它协议,比如zookeeper://xxxx:xx spring.dubbo.registry=multicast://224.0.0.0:1111 spring.dubbo.port=20801 spring.dubbo.group=test spring.dubbo.version=1.0.0
- 接下来在Spring Boot Application上添加@EnableDubboConfiguration,表示要开启dubbo功能。
-
/** * Consumer启动类 * * @author xionghui * @email xionghui.xh@alibaba-inc.com * @since 1.0.0 */ @SpringBootApplication @EnableDubboConfiguration public class DubboConsumerLauncher { public static void main(String[] args) { SpringApplication.run(DubboConsumerLauncher.class, args); } }
- 通过@DubboConsumer注入需要使用的interface:
-
/** * Consumer Controller * * @author xionghui * @email xionghui.xh@alibaba-inc.com * @since 1.0.0 */ @RestController @RequestMapping("/") public class ConsumerController { // timeout表示dubbo超时时间,单位为ms @DubboConsumer(timeout = 1000) private IHelloService iHelloService; @RequestMapping(value = "hello", method = RequestMethod.GET) @ResponseBody public String hello() { return this.iHelloService.hello(); } }
- 启动Spring Boot应用。
- 可以通过http://localhost:port/health监控服务信息:
-
{ status: "UP", dubbo: { status: "UP", ClassIdBean [clazz=interface org.test.IHelloService, group=test, version=1.0.0]: true }, diskSpace: { status: "UP", total: 487955914752, free: 455584600064, threshold: 10485760 } }
- 最后通过http://localhost:port/hello调用RPC服务,返回结果:
-
hi, you!
参考文档
- dubbo 介绍: http://dubbo.io/
- spring-boot 介绍: http://projects.spring.io/spring-boot/
- spring-boot-dubbo 参考: https://github.com/linux-china/spring-boot-dubbo