SpringBoot integrates Dubbo and zookeeper

Springboot integrates Dubbo and Zookeeper

The source address of this article: https://gitee.com/liuhuanhuan963019/zookeeper_dubbo.git

1. Test environment setup

Apache Dubbo |ˈdʌbəʊ| is a high-performance, lightweight open-source Java RPC framework that provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery.

Dubbo official website http://dubbo.apache.org/zh-cn/index.html

1. Understand the features of Dubbo

2. Check the official documentation

basic concept of dubbo

Service Provider (Provider): The service provider that exposes the service. When the service provider starts, it registers the service it provides with the registry.

Service consumer (Consumer): The service consumer that invokes the remote service. When the service consumer starts, it subscribes to the registry for the service it needs. The service consumer selects the provider address list based on the soft load balancing algorithm. One provider makes the call, and if the call fails, another call is chosen.

Registry: The registry returns the service provider address list to the consumer. If there is a change, the registry will push the change data to the consumer based on the long connection

Monitoring Center (Monitor): Service consumers and providers, accumulate the number of calls and call time in memory, and regularly send statistical data to the monitoring center every minute

Description of calling relationship

l The service container is responsible for starting, loading, and running the service provider.

l When the service provider starts up, it registers the services it provides with the registry.

l When the service consumer starts, it subscribes to the registration center for the services it needs.

l The registry returns the service provider address list to the consumer. If there is a change, the registry will push the change data to the consumer based on the persistent connection.

l Service consumers, from the provider address list, based on the soft load balancing algorithm, select a provider to call, and if the call fails, select another provider to call.

l Service consumers and providers accumulate the number of calls and call times in memory, and regularly send statistical data to the monitoring center every minute.

2.dubbo environment construction

Click into the dubbo official documentation, we recommend that we use the Zookeeper registry

What is zookeeper? You can view the official documentation

3. Install zookeeper under windows

1. Download zookeeper: address, http://mirrors.cnnic.cn/apache/zookeeper/ We download the latest version! Unzip zookeeper

2. Run /bin/zkServer.cmd, the first run will report an error, there is no zoo.cfg configuration file; we only need to copy a copy of zoo_sample.cfg in the conf directory and rename it to zoo.cfg

May encounter a problem: flashback!

Solution: Edit the zkServer.cmd file and add pause at the end. In this way, the operation will not exit when an error occurs, and an error message will be prompted to facilitate finding the cause.


3. Modify the zoo.cfg configuration file

Copy the zoo_sample.cfg under the conf folder and rename it to zoo.cfg.

Note a few important locations:

dataDir=./ directory for temporary data storage (writable relative path)

clientPort=2181 port number of zookeeper

After the modification is completed, start zookeeper again

It has been successfully started

4. Use zkCli.cmd to test

ls/: List all nodes saved under the zookeeper root

[zk: 127.0.0.1:2181(CONNECTED) 4] ls /
[zookeeper]

create –e /lhh 123: Create an lhh node with a value of 123

get /kuangshen: Get the value of the /kuangshen node

Let's look at the nodes again

4. Install dubbo-admin under windows

dubbo itself is not a service software. It is actually a jar package that can help your java program connect to zookeeper, and use zookeeper to consume and provide services.

However, in order to allow users to better manage and monitor many dubbo services, the official provides a visual monitoring program dubbo-admin, but this monitoring does not affect the use even if it is not installed.

Let's install it here:

1. Download dubbo-admin

Address: https://github.com/apache/dubbo-admin/tree/master Remember to choose the version with version number 0.2.0

2. Unzip into the directory

Modify dubbo-admin\src\main\resources \application.properties to specify the zookeeper address

server.port=7001
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.layout-url=/templates/default.vm
spring.messages.fallback-to-system-locale=false
spring.messages.basename=i18n/message
spring.root.password=root
spring.guest.password=guest

dubbo.registry.address=zookeeper://127.0.0.1:2181

3. Package dubbo-admin in the project directory

mvn clean package -Dmaven.test.skip=true

** The process of packing for the first time is a bit slow, you need to be patient! until it succeeds!

4. Execute dubbo-admin-0.0.1-SNAPSHOT.jar under dubbo-admin\target

java -jar dubbo-admin-0.0.1-SNAPSHOT.jar

[Note: The zookeeper service must be turned on! ]

After the execution is complete, let's visit http://localhost:7001/ , at this time we need to enter the login account and password, we are all default root-root;

After successful login, view the interface

The installation is complete!

5.SpringBoot + Dubbo + zookeeper

5.1 Framework Construction

1. Start zookeeper!

2. IDEA creates an empty project;

3. Create a module to implement the service provider: provider-server, just select the web dependency

4. After the project is created, we write a service, such as a ticket selling service;

write interface

package com.kuang.provider.service;

public interface TicketService {
   public String getTicket();
}

Write the implementation class

package com.kuang.provider.service;

public class TicketServiceImpl implements TicketService {
   @Override
   public String getTicket() {
       return "<Mad God says Java>";
  }
}

5. Create a module to implement service consumers: consumer-server, just select web dependencies

6. After the project is created, we write a service, such as a user's service;

write service

package com.kuang.consumer.service;

