转载请标注原文地址:https://blog.csdn.net/lilyssh/article/details/82753906
Spring Boot 2.0.4 集成 dubbo-spring-boot-starter 2.0.0。项目源码地址
一. 初始化工程
在high-concurrency/user文件夹下,创建工程:
(1)user-provider //服务提供者
(2)user-api //接口
(3)user-consumer //服务消费者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 1~/workspace/gitee/high-concurrency/user
2$ tree -I target
3.
4├── pom.xml
5├── user-api
6│ ├── pom.xml
7│ └── src
8│ └── main
9│ └── java
10│ └── cn
11│ └── lilyssh
12│ └── user
13│ └── api
14│ ├── model
15│ │ ├── request
16│ │ │ ├── UserInsertReq.java
17│ │ │ └── UserQueryReq.java
18│ │ └── response
19│ │ └── UserQueryResp.java
20│ └── service
21│ └── UserServiceApi.java
22├── user-consumer
23│ ├── pom.xml
24│ └── src
25│ └── main
26│ ├── java
27│ │ └── cn
28│ │ └── lilyssh
29│ │ └── user
30│ │ └── consumer
31│ │ ├── UserConsumerApplication.java
32│ │ ├── controller
33│ │ │ └── UserController.java
34│ │ └── service
35│ │ └── UserService.java
36│ └── resources
37│ └── application.yml
38└── user-provider
39 ├── pom.xml
40 └── src
41 └── main
42 ├── java
43 │ └── cn
44 │ └── lilyssh
45 │ └── user
46 │ └── provider
47 │ ├── UserProviderApplication.java
48 │ ├── dao
49 │ │ ├── entity
50 │ │ │ └── UserEntity.java
51 │ │ ├── mapper
52 │ │ │ └── UserMapper.java
53 │ │ └── repository
54 │ │ └── UserRepository.java
55 │ └── service
56 │ └── UserService.java
57 └── resources
58 └── application.yml
59
60
二、在user-api工程中添加接口
(1) 添加实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1package cn.lilyssh.user.api.model.request;
2
3import lombok.Data;
4import java.io.Serializable;
5import java.util.Date;
6
7@Data
8public class UserQueryResp implements Serializable {
9 private int id;
10 private String uuid;
11 private String userName;
12 private String password;
13 private String realName;
14 private int sex;
15 private int age;
16 private int phone;
17 private String email;
18 private int status;
19 private String lastLoginIp;
20 private Date lastLoginTime;
21 private int idType;
22 private String idNumber;
23 private String address;
24}
25
26
(2) 添加接口
1
2
3
4
5
6
7
8
9
10 1package cn.lilyssh.user.api.service;
2
3import cn.lilyssh.user.api.model.response.UserQueryResp;
4import java.util.List;
5
6public interface UserServiceApi {
7 List<UserQueryResp> list();
8}
9
10
三、发布dubbo服务(user-provider)
1. 添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 1<dependency>
2 <groupId>com.alibaba.spring.boot</groupId>
3 <artifactId>dubbo-spring-boot-starter</artifactId>
4 <version>RELEASE</version>
5 <scope>compile</scope>
6</dependency>
7<dependency>
8 <groupId>com.101tec</groupId>
9 <artifactId>zkclient</artifactId>
10 <version>0.10</version>
11</dependency>
12<dependency>
13 <groupId>com.alibaba</groupId>
14 <artifactId>fastjson</artifactId>
15 <version>1.2.49</version>
16</dependency>
17
18
2. dubbo配置
(1)在src/main/resources下新建文件application.yml,配置dubbo :
1
2
3
4
5
6
7 1spring:
2 application:
3 name: user-provider
4 dubbo:
5 registry: zookeeper://ssh.qianxunclub.com:2181
6
7
(2) 在spirng boot启动类中,添加@EnableDubboConfiguration注解启用dubbo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1package cn.lilyssh.user.provider;
2
3import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
4import lombok.extern.slf4j.Slf4j;
5import org.springframework.boot.SpringApplication;
6import org.springframework.boot.autoconfigure.SpringBootApplication;
7
8@SpringBootApplication
9@EnableDubboConfiguration
10public class UserProviderApplication {
11 public static void main(String[] args) {
12 SpringApplication.run(UserProviderApplication.class, args);
13 }
14}
15
16
(3) 创建接口实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 1package cn.lilyssh.user.provider.service;
2
3import cn.lilyssh.user.api.model.response.UserQueryResp;
4import cn.lilyssh.user.api.service.UserServiceApi;
5import cn.lilyssh.user.provider.dao.entity.UserEntity;
6import cn.lilyssh.user.provider.dao.repository.UserRepository;
7import com.alibaba.dubbo.config.annotation.Service;
8import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
9import lombok.AllArgsConstructor;
10import lombok.extern.slf4j.Slf4j;
11import org.springframework.beans.BeanUtils;
12import org.springframework.stereotype.Component;
13import java.util.ArrayList;
14import java.util.List;
15
16@Slf4j
17@Service
18@Component
19@AllArgsConstructor
20public class UserService implements UserServiceApi {
21
22 private UserRepository userRepository;
23
24 @Override
25 public List<UserQueryResp> list(){
26 QueryWrapper<UserEntity> qw = new QueryWrapper<>();
27 List<UserEntity> userEntityList = userRepository.list(qw);
28 List<UserQueryResp> resultList=new ArrayList<>();
29 userEntityList.forEach(userEntity->{
30 UserQueryResp userQueryResp=new UserQueryResp();
31 BeanUtils.copyProperties(userEntity,userQueryResp);
32 resultList.add(userQueryResp);
33 });
34 return resultList;
35 }
36}
37
38
3. 启动项目,模拟测试调用dubbo服务
(1) 使用telnet模拟调用dubbo服务,端口为user-provider.xml里配置的dubbo端口,默认20880。
1
2
3 1telnet localhost 20880
2
3
如果连接成功,会看到
1
2
3
4
5
6
7 1Trying ::1...
2Connected to localhost.
3Escape character is '^]'.
4
5dubbo>
6
7
(2) 使用ls查看所有服务。
1
2
3
4 1dubbo>ls
2cn.lilyssh.user.api.service.UserServiceApi
3
4
(3) 使用cd进入到com.qianxunclub.demo.dubbo.DemoService中,并使用ls查看服务里的方法。
1
2
3
4
5
6
7
8
9
10 1dubbo>cd cn.lilyssh.user.api.service.UserServiceApi
2Used the cn.lilyssh.user.api.service.UserServiceApi as default.
3You can cancel default service by command: cd /
4dubbo>ls
5Use default service cn.lilyssh.user.api.service.UserServiceApi.
6
7list
8dubbo>
9
10
(4) 使用invoke模拟客户端调用服务。
1
2
3
4
5
6
7 1dubbo>invoke list()
2Use default service cn.lilyssh.user.api.service.UserServiceApi.
3[{"age":0,"id":1,"idType":0,"phone":0,"sex":0,"status":0,"userName":"Tom","uuid":"3"},{"age":0,"id":2,"idType":0,"phone":0,"sex":0,"status":0,"userName":"Mike","uuid":"4"}]
4elapsed: 334 ms.
5dubbo>
6
7
四、消费dubbo服务(user-consumer)
1. 添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13 1<dependency>
2 <groupId>com.alibaba.spring.boot</groupId>
3 <artifactId>dubbo-spring-boot-starter</artifactId>
4 <version>RELEASE</version>
5 <scope>compile</scope>
6</dependency>
7<dependency>
8 <groupId>com.101tec</groupId>
9 <artifactId>zkclient</artifactId>
10 <version>0.2</version>
11</dependency>
12
13
2. dubbo配置
(1)在src/main/resources下新建文件application.yml,配置dubbo:
1
2
3
4
5
6
7
8
9
10
11 1spring:
2 application:
3 name: user-consumer
4 dubbo:
5 registry:
6 address: zookeeper://ssh.qianxunclub.com:2181
7
8server:
9 port: 9000
10
11
(2) 在spirng boot启动类中,添加@EnableDubboConfiguration注解启用dubbo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1package cn.lilyssh.user.consumer;
2
3import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
4import org.springframework.boot.SpringApplication;
5import org.springframework.boot.autoconfigure.SpringBootApplication;
6
7@SpringBootApplication
8@EnableDubboConfiguration
9public class UserConsumerApplication {
10
11 public static void main(String[] args) {
12 SpringApplication.run(UserConsumerApplication.class, args);
13 }
14}
15
16
(3)创建Controller,给前端提供Rest接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 1package cn.lilyssh.user.consumer.controller;
2
3import cn.lilyssh.user.consumer.service.UserService;
4import lombok.AllArgsConstructor;
5import org.springframework.web.bind.annotation.GetMapping;
6import org.springframework.web.bind.annotation.RequestMapping;
7import org.springframework.web.bind.annotation.RestController;
8
9@RestController
10@AllArgsConstructor
11@RequestMapping("/user")
12public class UserController {
13
14 private UserService userService;
15
16 /**
17 * 获取所有用户
18 */
19 @GetMapping
20 public Object userList(){
21 return userService.userList();
22 }
23}
24
25
(4)创建Service,调用api接口。需用@Reference注解注入api接口服务。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1package cn.lilyssh.user.consumer.service;
2
3import cn.lilyssh.user.api.model.response.UserQueryResp;
4import cn.lilyssh.user.api.service.UserServiceApi;
5import com.alibaba.dubbo.config.annotation.Reference;
6import org.springframework.stereotype.Component;
7import java.util.List;
8
9@Component
10public class UserService{
11
12 @Reference
13 private UserServiceApi userServiceApi;
14
15 public List<UserQueryResp> userList(){
16 return userServiceApi.list();
17 }
18}
19
20
四、用postman测试,调用Rest接口
1. 启动user-provider和user-consumer工程
2. 在postman中输入测试地址
http://localhost:9000/user 会看到结果:
1
2
3 1[{"id":1,"uuid":"3","userName":"Tom","password":null,"realName":null,"sex":0,"age":0,"phone":0,"email":null,"status":0,"lastLoginIp":null,"lastLoginTime":null,"idType":0,"idNumber":null,"address":null},{"id":2,"uuid":"4","userName":"Mike","password":null,"realName":null,"sex":0,"age":0,"phone":0,"email":null,"status":0,"lastLoginIp":null,"lastLoginTime":null,"idType":0,"idNumber":null,"address":null}]
2
3
大功告成!