1. 程式人生 > >SpringBoot集成Dubbo

SpringBoot集成Dubbo

serializa 服務提供者 boot.s ble ger iba rs.remove cli sch

(1)、新建一個普通Maven項目,用於存放一些公共服務接口及公共的Bean等。

項目:

技術分享圖片

公共Bean:

 1 package cn.coreqi.entities;
 2 
 3 import java.io.Serializable;
 4 
 5 public class User implements Serializable {
 6     private Integer id;
 7     private String userName;
 8     private String passWord;
 9     private Integer enabled;
10 
11
public User() { 12 } 13 14 public User(Integer id, String userName, String passWord, Integer enabled) { 15 this.id = id; 16 this.userName = userName; 17 this.passWord = passWord; 18 this.enabled = enabled; 19 } 20 21 public Integer getId() { 22 return
id; 23 } 24 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 29 public String getUserName() { 30 return userName; 31 } 32 33 public void setUserName(String userName) { 34 this.userName = userName; 35 } 36 37 public String getPassWord() {
38 return passWord; 39 } 40 41 public void setPassWord(String passWord) { 42 this.passWord = passWord; 43 } 44 45 public Integer getEnabled() { 46 return enabled; 47 } 48 49 public void setEnabled(Integer enabled) { 50 this.enabled = enabled; 51 } 52 53 @Override 54 public String toString() { 55 return "User{" + 56 "id=" + id + 57 ", userName=‘" + userName + ‘\‘‘ + 58 ", passWord=‘" + passWord + ‘\‘‘ + 59 ", enabled=" + enabled + 60 ‘}‘; 61 } 62 }

公共服務接口:

 1 package cn.coreqi.service;
 2 
 3 import cn.coreqi.entities.User;
 4 
 5 import java.util.List;
 6 
 7 public interface UserService {
 8     public void addUser(User user);
 9     public void delById(Integer id);
10     public void modifyUser(User user);
11     public User getById(Integer id);
12     public List<User> getList();
13 }

(2)、新建SpringBoot項目用作與服務提供者

  1)、添加依賴

    

 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         <!--引入公共的Maven項目-->
 8         <dependency>
 9             <groupId>cn.coreqi</groupId>
10             <artifactId>springbootdubboapi</artifactId>
11             <version>1.0-SNAPSHOT</version>
12         </dependency>

*  dubbo-spring-boot-starter的版本與SpringBoot的版本之間有相應的對照關系

技術分享圖片

*  0.2.x的默認使用curator作為操作zookeeper的客戶端。0.1.x需要自行導入zookeeper的客戶端

1 <dependency>
2     <groupId>com.github.sgroschupf</groupId>
3     <artifactId>zkclient</artifactId>
4     <version>0.1</version>
5 </dependency>

  2)、配置相關屬性

1 dubbo.application.name=boot-service-provider
2 dubbo.registry.address=192.168.205.128:2181
3 dubbo.registry.protocol=zookeeper
4 dubbo.protocol.name=dubbo
5 dubbo.protocol.port=20880
6 dubbo.monitor.protocol=registry
7 
8 server.port=8080

  3)、在要暴露服務的服務實現類上添加Dubbo的@Service註解,並將Spring的@Service註解替換為@Component註解

 1 package cn.coreqi.service.impl;
 2 
 3 import cn.coreqi.entities.User;
 4 import cn.coreqi.service.UserService;
 5 import com.alibaba.dubbo.config.annotation.Service;
 6 import org.springframework.stereotype.Component;
 7 
 8 import java.util.ArrayList;
 9 import java.util.List;
10 
11 @Component  //org.springframework.stereotype.Component
12 @Service    //com.alibaba.dubbo.config.annotation.Service
13 public class UserServiceImpl implements UserService {
14     private static List<User> users = new ArrayList<>();
15     static {
16         users.add(new User(1,"fanqi","123456",1));
17         users.add(new User(2,"zhangsan","123456",1));
18         users.add(new User(3,"lisi","123456",1));
19         users.add(new User(4,"wangwu","123456",1));
20     }
21     @Override
22     public void addUser(User user) {
23         users.add(user);
24     }
25 
26     @Override
27     public void delById(Integer id) {
28         for (User s:users){
29             if(s.getId() == id){
30                 users.remove(s);
31                 break;
32             }
33         }
34     }
35 
36     @Override
37     public void modifyUser(User user) {
38         delById(user.getId());
39         addUser(user);
40     }
41 
42     @Override
43     public User getById(Integer id) {
44         for (User s:users){
45             if(s.getId() == id){
46                 return s;
47             }
48         }
49         return null;
50     }
51 
52     @Override
53     public List<User> getList() {
54         return users;
55     }
56 }

  4)、在主程序啟動類上添加@EnableDubbo註解開啟基於註解的Dubbo功能

 1 package cn.coreqi;
 2 
 3 import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 @SpringBootApplication
 8 @EnableDubbo
 9 public class SpringbootdubboserviceproviderApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(SpringbootdubboserviceproviderApplication.class, args);
13     }
14 
15 }

(3)、新建SpringBoot項目用作與服務消費者

  1)、添加依賴(和服務提供者相同。此處略)

  2)、配置相關屬性

  

1 dubbo.application.name=boot-service-consumer
2 dubbo.registry.address=zookeeper://192.168.205.128:2181
3 dubbo.monitor.protocol=registry
4 
5 server.port=9090

  3)、在遠程引用服務的屬性上添加@Reference註解

 1 package cn.coreqi.controller;
 2 
 3 import cn.coreqi.entities.User;
 4 import cn.coreqi.service.UserService;
 5 import com.alibaba.dubbo.config.annotation.Reference;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 import java.util.List;
11 
12 @Controller
13 public class UserController {
14     @Reference
15     public UserService userService;
16 
17     @ResponseBody
18     @RequestMapping("/users")
19     public List<User> getUsers(){
20         return userService.getList();
21     }
22 }

  4)、在主程序啟動類上添加@EnableDubbo註解開啟基於註解的Dubbo功能(和服務提供者相同。此處略)

SpringBoot集成Dubbo