With Zookeeper, explain the Dubbo integration Spring process.

preface

Microservice frameworks used in Java mainly include Dubbo and SpringCloud. This article mainly records the overall process of Dubbo’s integration with Spring, and then briefly analyzes the information of Dubbo’s registration in Zookeeper.

Jave likes to use ZooKeeper and Go likes to use ETCD. The following integration example is actually based on this picture.

Deploy the Zookeeper environment

Depend on the Zookeeper Dubbo micro service framework, so need to deploy the Zookeeper environment, you can refer to: blog.csdn.net/zsy3313422/…

Dubbo integrates with Spring

The directory structure

External exposed interface (GMall-interface)

UserAddress.java

@Data
public class UserAddress implements Serializable {
    private Integer id;
    private String userAddress; // User address
    private String userId; / / user id
    private String consignee; / / consignee
    private String phoneNum; // Phone number
    private String isDefault; // Is the default address Y- Yes N- No

    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; }}Copy the code

OrderService.java

public interface OrderService {
    // Initialize the order
    public List<UserAddress> initOrder(String userId);
}
Copy the code

UserService.java

public interface UserService {
    // Return all shipping addresses by user ID
    public List<UserAddress> getUserAddressList(String userId);
}
Copy the code

The UserService interface is provided for service providers to register Dubbo for consumers to invoke.

Service Provider (order-service-provider)

UserServiceImpl.java

@Service
public class UserServiceImpl implements UserService {

    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        // Simulate the process of getting data, here to simplify, custom two address object return
        UserAddress address1 = new UserAddress(1.3 / F, Hongfu Science Park, Changping District, Beijing."1"."Miss Li"."010-56253825"."Y");
        UserAddress address2 = new UserAddress(2.9 / F, Block B, West Silicon Valley Building, Bao 'an District, Shenzhen City."1"."Miss Wang"."010-56253825"."N");

        returnArrays.asList(address1, address2); }}Copy the code

Implement the getUserAddressList interface.

Provider.java

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // block and wait for the consumer to call}}Copy the code

Start the application to provide external services.

provider.xml

<? The 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; Enable packet scanning &ndash; &gt; -- > <! -- <context:component-scan base-package="com.louzai.gmall.service.impl"></context:component-scan>--> <! --> <dubbo:application name="gmall-user-provider"></dubbo:application> <! -- specify the location of the registry --> <! - "dubbo: registry address =" zookeeper: / / 127.0.0.1:2181 "> < / dubbo: registry > -- > < dubbo: registry protocol =" zookeeper" Address = "127.0.0.1:2181" > < / dubbo: registry > <! <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <! Ref: To service the realization of the real object - > < dubbo: service interface = "com. Louzai. Gmall. Service. UserService" ref = "userServiceImpl" > < / dubbo: service > <! - service implementation object - > < bean id = "userServiceImpl" class = "com. Louzai. Gmall. Service. Impl. UserServiceImpl" > < / bean > < / beans >Copy the code

Register Zookeeper, specify communication ports, and expose interfaces through Dubbo.

Add dependencies to pom.xml:

< the dependency > < groupId > org. Example < / groupId > < artifactId > gmall - interface < / artifactId > < version > 1.0 - the SNAPSHOT < / version > <scope>compile</scope> </dependency> <! Alibaba </groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <! -- Since we use ZooKeeper as the registry, Zookeeper dubbo 2.6 or later introduces zookeeper client --> <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 > < the dependency > < groupId > org. Example < / groupId > < artifactId > gmall - interface < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < scope > compile < / scope > < / dependency >Copy the code

Order-service-consumer

OrderServiceImpl.java

/** * 1, register the service provider to the registry (expose service) * 1), import dubbo dependencies (2.6.2), configure the service provider * 2, let the service consumer go to the registry to subscribe to the service provider's service address */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    UserService userService;

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

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

Implement the initOrder interface and internally call the interface provided by 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("Call completed...."); System.in.read(); }}Copy the code

Start the service, simulating the Dubbo call.

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"> <! Open the package, scan - > < context: component - scan base - package = "com. Louzai. Gmall. Service. Impl" > < / context: component - scan > <! --> <dubbo:application name="gmall-order-consumer"></dubbo:application> <! Dubbo :registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry> <! Declare the interface of the remote service to be called. Generate the remote service agent - > < dubbo: reference id = "userService" interface = "com. Louzai. Gmall. Service. UserService" > < / dubbo: reference > </beans>Copy the code

Specify the Zookeeper registration entry and the interface for calling Dubbo to call the downstream through Zookeeper.

Add dependencies to POM.xml: same as provider

Project integration

Pom.xml, add the following module to the project:

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

You can create a child project in the parent project without manually adding it:

Start the service

When the Zookeeper service is started, the two sub-projects provider and Consumer are respectively started, as shown in the screenshot below:

Start provider first, then Consumer.

Zookeeper registration analysis

Go to the bin directory of ZooKeeper and run the following command:

. / zkCli. Sh - server 127.0.0.1:2181Copy the code

You can run the following command to obtain registration information:

ls /dubbo/com.louzai.gmall.service.UserService/consumers
ls /dubbo/com.louzai.gmall.service.UserService/providers
Copy the code

After formatting the registration information, you can see the internal registration information:

Dubbo: / / 192.168.31.80:20880 / com. Louzai. Gmall. Service. The UserService? Anyhost = true&application = gmall - user - provider&dubbo = 2.6.2 & generic = false&interface = com. Louzai. Gmall. Service. UserService&me thods=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. UserServi ce&methods=getUserAddressList&pid=38581&side=consumer&timestamp=1625366019812Copy the code

Afterword.

In order to integrate the Demo, I finished it at noon from 9:30pm last night (I must have slept at night), and spent more than an hour writing the article in the afternoon. After a Sunday, I felt it took quite a long time. There is also an integration of Dubbo and Spring Boost in the back, which feels almost the same, but in a different configuration form. I can try it later, but I’m not going to do it now.

In the afternoon, I will study for a while and watch a movie to reward myself. I forgot that my wife asked me to help him clean up.

Welcome everyone to like a lot, more articles, please pay attention to the wechat public number “Lou Zai advanced road”, point attention, do not get lost ~~