public class UserService {
   //We need to get the services that go to the registry
}

Requirement: Now our users want to use the service of buying tickets, how to do this? **

5.2 Service Providers

1. To register the service provider to the registry, we need to integrate Dubbo and zookeeper, so we need to guide the package

We enter github from the dubbo official website, look at the help document below, find dubbo-springboot, and find the dependency package

<!-- Dubbo Spring Boot Starter -->
<dependency>
   <groupId>org.apache.dubbo</groupId>
   <artifactId>dubbo-spring-boot-starter</artifactId>
   <version>2.7.3</version>
</dependency>

Let's go to the maven repository to download the zookeeper package, zkclient;**

<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
   <groupId>com.github.sgroschupf</groupId>
   <artifactId>zkclient</artifactId>
   <version>0.1</version>
</dependency>

[New version of the pit] zookeeper and its dependency packages, to resolve log conflicts, also need to remove log dependencies;

<!-- introduce zookeeper -->
<dependency>
   <groupId>org.apache.curator</groupId>
   <artifactId>curator-framework</artifactId>
   <version>2.12.0</version>
</dependency>
<dependency>
   <groupId>org.apache.curator</groupId>
   <artifactId>curator-recipes</artifactId>
   <version>2.12.0</version>
</dependency>
<dependency>
   <groupId>org.apache.zookeeper</groupId>
   <artifactId>zookeeper</artifactId>
   <version>3.4.14</version>
   <!--exclude this slf4j-log4j12-->
   <exclusions>
       <exclusion>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-log4j12</artifactId>
       </exclusion>
   </exclusions>
</dependency>

2. Configure dubbo related properties in the springboot configuration file! **

#current application name
dubbo.application.name=provider-server
#Registration center address
dubbo.registry.address=zookeeper://127.0.0.1:2181
#Scan the services under the specified package
dubbo.scan.base-packages=com.kuang.provider.service

3. Configure service annotations in the implementation class of service and publish the service! Pay attention to the problem of importing packages** At this time, the service imported bubbo package, not the spring package, must pay attention to this

import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Service //publish the service
@Component //put in a container
public class TicketServiceImpl implements TicketService {
   @Override
   public String getTicket() {
       return "<Mad God says Java>";
  }
}

Logical understanding: When the application starts, dubbo will scan the service annotated with @component under the specified package and publish it in the specified registry! **

5.3 Service consumers

1. Import dependencies, the same as the previous dependencies;

<!--dubbo-->
<!-- Dubbo Spring Boot Starter -->
<dependency>
   <groupId>org.apache.dubbo</groupId>
   <artifactId>dubbo-spring-boot-starter</artifactId>
   <version>2.7.3</version>
</dependency>
<!--zookeeper-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
   <groupId>com.github.sgroschupf</groupId>
   <artifactId>zkclient</artifactId>
   <version>0.1</version>
</dependency>
<!-- introduce zookeeper -->
<dependency>
   <groupId>org.apache.curator</groupId>
   <artifactId>curator-framework</artifactId>
   <version>2.12.0</version>
</dependency>
<dependency>
   <groupId>org.apache.curator</groupId>
   <artifactId>curator-recipes</artifactId>
   <version>2.12.0</version>
</dependency>
<dependency>
   <groupId>org.apache.zookeeper</groupId>
   <artifactId>zookeeper</artifactId>
   <version>3.4.14</version>
   <!--exclude this slf4j-log4j12-->
   <exclusions>
       <exclusion>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-log4j12</artifactId>
       </exclusion>
   </exclusions>
</dependency>

2. Configuration parameters

#current application name
dubbo.application.name=consumer-server
#Registration center address
dubbo.registry.address=zookeeper://127.0.0.1:2181
  1. Originally, the normal step is to package the interface of the service provider, and then import it with the pom file. We use a simple method here to directly take the interface of the service. The path must be correct, that is, the same as the service provider;**

  1. Improve consumer services

    package com.kuang.consumer.service;
    
    import com.kuang.provider.service.TicketService;
    import org.apache.dubbo.config.annotation.Reference;
    import org.springframework.stereotype.Service;
    
    @Service //inject into the container
    public class UserService {
    
       @Reference //When referring to the specified service remotely, he will match it according to the full class name to see who has registered the full class name with the registry.
       TicketService ticketService;
    
       public void bugTicket(){
           String ticket = ticketService.getTicket();
           System.out.println("buy at the registry"+ticket);
      }
    
    }
    
  2. Writing test classes;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerServerApplicationTests {

   @Autowired
   UserService userService;

   @Test
   public void contextLoads() {

       userService.bugTicket();

  }

}

start test

1. Enable zookeeper

2. Open dubbo-admin to monitor [you can do without]

3. Start the server

4. Consumer consumption test, results:

Test
public class ConsumerServerApplicationTests {

@Autowired
UserService userService;

@Test
public void contextLoads() {

   userService.bugTicket();

}

}

ok , this is the application of SpingBoot + dubbo + zookeeper to realize distributed development, which is actually a service splitting idea;

Tags: Spring Boot Zookeeper rpc

Posted by batterdmooie on Wed, 18 May 2022 01:48:12 +0300