【Dubbo系列1】Dubbo与Spring的集成

180 阅读5分钟

往期精选(欢迎转发~~)

结合Zookeeper,讲解Dubbo集成Spring流程。

前言

Java中用到的微服务框架主要包括Dubbo和SpringCloud,这篇文章主要记录Dubbo集成Spring的整体流程,然后再简单分析Dubbo在Zookeeper中注册的信息。

感觉微服务的服务发现模块都差不多,Jave喜欢用zookeeper,Go喜欢用etcd,下面的集成示例,其实就是围绕这幅图展开的。

Zookeeper环境部署

Dubbo微服务框架依赖Zookeeper,所以需要先部署Zookeeper环境,可以参考:blog.csdn.net/zsy3313422/…

Dubbo与Spring集成

目录结构

对外暴露接口(gmall-interface)

UserAddress.java

@Data
public class UserAddress implements Serializable {
    private Integer id;
    private String userAddress; //用户地址
    private String userId; //用户id
    private String consignee; //收货人
    private String phoneNum; //电话号码
    private String isDefault; //是否为默认地址    Y-是     N-否

    public UserAddress(int 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;
    }
}

OrderService.java

public interface OrderService {
    //初始化订单
    public List<UserAddress> initOrder(String userId);
}

UserService.java

public interface UserService {
    //按照用户id返回所有的收货地址
    public List<UserAddress> getUserAddressList(String userId);
}

这个很简单,主要是提供UserService接口,供服务提供方注册Dubbo,供消费方调用。

服务提供方(order-service-provider)

UserServiceImpl.java

@Service
public class UserServiceImpl implements UserService {

    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        //模拟获取数据过程,这里为简化,自定义两个地址对象返回
        UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
        UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座9层", "1", "王老师", "010-56253825", "N");

        return Arrays.asList(address1, address2);
    }
}

实现getUserAddressList接口。

Provider.java

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); //堵塞一下,等待消费方调用
    }
}

启动应用,开始对外提供服务。

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:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://dubbo.apache.org/schema/dubbo
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!--    &lt;!&ndash; 开启包扫描 &ndash;&gt;-->
<!--    <context:component-scan base-package="com.louzai.gmall.service.impl"></context:component-scan>-->

    <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
    <dubbo:application name="gmall-user-provider"></dubbo:application>

    <!-- 2、指定注册中心的位置  -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>

    <!-- 3、指定通信规则(通信协议   通信端口) -->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>

    <!-- 4、暴露服务   ref:指向服务的真正的实现对象 -->
    <dubbo:service interface="com.louzai.gmall.service.UserService" ref="userServiceImpl"></dubbo:service>

    <!-- 服务的实现对象 -->
    <bean id="userServiceImpl" class="com.louzai.gmall.service.impl.UserServiceImpl"></bean>

</beans>

注册Zookeeper、指定通信端口、通过dubbo对外暴露接口。

pom.xml中加入依赖:

<dependency>
    <groupId>org.example</groupId>
    <artifactId>gmall-interface</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

<!-- 引入dubbo -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
</dependency>

<!-- 由于我们使用zookeeper作为注册中心,所以需要操作zookeeper dubbo 2.6以前的版本引入zkclient操作zookeeper
            dubbo 2.6及以后的版本引入curator操作zookeeper  -->
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>
<!-- curator-framework -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.12.0</version>
</dependency>
<dependency>
    <groupId>org.example</groupId>
    <artifactId>gmall-interface</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>

服务使用方(order-service-consumer)

OrderServiceImpl.java

/**
 * 1、将服务提供者注册到注册中心(暴露服务)
 *         1)、导入dubbo依赖(2.6.2)\操作zookeeper的客户端(curator)
 *         2)、配置服务提供者
 * 2、让服务消费者去注册中心订阅服务提供者的服务地址
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    UserService userService;

    @Override
    public List<UserAddress> initOrder(String userId) {
        System.out.println("用户id:"+userId);

        List<UserAddress> addressList = userService.getUserAddressList(userId);
        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress.getUserAddress());
        }
        return addressList;
    }
}

实现initOrder接口,内部调用dubbo提供的接口。

Consumer.java

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        OrderService orderService = context.getBean(OrderService.class);

        orderService.initOrder("1");
        System.out.println("调用完成....");
        System.in.read();
    }
}

启动服务,模拟dubbo调用。

comsumer.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 开启包扫描 -->
    <context:component-scan base-package="com.louzai.gmall.service.impl"></context:component-scan>

    <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
    <dubbo:application name="gmall-order-consumer"></dubbo:application>
    <!-- 2、指定注册中心的位置 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    <!-- 3、声明需要调用的远程服务的接口;生成远程服务代理 -->
    <dubbo:reference id="userService" interface="com.louzai.gmall.service.UserService"></dubbo:reference>
</beans>

指定Zookeeper注册入口,并指定调用dubbo的接口,通过Zookeeper对下游进行调用。

pom.xml中加入依赖:同provider

项目整合

pom.xml,在工程项目中添加如下module:

<modules>
    <module>gmall-interface</module>
    <module>user-service-provider</module>
    <module>order-service-consumer</module>
</modules>

这个其实可以不用手动添加,直接在父项目中创建子工程,创建方式为:

启动服务

在保证Zookeeper服务启动的情况下,分别启动两个子工程provider和consumer,截图如下:

这里需要注意,一定是先启动provider,在启动consumer,否则会报错!

Zookeeper注册分析

进入zookeeper的bin目录,执行:

./zkCli.sh -server 127.0.0.1:2181

可以通过如下命令获取注册信息:

ls /dubbo/com.louzai.gmall.service.UserService/consumers
ls /dubbo/com.louzai.gmall.service.UserService/providers

将注册信息格式化后,可以看到内部的注册信息:

dubbo://192.168.31.80:20880/com.louzai.gmall.service.UserService?anyhost=true&application=gmall-user-provider&dubbo=2.6.2&generic=false&interface=com.louzai.gmall.service.UserService&methods=getUserAddressList&pid=38557&side=provider&timestamp=1625365982680
consumer://192.168.31.80/com.louzai.gmall.service.UserService?application=gmall-order-consumer&category=consumers&check=false&dubbo=2.6.2&interface=com.louzai.gmall.service.UserService&methods=getUserAddressList&pid=38581&side=consumer&timestamp=1625366019812

后记

为了集成这个Demo,从昨天晚上9点半搞到今天中午才搞完(晚上肯定还是要睡觉的哈),下午再花一个多小时写成文章,一个周日就这样过去了,感觉还是挺花时间的。后面还有个Dubbo和Spring Boost的集成,感觉都差不多,无非就是配置形式不一样,后面有时间可以再试一下,目前就不打算再倒腾了。

下午再学习一会,准备看部电影,犒劳一下自己,忘了,我老婆还让我帮他打扫卫生。。。

欢迎大家多多点赞,更多文章,请关注微信公众号“楼仔进阶之路”,点关注,不迷路~~