Welcome to my GitHub

Github.com/zq2599/blog…

Content: all original article classification summary and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;

Dubbo Combat series navigation

  1. Preparation and initial experience
  2. With SpringBoot integration
  3. Use the Zookeeper registry
  4. Admin console dubo-admin

This paper gives an overview of

  • This article is the third in the dubbo Live Series, focusing on the use of Zookeeper as a registry;
  • The whole environment is composed of service providers and service consumers. This simple form is just for easy learning. In fact, most cases will deploy a registry, where service providers register, and service consumers get specific service information from the registry.
  • The following is a typical architecture diagram for a typical provider/consumer/registry:

  • Dubbo’s official recommended registry is Zookeeper, as shown below:

About the Zookeeper

  1. This time, single-node Zookeeper, version 3.4.13, IP192.168.50.43, port 2181 is used
  2. As for the deployment of Zookeeper, I made rapid deployment through Docker. Please refer to the article “CDH DS218+ Deployment Kafka”.
  • Once you have Zookeeper deployed, start coding the service provider and consumer;

Download the source code

  1. If you don’t want to code, you can download all the source code at GitHub, with the following address and link information:
The name of the link note
Project home page Github.com/zq2599/blog… The project’s home page on GitHub
Git repository address (HTTPS) Github.com/zq2599/blog… The project source warehouse address, HTTPS protocol
Git repository address (SSH) [email protected]:zq2599/blog_demos.git The project source warehouse address, SSH protocol
  1. The Git project has multiple folders, and the application of this chapter is in the Dubbopractice folder, as shown in the red box below:

3. Dubbopractice is a parent-child project. The code of this project is in the two sub-projects springBootzkProvider and SpringBootzkConsumer, as shown below:

Code (Service Provider)

  • To create service engineering springbootmulticastprovider, altogether to create four files, create order and function in the following table:
Create order The file name role
1 pom.xml Project POM document
2 src/main/resources/application.yml The configuration file
3 DemoServiceImpl.java Provide specific services
4 SpringBootZKProviderApplication.java Start the class
  • The complete file location is shown below:

  • Next create each of these one by one;
  1. Create project, called springbootmulticastprovider pom. The XML content as follows, is to focus on new rely on dubbo – spring – the boot – starter, this is dubbo in starter SpringBoot environment depend on, Dubo-dependencies – Zookeeper:

      
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbopractice</artifactId>
        <groupId>com.bolingcavalry</groupId>
        <version>1.0 the SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bolingcavalry</groupId>
    <artifactId>springbootzkprovider</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <name>springbootzkprovider</name>
    <description>Demo project for dubbo service provider from Spring Boot</description>

    <! -- spring-boot-starter-parent -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.3. RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.bolingcavalry</groupId>
            <artifactId>practiceinterface</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${springboot.version}</version>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code
  1. Application configuration file. Yml, should pay attention to the registry. Address the value of the zookeeper: / / 192.168.50.43:2181, this is the configuration of a zookeeper registry:
dubbo:
  application:
    #application-name Specifies the module name
    name: springboot-zk-provider
  registry:
    address: Zookeeper: / / 192.168.50.43:2181
  protocol:
    name: dubbo
    port: 20880
Copy the code
  1. Write the Service implementation class DemoServiceImp.java, noting that the @service annotation exposes the instance of the current class as a remote Service:
package com.bolingcavalry.springbootzkprovider;

import com.bolingcavalry.dubbopractice.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;

