Welcome to my GitHub

Github.com/zq2599/blog…

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

Welcome to my GitHub

Here classification and summary of xinchen all original (including supporting source code) : github.com/zq2599/blog…

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 second part of “Dubbo combat” series, together with the actual combat SpringBoot (2.3.3.RELEASE version) and dubbo integration, which is also a common technology combination of distributed services, this article consists of the following content:
  1. Springbootmulticastprovider create child engineering, providing services;
  2. Springbootmulticastconsumer, create a project started provides a web interface, let’s call this web interface, Springbootmulticastconsumer will remote call springbootmulticastprovider services, the following figure:

  1. The actual practice of this paper does not use the registry for the time being. Instead, the service provider broadcasts its own address when it starts, and then the consumer subscribes when it starts, and calls it remotely at any time, as shown in the following figure:

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 parent-child structure engineering, this code in springbootmulticastprovider and springbootmulticastconsumer these two sons of engineering, the following figure:

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 SpringBootMulticastProviderApplication.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 SpringBoot environment starter depend on:

      
<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>springbootmulticastprovider</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <name>springbootmulticastprovider</name>
    <description>Demo project for dubbo service provider from Spring Boot, multicast mode</description>

    <! -- spring-boot-starter-parent -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springboot.version}</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.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. Yml configuration file, note that registry. Address is configured in broadcast mode:
dubbo:
  application:
    #application-name Specifies the module name
    name: springboot-multicast-provider
    id: springboot-multicast-provider
  registry:
    address: Multicast: / / 224.5.6.7:1234
    id: registry
  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.springbootmulticastprovider;

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-multicast-provider, Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "I'm springboot-multicast-provider, Hello " + name + ", response from provider: "+ RpcContext.getContext().getLocalAddress(); }}Copy the code
  1. Write SpringBoot start class SpringBootMulticastProviderApplication. Java, pay attention to add @ EnableDubbo comments:
package com.bolingcavalry.springbootmulticastprovider;

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

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

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 SpringBootMulticastConsumerApplication.java Start the class
  • The complete file location is shown below:

  • Next, create each of these files one by one;
  1. Create project, called springbootmulticastconsumer pom. The XML content as follows, also need to rely on dubbo – spring – the boot – starter:

      
<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>springbootmulticastconsumer</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <name>springbootmulticastconsumer</name>
    <description>Demo project for dubbo service consumer from Spring Boot, multicast mode</description>

    <! -- spring-boot-starter-parent -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springboot.version}</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.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, note the value of dubo.registry. Address, in addition to broadcast mode, and add unicast=false to ensure that multiple consumer processes can receive broadcasts:
dubbo:
  application:
    name: springboot-multicast-consumer
    id: springboot-multicast-consumer
    qosEnable: false
  registry:
    address: Multicast: / / 224.5.6.7:1234? unicast=false
    id: registry
  protocol:
    name: dubbo
    port: 20880
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.springbootmulticastconsumer.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 (multicast mode) : "+ demoService.sayHello(name); }}Copy the code
  1. Write a Controller class that provides web services externally:
package com.bolingcavalry.springbootmulticastconsumer.controller;

import com.bolingcavalry.springbootmulticastconsumer.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.springbootmulticastconsumer;

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.springbootmulticastconsumer.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 (broadcast mode)")
                / / 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 SpringBootMulticastConsumerApplication. Java:
package com.bolingcavalry.springbootmulticastconsumer;

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

@SpringBootApplication
@EnableDubbo
public class SpringBootMulticastConsumerApplication {
    public static void main(String[] args) { SpringApplication.run(SpringBootMulticastConsumerApplication.class, args); }}Copy the code
  1. At this point, the service consumer code completion, directly on the IDEA of running SpringBootMulticastConsumerApplication class can start;
  2. Through the browser to access swagger, address is: http://localhost:8081/swagger-ui.html, as the chart, click the red box position on interface details:

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

  1. Below the red box is the response data, the content is springbootmulticastconsumer remote call springbootmulticastprovider service:

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

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…