IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程

IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第1张图片

放上一张Dubbo的架构图
IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第2张图片
刚刚学过SpringCloud,Dubbo虽然没有Spring家族那么强悍,但始终不能违背Dubbo是一款高性能优秀的RPC框架。

click官网地址了解更多

自己也是初学者,想自己动手做一个简单的demo,于是上网查资料,发现针对那种入门的案例,网上主要有两个比较经典的,一个是尚硅谷雷神版,还有一个是官网推荐的入门 案例。

雷神的Dubbo视频教程 大神讲解

视频中用的eclipse开发,习惯IDEA开发的我们不免手痒想要使用IDEA整合一波。

IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第3张图片

1. 提出需求

IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第4张图片
业务拆分
IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第5张图片
再次拆分
IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第6张图片

2. 环境准备

1. Zookeeper-3.4.14
2. dubbo-admin 可视化管理界面

安装过程参照雷神视频,没什么技术含量

准备好三个SpringBoot模块
IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第7张图片

3. 业务接口整合

这个模块的主要功能在于分包解耦
IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第8张图片
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. 服务提供者

IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第9张图片

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


<dependency>
	<groupId>com.liuzeyugroupId>
	<artifactId>gmall-interfaceartifactId>
	<version>0.0.1-SNAPSHOTversion>
dependency>

<dependency>
	<groupId>com.alibaba.bootgroupId>
	<artifactId>dubbo-spring-boot-starterartifactId>
	<version>0.2.0version>
dependency>

5. 服务消费者

IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第10张图片
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


<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
	<groupId>com.liuzeyugroupId>
	<artifactId>gmall-interfaceartifactId>
	<version>0.0.1-SNAPSHOTversion>
dependency>


<dependency>
	<groupId>com.alibaba.bootgroupId>
	<artifactId>dubbo-spring-boot-starterartifactId>
	<version>0.2.0version>
dependency>

6. 测试

DEMO的测试流程应该这样

  1. 搭建ZK环境
    IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第11张图片

  2. 搭建Dubbo可视化管理界面
    IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第12张图片

  3. 项目启动

  4. 查看是否服务注册进来
    IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第13张图片

  5. 接口调式

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

    IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第14张图片
    实现了RPC远程通信!!

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

7.1 XML整合配置文件

因为SpringBoot屏蔽了大量的XML配置,但是dubbo官方又建议我们使用XML配置,所以整合的最后附上XML配置
在这里插入图片描述

consumer.xml


<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:application name="order-service-consumer" owner="lzy" organization="hgfzp"/>

    
    
    <dubbo:registry address="zookeeper://localhost:2181"/>

    
    
    <dubbo:protocol name="dubbo" port="20881" />

    
    
    <dubbo:service interface="com.liuzeyu.service.OrderService" ref="orderService" protocol="dubbo" />
    
    <bean id="orderService" class="com.liuzeyu.service.impl.OrderServiceImpl"/>


    <dubbo:monitor protocol="registry"/>

beans>

在这里插入图片描述

provider.xml


<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:application name="user-service-provider" owner="lzy" organization="hgfzp"/>

    
    
    <dubbo:registry address="zookeeper://localhost:2181"/>

    

    <dubbo:protocol name="dubbo" port="20880" />

    

    <dubbo:service interface="com.liuzeyu.service.UserService" ref="userService" protocol="dubbo" />
    
    <bean id="userService" class="com.liuzeyu.service.impl.UserServiceImpl"/>
    <dubbo:monitor protocol="registry"/>
beans>

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

OrderServiceImpl:
在这里插入图片描述
UserServiceImpl:
在这里插入图片描述

7.2 配置类整合

通过配置自定义配置类注入配置信息

IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第15张图片

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;
    }

}

IDEA+Dubbo+Zookeeper+SpringBoot 整合详细教程_第16张图片

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;
    }
}

使用配置类需要注意两点

  1. 需要指定dubbo配置类的包扫描路径,启动类上配置 @EnableDubbo(scanBasePackages = "com.liuzeyu")

  2. 需要将接口实现暴露出来

    OrderServiceImpl
    在这里插入图片描述

    UserServiceImpl
    在这里插入图片描述

    两种方案的配置文件整合同样可以实现RPC通信,可自行测试!

你可能感兴趣的:(高级框架,java,zookeeper,rpc)