乐趣区

关于云计算:dubbo实战之二与SpringBoot集成

欢送拜访我的 GitHub

https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,波及 Java、Docker、Kubernetes、DevOPS 等;

欢送拜访我的 GitHub

这里分类和汇总了欣宸的全副原创 (含配套源码):https://github.com/zq2599/blog_demos

《dubbo 实战》系列导航

  1. 筹备和初体验
  2. 与 SpringBoot 集成
  3. 应用 Zookeeper 注册核心
  4. 治理控制台 dubbo-admin

本篇概览

  • 本文是《dubbo 实战》系列的第二篇,一起来实战 SpringBoot(<font color=”red”>2.3.3.RELEASE</font> 版本)与 dubbo 的集成,这也是分布式服务的罕用技术组合,本文由以下内容组成:
  1. 创立子工程 <font color=”blue”>springbootmulticastprovider</font>,对外提供服务;
  2. 创立子工程 <font color=”blue”>springbootmulticastconsumer</font>,启动后提供一个 web 接口,咱们调用这个 web 接口时,springbootmulticastconsumer 会近程调用 springbootmulticastprovider 提供的服务,如下图:

  1. 本篇的实战暂不应用注册核心,而是服务提供方启动时播送本人的地址,再由生产方启动时订阅,并随时近程调用,调用逻辑如下图所示:

源码下载

  1. 如果您不想编码,能够在 GitHub 下载所有源码,地址和链接信息如下表所示:
名称 链接 备注
我的项目主页 https://github.com/zq2599/blo… 该我的项目在 GitHub 上的主页
git 仓库地址 (https) https://github.com/zq2599/blo… 该我的项目源码的仓库地址,https 协定
git 仓库地址 (ssh) git@github.com:zq2599/blog_demos.git 该我的项目源码的仓库地址,ssh 协定
  1. 这个 git 我的项目中有多个文件夹,本章的利用在 <font color=”blue”>dubbopractice</font> 文件夹下,如下图红框所示:

  1. <font color=”blue”>dubbopractice</font> 是父子构造的工程,本篇的代码在 <font color=”red”>springbootmulticastprovider</font> 和 <font color=”red”>springbootmulticastconsumer</font> 这两个子工程中,如下图:

编码 (服务提供方)

  • 先创立提供服务的工程 <font color=”blue”>springbootmulticastprovider</font>,一共要创立 4 个文件,创立程序和性能如下表:
创立程序 文件名 作用
1 pom.xml 工程的 pom 文件
2 src/main/resources/application.yml 配置文件
3 DemoServiceImpl.java 提供具体的服务
4 SpringBootMulticastProviderApplication.java 启动类
  • 残缺的文件地位如下图:

  • 接下来一一创立上述内容;
  1. 创立名为 <font color=”blue”>springbootmulticastprovider</font> 的子工程,pom.xml 内容如下,要重点关注的是新增依赖 <font color=”blue”>dubbo-spring-boot-starter</font>,这就是 dubbo 在 SpringBoot 环境的 starter 依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bolingcavalry</groupId>
    <artifactId>springbootmulticastprovider</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>springbootmulticastprovider</name>
    <description>Demo project for dubbo service provider from Spring Boot, multicast mode</description>

    <!-- 不必 spring-boot-starter-parent 作为 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>
  1. 配置文件 <font color=”blue”>application.yml</font>,要留神的是 <font color=”red”>registry.address</font> 的配置是播送模式:
dubbo:
  application:
    #application-name 本模块名字
    name: springboot-multicast-provider
    id: springboot-multicast-provider
  registry:
    address: multicast://224.5.6.7:1234
    id: registry
  protocol:
    name: dubbo
    port: 20880
  1. 编写服务实现类 <font color=”blue”>DemoServiceImpl.java</font>,留神 <font color=”red”>@Service</font> 注解将以后类的实例作为近程服务对外裸露:
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();
    }
}
  1. 编写 SpringBoot 启动类 <font color=”blue”>SpringBootMulticastProviderApplication.java</font>,留神要增加 <font color=”red”>@EnableDubbo 注解 </font>:
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);
    }
}
  1. 至此服务提供方编码实现,间接在 IDEA 上运行 <font color=”blue”>SpringBootMulticastProviderApplication</font> 类即可启动服务,启动胜利后的日志输入如下图:

