Apache Dubbo 是一款高性能Java RPC框架
由阿里巴巴开源并进入Apache孵化器,官网 http://dubbo.apache.org
提供服务化基础功能: 接口远程调用,智能负载均衡,服务发现与注册和运维治理界面等功能
集成SpringBoot中文说明地址 https://github.com/apache/incubator-dubbo-spring-boot-project/blob/master/README_CN.md
我们重新看下项目中的结构,做出如下修改
...somefun
......somefun-web (springboot项目 dubbo客户端)
......somefun-service-system
.........somefun-system-api (对外提供模块,提供接口和DTO)
.........somefun-system-service (springboot项目 dubbo服务提供端)
1我们将web模块开发成dubbo服务消费端部分
2将web模块中的pom去掉 somefun-system-service 依赖(somefun-system-api 依赖不去掉,通过此模块实现远程调用 而不侵入代码)
3将somefun-system-service模块修改为springboot项目独立运行
服务提供方的代码
somefun-system-service模块
pom文件
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <parent>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId>
8 <version>2.1.1.RELEASE</version>
9 <relativePath/>
10 </parent>
11
12 <modelVersion>4.0.0</modelVersion>
13
14 <artifactId>somefun-system-service</artifactId>
15 <dependencies>
16 <dependency>
17 <groupId>com.zhj</groupId>
18 <artifactId>somefun-system-api</artifactId>
19 <version>1.0-SNAPSHOT</version>
20 </dependency>
21
22 <dependency>
23 <groupId>com.alibaba.boot</groupId>
24 <artifactId>dubbo-spring-boot-starter</artifactId>
25 <version>0.2.0</version> <!-- 最新的0.2.1-SNAPSHOT 可以去上边的GitHub地址中下载编译 -->
26 </dependency>
27
28 <!-- Dubbo -->
29 <dependency>
30 <groupId>com.alibaba</groupId>
31 <artifactId>dubbo</artifactId>
32 <version>2.6.5</version>
33 </dependency>
34 <!-- Spring Context Extras -->
35 <dependency>
36 <groupId>com.alibaba.spring</groupId>
37 <artifactId>spring-context-support</artifactId>
38 <version>1.0.2</version>
39 </dependency>
40
41 </dependencies>
42
43
44 </project>
创建启动类SomefunSystemApplication
1 @SpringBootApplication (scanBasePackages={"com.zhj.somefun"})
2 public class SomefunSystemApplication {
3
4 public static void main(String[] args) {
5
6 new SpringApplicationBuilder(SomefunSystemApplication.class)
7 .web(WebApplicationType.NONE) //非web方式运行
8 .run(args);
9 }
10
11 }
TestServiceImpl类:
注意下这时候的Service注解的包要引用 duboo中的service注解
1 package com.zhj.somefun.system.service.impl;
2
3 import com.zhj.somefun.system.api.dto.TestDto;
4 import com.zhj.somefun.system.api.service.TestService;
5
6 import com.alibaba.dubbo.config.annotation.Service;
7
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.List;
11
12 @Service
13 public class TestServiceImpl implements TestService {
14
15 @Override
16 public List<TestDto> getTestList() {
17 List<TestDto> list = new ArrayList<>();
18 TestDto dto = new TestDto();
19 dto.setAge(18);
20 dto.setId(1);
21 dto.setName("姓名1");
22 dto.setCreatedate(new Date());
23 list.add(dto);
24 return list;
25 }
26 }
TestDto类:
要实现Serializable接口
1 public class TestDto implements Serializable{
2 private Integer id;
3 private String name;
4
5 public Integer getId() {
6 return id;
7 }
8 …… ……
9 }
application.yml文件
1 server:
2 port: 6666
3 spring:
4 application:
5 name: dubbo-provider-demo
6
7 dubbo:
8 application:
9 id: dubbo-provider-demo
10 name: dubbo-provider-demo
11 monitor:
12 protocol: registry
13 protocol:
14 id: dubbo
15 name: dubbo
16 port: 12345
17 registry:
18 address: zookeeper://127.0.0.1:2181
19 id: my-registry
20 scan:
21 #扫描server包地址
22 basePackages: com.zhj.somefun.system.service
服务消费方
我们将web模块改造成dubbo调用方
pom 引入dubbo
1 <dependency>
2 <groupId>com.alibaba.boot</groupId>
3 <artifactId>dubbo-spring-boot-starter</artifactId>
4 <version>0.2.0</version>
5 </dependency>
6
7 <!-- Dubbo -->
8 <dependency>
9 <groupId>com.alibaba</groupId>
10 <artifactId>dubbo</artifactId>
11 <version>2.6.5</version>
12 </dependency>
13 <!-- Spring Context Extras -->
14 <dependency>
15 <groupId>com.alibaba.spring</groupId>
16 <artifactId>spring-context-support</artifactId>
17 <version>1.0.2</version>
18 </dependency>
Testcontroller控制器类
注解修改为Reference注解类
1 @RestController
2 public class Testcontroller {
3
4 @Reference
5 private TestService testService;
6
7 @GetMapping("/getlist")
8 public List<TestDto> getlist(){
9 return testService.getTestList();
10 }
11
12 }
application.yml 文件
1 dubbo:
2 application:
3 id: dubbo-consumer-demo
4 name: dubbo-consumer-demo
5 monitor:
6 protocol: registry
7 protocol:
8 id: dubbo
9 name: dubbo
10 port: 12345
11 registry:
12 address: zookeeper://127.0.0.1:2181
13 server:
14 port: 8080
15 spring:
16 profiles:
17 active: dev
18 application:
19 name: dubbo-consumer-demo
因为我们注册中心使用的是zookeeper,我们可以在网上下载最新的zookeeper文件,运行起来即可。
运行服务器提供方的项目somefun-system-service
运行消费方的项目somefun-web
访问 http://localhost:8080/getlist ,
我们会发现已经可以返回 JSON数据
[{"id":1,"name":"姓名1","age":18,"createdate":"2018-12-22T06:58:43.535+0000"}]
这说明dubbo已经可以正常运行了
我们在运行项目中的时候编译器可能会打印错误日志
com.alibaba.dubbo.rpc.RpcException: No provider available from registry 127.0.0.1:2181 for service com.alibaba.dubbo.monitor.MonitorService on consumer 192.168.1.73 use dubbo version 2.6.5, please check status of providers(disabled, not registered or in blacklist).
这个因为 dubbo的监控中心没有接入 ,对于这篇代码来说暂时没有影响
这里只是介绍了最简单的springboot 接入dubbo,更新丰富的功能和详解的介绍在dubbo官网