IDEA Dubbo Zookeeper SpringBoot IDEA整合Dubbo+Zookeeper+SpringBoot实现
米兰的小铁匠z 人气:0本文主要介绍了IDEA整合Dubbo+Zookeeper+SpringBoot实现,分享给大家,具体如下:
放上一张Dubbo的架构图
刚刚学过SpringCloud,Dubbo虽然没有Spring家族那么强悍,但始终不能违背Dubbo是一款高性能优秀的RPC框架。
自己也是初学者,想自己动手做一个简单的demo,于是上网查资料,发现针对那种入门的案例,网上主要有两个比较经典的,一个是尚硅谷雷神版,还有一个是官网推荐的入门 案例。
雷神的Dubbo视频教程大神讲解
视频中用的eclipse开发,习惯IDEA开发的我们不免手痒想要使用IDEA整合一波。
1. 提出需求
业务拆分
再次拆分
2. 环境准备
1. Zookeeper-3.4.14
2. dubbo-admin 可视化管理界面
安装过程参照雷神视频,没什么技术含量
准备好三个SpringBoot模块
3. 业务接口整合
这个模块的主要功能在于分包解耦
UserAddress.java:
public class UserAddress implements Serializable { private Integer id; private String userAddress; private String userId; private String consignee; private String phoneNum; private String isDefault; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getConsignee() { return consignee; } public void setConsignee(String consignee) { this.consignee = consignee; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getIsDefault() { return isDefault; } public void setIsDefault(String isDefault) { this.isDefault = isDefault; } public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum, String isDefault) { this.id = id; this.userAddress = userAddress; this.userId = userId; this.consignee = consignee; this.phoneNum = phoneNum; this.isDefault = isDefault; } @Override public String toString() { return "UserAddress{" + "id=" + id + ", userAddress='" + userAddress + '\'' + ", userId='" + userId + '\'' + ", consignee='" + consignee + '\'' + ", phoneNum='" + phoneNum + '\'' + ", isDefault='" + isDefault + '\'' + '}'; } }
OrderService.java
public interface OrderService { //初始化订单模块 public List<UserAddress> initOrder(String userId); }
UserService.java
public interface UserService { //消费订单模块 public List<UserAddress> getUserAddressList(String userId); }
4. 服务提供者
UserServiceImpl.java
@Component @Service //用的是com.alibaba.dubbo.config.annotation.Service,目的是为了在注册中心暴露服务 public class UserServiceImpl implements UserService { @Override public List<UserAddress> getUserAddressList(String userId) { UserAddress userAddress1 = new UserAddress(1,"福建省莆田市","37","刘老师","10086","1"); UserAddress userAddress2 = new UserAddress(2,"福建省福州市","33","黄老师","1001","1"); List<UserAddress> addresses = new ArrayList<>(); addresses.add(userAddress1); addresses.add(userAddress2); return addresses; } }
UserServiceProviderApplication.java
@EnableDubbo //开启Dubbo @SpringBootApplication public class UserServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(UserServiceProviderApplication.class, args); } }
application.properties
#应用端口 server.port=8080 #dubbo服务地址 dubbo.application.name=user-service-provider #zk注册中心协议和地址 dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181 #通信规则(通信协议和端口) dubbo.protocol.name=dubbo dubbo.protocol.port=20880 #连接监控中心(这里面我没有启动监控中心) dubbo.monitor.protocol=registry
pom.xml 新增依赖
<!-- 引入公共API,以实现其接口 --> <dependency> <groupId>com.liuzeyu</groupId> <artifactId>gmall-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 引入spring-boot-starter以及dubbo和curator的依赖 --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
5. 服务消费者
OrderController.java
@RequestMapping("order") @Controller public class OrderController { @Autowired private OrderService orderService; @ResponseBody //以json格式返回 @RequestMapping("/initOrder") public List<UserAddress> initOrder(@RequestParam("uid") String userId){ List<UserAddress> userAddresses = orderService.initOrder(userId); return userAddresses; } }
OrderServiceImpl.java
@Service //暴露服务 @Component public class OrderServiceImpl implements OrderService { //远程调用服务 @Reference(url = "dubbo://127.0.0.1:20880") private UserService userService; @Override public List<UserAddress> initOrder(String userId) { return userService.getUserAddressList(userId); } }
@Reference
这边的URL指向需要注意,因为我们的通信协议使用dubbo,地址和端口也必须指明,否则会发现UserService 注入为null的现象!
OrderServiceConsumerApplication.java
@EnableDubbo @SpringBootApplication public class OrderServiceConsumerApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceConsumerApplication.class, args); } }
application.properties
#dubbo服务名称 dubbo.application.name=order-service-consumer #zk地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 #监控中心配置 dubbo.monitor.protocol=registry #这个需要注意,每一个服务的dubbo端口不能冲突 dubbo.protocol.port=20881 server.port=8081
pom.xml
<!-- springboot web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.liuzeyu</groupId> <artifactId>gmall-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 引入spring-boot-starter以及dubbo和curator的依赖 --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
6. 测试
DEMO的测试流程应该这样
搭建ZK环境
搭建Dubbo可视化管理界面
项目启动
查看是否服务注册进来
接口调式
访问:http://localhost:8081/order/initOrder?uid=33
实现了RPC远程通信!!
7. 最后附上另外两种配置文件整合方式
7.1 XML整合配置文件
因为SpringBoot屏蔽了大量的XML配置,但是dubbo官方又建议我们使用XML配置,所以整合的最后附上XML配置
consumer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识--> <!-- 对应properties:dubbo.application.name=user-service-provider--> <dubbo:application name="order-service-consumer" owner="lzy" organization="hgfzp"/> <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper--> <!-- 对应properties:dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <!-- 对应properties:dubbo.protocol.name=dubbo dubbo.protocol.port=20880--> <dubbo:protocol name="dubbo" port="20881" /> <!--使用 dubbo 协议实现定义好的 com.liuzeyu.service.UserService 接口--> <!-- 对应@Component @Service注入UserServiceImpl--> <dubbo:service interface="com.liuzeyu.service.OrderService" ref="orderService" protocol="dubbo" /> <!--具体实现该接口的 bean--> <bean id="orderService" class="com.liuzeyu.service.impl.OrderServiceImpl"/> <!-- 配置监控中心--> <dubbo:monitor protocol="registry"/> </beans>
provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识--> <!-- 对应properties:dubbo.application.name=user-service-provider--> <dubbo:application name="user-service-provider" owner="lzy" organization="hgfzp"/> <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper--> <!-- 对应properties:dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <!-- 对应properties:dubbo.protocol.name=dubbo dubbo.protocol.port=20880--> <dubbo:protocol name="dubbo" port="20880" /> <!--使用 dubbo 协议实现定义好的 com.liuzeyu.service.UserService 接口--> <!-- 对应@Component @Service注入UserServiceImpl--> <dubbo:service interface="com.liuzeyu.service.UserService" ref="userService" protocol="dubbo" /> <!--具体实现该接口的 bean--> <bean id="userService" class="com.liuzeyu.service.impl.UserServiceImpl"/> <dubbo:monitor protocol="registry"/> </beans>
如需要启动测试记住需要注释掉之前加在接口实现类上面的服务暴露配置
OrderServiceImpl:
UserServiceImpl:
7.2 配置类整合
通过配置自定义配置类注入配置信息
OrderConfig.java 服务提供者使用
@Configuration public class OrderConfig { @Bean public ApplicationConfig applicationConfig(){ ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("user-service-provider"); applicationConfig.setOwner("lzy"); applicationConfig.setOrganization("hgfzp"); return applicationConfig; } @Bean public RegistryConfig registryConfig(){ RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setProtocol("zookeeper"); registryConfig.setAddress("127.0.0.1:2181"); return registryConfig; } @Bean public ProtocolConfig protocolConfig(){ ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20880); return protocolConfig; } @Bean public ServiceConfig<UserService> serviceConfig(UserService userService){ ServiceConfig<UserService> serviceConfig = new ServiceConfig<UserService>(); serviceConfig.setInterface(UserService.class); serviceConfig.setRef(userService); //配置每一个Method信息 List<MethodConfig> methodConfigs = new ArrayList<>(); MethodConfig methodConfig = new MethodConfig(); methodConfig.setId("userService"); methodConfig.setName("getUserAddressList"); methodConfigs.add(methodConfig); serviceConfig.setMethods(methodConfigs); return serviceConfig; } }
ConsumerConfig.java 服务提供者使用
@Configuration public class ConsumerConfig { @Bean public ApplicationConfig applicationConfig(){ ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("order-service-consumer"); applicationConfig.setOwner("lzy"); applicationConfig.setOrganization("hgfzp"); return applicationConfig; } @Bean public RegistryConfig registryConfig(){ RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setProtocol("zookeeper"); registryConfig.setAddress("127.0.0.1:2181"); return registryConfig; } @Bean public ProtocolConfig protocolConfig(){ ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20881); return protocolConfig; } @Bean public ServiceConfig<OrderService> serviceConfig(OrderService orderService){ ServiceConfig<OrderService> serviceConfig = new ServiceConfig<OrderService>(); serviceConfig.setInterface(OrderService.class); serviceConfig.setRef(orderService); //配置每一个Method信息 List<MethodConfig> methodConfigs = new ArrayList<>(); MethodConfig methodConfig = new MethodConfig(); methodConfig.setId("orderService"); methodConfig.setName("initOrder"); methodConfigs.add(methodConfig); serviceConfig.setMethods(methodConfigs); return serviceConfig; } }
使用配置类需要注意两点
需要指定dubbo配置类的包扫描路径,启动类上配置 @EnableDubbo(scanBasePackages = "com.liuzeyu")
需要将接口实现暴露出来
OrderServiceImpl
UserServiceImpl
两种方案的配置文件整合同样可以实现RPC通信,可自行测试!
加载全部内容