SpringBoot集成Dubbo
(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功能(和服务提供者相同。此处略)