IDEA整合Dubbo+Zookeeper+SpringBoot实现

网友投稿 208 2022-10-21


IDEA整合Dubbo+Zookeeper+SpringBoot实现

本文主要介绍了IDEA整合Dubbo+Zookeeper+SpringBoot实现,分享给大家,具体如下:

放上一张Dubbo的架构图

刚刚学过SpringCloud,Dubbo虽然没有Spring家族那么强悍,但始终不能违背Dubbo是一款高性能优秀的RPC框架。

click官网地址了解更多

自己也是初学者,想自己动手做一个简单的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 initOrder(String userId);

}

UserService.java

public interface UserService {

//消费订单模块

public List getUserAddressList(String userId);

}

4. 服务提供者

UserServiceImpl.java

@Component

@Service //用的是com.alibaba.dubbo.config.annotation.Service,目的是为了在注册中心暴露服务

public class UserServiceImpl implements UserService {

@Override

public List getUserAddressList(String userId) {

UserAddress userAddress1 = new UserAddress(1,"福建省莆田市","37","刘老师","10086","1");

UserAddress userAddress2 = new UserAddress(2,"福建省福州市","33","黄老师","1001","1");

List 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 新增依赖

com.liuzeyu

gmall-interface

0.0.1-SNAPSHOT

com.alibaba.boot

dubbo-spring-boot-starter

0.2.0

5. 服务消费者

OrderController.java

@RequestMapping("order")

REHquVZdyg@Controller

public class OrderController {

@Autowired

private OrderService orderService;

@ResponseBody //以json格式返回

@RequestMapping("/initOrder")

public List initOrder(@RequestParam("uid") String userId){

List userAddresses = ohttp://rderService.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 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

org.springframework.boot

spring-boot-starter-web

com.liuzeyu

gmall-interface

0.0.1-SNAPSHOT

com.alibaba.boot

dubbo-spring-boot-starter

0.2.0

6. 测试

DEMO的测试流程应该这样

搭建ZK环境

搭建Dubbo可视化管理界面

项目启动

查看是否服务注册进来

接口调式

访问:http://localhost:8081/order/initOrder?uid=33

实现了RPC远程通信!!

7. 最后附上另外两种配置文件整合方式

7.1 XML整合配置文件

因为SpringBoot屏蔽了大量的XML配置,但是dubbo官方又建议我们使用XML配置,所以整合的最后附上XML配置

consumer.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

provider.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

如需要启动测试记住需要注释掉之前加在接口实现类上面的服务暴露配置

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 serviceConfig(UserService userService){

ServiceConfig serviceConfig = new ServiceConfig();

serviceConfig.setInterface(UserService.class);

serviceConfig.setRef(userService);

//配置每一个Method信息

List 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("hgfzphttp://");

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 serviceConfig(OrderService orderService){

ServiceConfig serviceConfig = new ServiceConfig();

serviceConfig.setInterface(OrderService.class);

serviceConfig.setRef(orderService);

//配置每一个Method信息

List 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通信,可自行测试!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:VoIP常见编码的带宽计算
下一篇:Linux sudo 提权之软链接
相关文章

 发表评论

暂时没有评论,来抢沙发吧~