@Slf4j
@Service
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        log.info("I'm springboot-zk-provider, Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "I'm springboot-zk-provider, Hello " + name + ", response from provider: "+ RpcContext.getContext().getLocalAddress(); }}Copy the code
  1. Write SpringBoot start class SpringBootZKProviderApplication. Java, pay attention to add @ EnableDubbo comments:
package com.bolingcavalry.springbootzkprovider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class SpringBootZKProviderApplication {
    public static void main(String[] args) { SpringApplication.run(SpringBootZKProviderApplication.class, args); }}Copy the code
  1. At this point the provider code completion, directly on the IDEA of running SpringBootZKProviderApplication class can start the service, after the success of the start of the log output, as shown, as shown in the red box has connected the Zookeeper:

Code (service consumer)

  • Now that the service is available on the network, let’s write the code of the consuming party. A total of 6 files will be created. The creation sequence and functions are shown in the following table:
Create order The file name role
1 pom.xml Project POM document
2 src/main/resources/application.yml The configuration file
3 RemoteInvokeServiceImpl.java The Service layer, where the service provider’s services are invoked remotely
4 DemoController.java Web interface class to provide Web services externally
5 SwaggerConfig.java The Swagger configuration class facilitates testing interfaces through pages
6 SpringBootZKConsumerApplication.java Start the class
  • The complete file location is shown below:

  • Next, create each of these files one by one;
  1. Dubo-spring-boot-starter and dubo-dependencies -zookeeper: < springBootzkConsumer >

      
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbopractice</artifactId>
        <groupId>com.bolingcavalry</groupId>
        <version>1.0 the SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bolingcavalry</groupId>
    <artifactId>springbootzkconsumer</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <name>springbootzkconsumer</name>
    <description>Demo project for dubbo service consumer from Spring Boot</description>

    <! -- spring-boot-starter-parent -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.3. RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <! -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <! -- swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
        <dependency>
            <groupId>com.bolingcavalry</groupId>
            <artifactId>practiceinterface</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${springboot.version}</version>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code
  1. Write the configuration file application.yml, noting the value of dubo.registry. Address:
dubbo:
  application:
    #application-name Specifies the module name
    name: springboot-zk-consumer
  registry:
    address: Zookeeper: / / 192.168.50.43:2181
server:
  port: 8081
Copy the code
  1. If you want to call the remote service, you only need to annotate the interface with @reference. In addition, timeout is added through the timeout attribute:
package com.bolingcavalry.springbootzkconsumer.service;

import com.bolingcavalry.dubbopractice.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service
public class RemoteInvokeServiceImpl {

    @Reference(timeout = 2000)
    private DemoService demoService;

    public String sayHello(String name) {
        return "from dubbo remote (zk registry center mode) : "+ demoService.sayHello(name); }}Copy the code
  1. Write a Controller class that provides web services externally:
package com.bolingcavalry.springbootzkconsumer.controller;

import com.bolingcavalry.springbootzkconsumer.service.RemoteInvokeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
@Api(tags = {"DemoController"})
public class DemoController {
    @Autowired
    private RemoteInvokeServiceImpl remoteInvokeService;

    @apiOperation (value =" get response from Dubbo service Provider ", notes=" get response from Dubbo service Provider ")
    @APIIMPLICITParam (name = "name", value = "nickname ", paramType = "path", Required = true, dataType = "String")
    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public String sayHello(@PathVariable String name){
        returnremoteInvokeService.sayHello(name); }}Copy the code
  1. Also add the Swagger configuration class:
package com.bolingcavalry.springbootzkconsumer;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(a) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .tags(new Tag("DemoController"."Demo Service"))
                .select()
                // The current package path
                .apis(RequestHandlerSelectors.basePackage("com.bolingcavalry.springbootzkconsumer.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    // Build the details function of the API document, note which annotation here refers to
    private ApiInfo apiInfo(a) {
        return new ApiInfoBuilder()
                // Page title
                .title("Dubbo remote call service operation (ZK registry)")
                / / founder
                .contact(new Contact("Programmer Chen"."https://github.com/zq2599/blog_demos"."[email protected]"))
                / / version number
                .version("1.0")
                / / description
                .description("API description") .build(); }}Copy the code
  1. The last is to start the class SpringBootZKConsumerApplication. Java:
package com.bolingcavalry.springbootzkconsumer;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class SpringBootZKConsumerApplication {
    public static void main(String[] args) { SpringApplication.run(SpringBootZKConsumerApplication.class, args); }}Copy the code
  1. At this point, the service consumer code completion, directly on the IDEA of running SpringBootZKConsumerApplication classes can be started, the following figure, the visible service consumer also connect the Zookeeper success:

8. Access swagger through browser, address is:http://localhost:8081/swagger-ui.html, click the position of the red box to expand the interface details:

  1. As shown below, enter web interface parameters to initiate a request:

10. The red box below shows the response data from springBootzkConsumer’s remote invocation of SpringBootzkProvider:

View the data on Zookeeper

  1. Review the official description of the data in the Zookeeper registry as follows:

  1. According to the above information, let’s go to Zookeeper to check the data and verify whether it is consistent with the above figure. After logging in to Zookeeper, execute the zkcli. sh interactive mode of progress.
  2. Check the root directory, and sure enough, the subdirectory named Dubbo:
[zk: localhost:2181(CONNECTED) 18] ls /                                                                    
[dubbo, log_dir_event_notification, isr_change_notification, zookeeper, admin, consumers, cluster, config, latest_producer_id_block, controller, brokers, controller_epoch]
Copy the code
  1. Under dubbo, there are subdirectories of the remote service:
[zk: localhost:2181(CONNECTED) 19] ls /dubbo
[com.bolingcavalry.dubbopractice.service.DemoService, config, metadata]
Copy the code
  1. The providers node of the remote service provides the following information:
[zk: Localhost: 2181 (CONNECTED) 16] get/dubbo/com bolingcavalry. Dubbopractice. Service. DemoService/will 169.254.38.208 cZxid = 0x552 ctime = Sat Oct 17 04:05:52 UTC 2020 mZxid = 0x552 mtime = Sat Oct 17 04:05:52 UTC 2020 pZxid = 0x5ec cversion = 27 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 14 numChildren = 1Copy the code
  1. Take a look at the contents of the consumer node:
[zk: Localhost: 2181 (CONNECTED) 17] get/dubbo/com bolingcavalry. Dubbopractice. Service. DemoService/consumers 169.254.38.208 cZxid = 0x55f ctime = Sat Oct 17 04:22:58 UTC 2020 mZxid = 0x55f mtime = Sat Oct 17 04:22:58 UTC 2020 pZxid = 0x5f0 cversion = 33 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 14 numChildren = 1Copy the code
  1. The data recorded on Zookeeper is consistent with that described in the official document.
  • At this point, dubbo and Springboot integration of actual combat is completed, I hope to provide you with some references, to help you quickly develop dubbo applications;

Welcome to pay attention to the public number: programmer Xin Chen

Wechat search “programmer Xin Chen”, I am Xin Chen, looking forward to enjoying the Java world with you…

You are not alone, Xinchen original accompany all the way

  1. Java series
  2. Spring series
  3. The Docker series
  4. Kubernetes series
  5. Database + middleware series
  6. The conversation series

Welcome to pay attention to the public number: programmer Xin Chen

Wechat search “programmer Xin Chen”, I am Xin Chen, looking forward to enjoying the Java world with you…

Github.com/zq2599/blog…