SpringBoot 2.0 集成 Dubbo
SpringBoot 2.0 集成 Dubbo
服务提供者示例
实现步骤
引入相关依赖
<dependencies>
<!-- 引入springboot相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 引入springboot dubbo依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- 引入RPC公共接口 -->
<dependency>
<groupId>cn.tyrone.springboot</groupId>
<artifactId>springboot-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
注意:springboot-dubbo-api
属于dubbo要暴露的服务接口工程
创建application.yml文件
server:
port: 9090
spring:
application:
name: spingboot-dubbo-provider
dubbo:
application:
id: spring-dubbo-provider
name: spring-dubbo-provider
registry: # 配置注册中心
protocol: zookeeper # 指定注册中心协议
address: localhost:2181 # 指定注册中心地址
protocol: # 配置协议
name: dubbo
port: 20880
# monitor:
# protocol: registry # 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址,否则直连监控中心。
创建springboot-dubbo-provider启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
@SpringBootApplication
@EnableDubbo // @EnableDubbo <==> @EnableDubboConfig + @DubboComponentScan
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
创建服务提供者实现
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import cn.tyrone.springboot.dubbo.api.DemoService;
/*
* 通过com.alibaba.dubbo.config.annotation.Service注解来声明要暴露的接口
*
* @Service(interfaceClass = DemoService.class)相当于配置文件中的<dubbo:service interface="cn.tyrone.springboot.dubbo.api.DemoService" />
*
*/
@Service(interfaceClass = DemoService.class)
@Component
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return name + ":SpringBoot Dubbo Integrate.......";
}
}
服务消费者示例
实现步骤
引入相关依赖
<dependencies>
<!-- 引入springboot相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 引入springboot dubbo依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- 引入RPC公共接口 -->
<dependency>
<groupId>cn.tyrone.springboot</groupId>
<artifactId>springboot-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
注意:springboot-dubbo-api
属于dubbo要暴露的服务接口工程
创建application.yml文件
server:
port: 7070
spring:
application:
name: spring-boot-consumer
dubbo:
application:
id: spring-boot-consumer
name: spring-boot-consumer
registry:
protocol: zookeeper
address: localhost:2181
创建springboot-dubbo-consumer启动类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import cn.tyrone.springboot.dubbo.api.DemoService;
@EnableDubbo // @EnableDubbo <==> @EnableDubboConfig + @DubboComponentScan
@SpringBootApplication
public class Application implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
/*
* 通过 @Reference 注解,生成远程代理服务,用于获取服务端请求
*/
@Reference(interfaceClass = DemoService.class)
private DemoService demoService;
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
String name = "SpringBoot-Dubbo-Consummer-Example";
String result = demoService.sayHello(name);
log.info("SpringBoot整合Dubbo消费端示例结果:" + result);
}
}
RPC 接口示例
dubbo建议将服务接口,服务模型,服务异常等均放在 API 包中,因为服务模型及异常也是 API 的一部分,同时,这样做也符合分包原则:重用发布等价原则(REP),共同重用原则(CRP)。RPC 接口即将服务提供者和服务消费者共同使用的接口抽象到公共模块中。
实现步骤
按需创建服务接口
public interface DemoService {
String sayHello(String name);
}
注意事项:本示例是用zookeeper做注册中心的,所以在搭建dubbo环境时,确保已经安装zookeeper。
测试
分别启动服务提供者示例和服务消费者示例,观察控制台日志输出,如果程序正常,会在服务消费者控制台看到如下日志:
2018-09-12 21:44:56.640 INFO 34995 --- [ main] c.t.s.dubbo.consumer.Application : SpringBoot整合Dubbo消费端示例结果:SpringBoot-Dubbo-Consummer-Example:SpringBoot Dubbo Integrate.......
源代码链接
服务提供者:https://github.com/myNameIssls/springboot-study/tree/master/springboot-dubbo-provider
服务消费者:https://github.com/myNameIssls/springboot-study/tree/master/springboot-dubbo-consumer
RPC接口工程:https://github.com/myNameIssls/springboot-study/tree/master/springboot-dubbo-api
参考链接:
http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
https://github.com/apache/incubator-dubbo-spring-boot-project/blob/master/README_CN.md
https://github.com/apache/incubator-dubbo
推荐阅读
-
SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用
-
SpringBoot集成JWT实现权限认证
-
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
-
SpringBoot 2.0 开发案例之百倍级减肥瘦身之旅
-
SpringBoot集成WebSocket长连接实际应用详解
-
Vue2.0中集成UEditor富文本编辑器的方法
-
Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
-
小白的springboot之路(十二)、集成log4j2日志
-
springcloud Springboot vue.js Activiti6 前后分离 跨域 工作流 集成代码生成器 shiro权限
-
SpringBoot 2.0整合阿里云OSS,实现动静分离架构