编码 (服务生产方)

  • 当初网络上曾经有了服务,咱们再来编写服用生产方的代码,一共要创立 6 个文件,创立程序和性能如下表:
创立程序 文件名 作用
1 pom.xml 工程的 pom 文件
2 src/main/resources/application.yml 配置文件
3 RemoteInvokeServiceImpl.java service 层,在这里近程调用服务提供方的服务
4 DemoController.java web 接口类,对外提供 web 服务
5 SwaggerConfig.java swagger 配置类,便于通过页面测试接口
6 SpringBootMulticastConsumerApplication.java 启动类
  • 残缺的文件地位如下图:

  • 接下来一一创立上述文件;
  1. 创立名为 <font color=”blue”>springbootmulticastconsumer</font> 的子工程,pom.xml 内容如下,同样须要依赖 <font color=”blue”>dubbo-spring-boot-starter</font>:
<?xml version="1.0" encoding="UTF-8"?>
<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-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bolingcavalry</groupId>
    <artifactId>springbootmulticastconsumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>springbootmulticastconsumer</name>
    <description>Demo project for dubbo service consumer from Spring Boot, multicast mode</description>

    <!-- 不必 spring-boot-starter-parent 作为 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>
        <!-- swagger 依赖 -->
        <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>
  1. 编写配置文件 <font color=”blue”>application.yml</font>,留神 dubbo.registry.address 的值,除了是播送模式,还要增加 <font color=”blue”>unicast=false</font>,这样能力保障多个消费者过程都能收到播送:
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
  1. 编写调用近程服务的代码,如下,可见如果想调用近程服务,只有对接口做 @Reference 正文即可,另外还通过 <font color=”red”>timeout</font> 属性减少了超时配置:
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);
    }
}
  1. 再编写对外提供 web 服务的 Controller 类:
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 = "获取 dubbo service provider 的响应", notes="\" 获取 dubbo service provider 的响应 ")
    @ApiImplicitParam(name = "name", value = "昵称", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public String sayHello(@PathVariable String name){return remoteInvokeService.sayHello(name);
    }
}
  1. 还要增加 swagger 配置类:
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() {return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .tags(new Tag("DemoController", "演示服务"))
                .select()
                // 以后包门路
                .apis(RequestHandlerSelectors.basePackage("com.bolingcavalry.springbootmulticastconsumer.controller"))
                .paths(PathSelectors.any())
                .build();}

    // 构建 api 文档的详细信息函数, 留神这里的注解援用的是哪个
    private ApiInfo apiInfo() {return new ApiInfoBuilder()
                // 页面题目
                .title("dubbo 近程调用服务的操作 ( 播送模式)")
                // 创建人
                .contact(new Contact("程序员欣宸", "https://github.com/zq2599/blog_demos", "zq2599@gmail.com"))
                // 版本号
                .version("1.0")
                // 形容
                .description("API 形容")
                .build();}
}
  1. 最初是启动类 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);
    }
}
  1. 至此,服务生产方编码实现,间接在 IDEA 上运行 <font color=”blue”>SpringBootMulticastConsumerApplication</font> 类即可启动;
  2. 通过浏览器拜访 swagger,地址是:http://localhost:8081/swagger-ui.html,如下图,点击红框地位开展接口详情:

  1. 如下图,输出 web 接口参数发动申请:

  1. 下图红框中就是响应的数据,内容是 <font color=”blue”>springbootmulticastconsumer</font> 近程调用 <font color=”blue”>springbootmulticastprovider</font> 的服务失去的:

  • 至此,dubbo 与 springboot 集成的实战就实现了,心愿能给您提供一些参考,助您疾速开发 dubbo 利用;

你不孤独,欣宸原创一路相伴

  1. Java 系列
  2. Spring 系列
  3. Docker 系列
  4. kubernetes 系列
  5. 数据库 + 中间件系列
  6. DevOps 系列

欢送关注公众号:程序员欣宸

微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游 Java 世界 …
https://github.com/zq2599/blog_demos

退出移动版