Dubbo Spring Cloud 服务级注册 Consul

Dubbo Spring Cloud 服务级注册 Consul

前言

最近在做新项目,决定采用Dubbo作为微服务调用的协议,同时由于还有部分业务需要采用Feign的调用方式。所以采用Spring Cloud + Dubbo 的服务架构。

众所周知Dubbo3以下为接口级别注册。这样会给注册中心带来较大的压力,同时笔者的业务场景是Feign 调用和Dubbo调用同时存在,这样Dubbo接口的健康检查和Feign的健康检查就是2个指标。本质上来说,Spring Cloud 的服务发现健康度可以反映当前服务中Dubbo接口的健康度,因此引入 Spring Cloud Starter Dubbo

全新服务发现模型
Apache-Dubbo-服务自省架构设计

版本信息

  • spring-boot-dependencies 2.3.5.RELEASE
  • Spring Cloud Alibaba 2.2.6.RELEASE
  • Spring Cloud Hoxton.SR19

依赖中间件环境

consul 启动命令 consul agent -dev -ui -client 0.0.0.0

项目结构

parent pom.xml

<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-dependenciesartifactId>
    <version>2.3.5.RELEASEversion>
    <relativePath/>
parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>2.2.6.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>Hoxton.SR9version>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

provider-api模块定义接口

public interface BizService {
    RespVO hello(ReqVO reqVO);
}

provider 模块

pom.xml

<dependencies>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-consul-discoveryartifactId>
    dependency>
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>spring-cloud-starter-dubboartifactId>
    dependency>

    <dependency>
        <groupId>xyz.tiecodegroupId>
        <artifactId>provider-apiartifactId>
        <version>1.0-SNAPSHOTversion>
    dependency>

dependencies>

bootstrap.yml

server:
  port: 9002
spring:
  application:
    name: demo-consumer
  cloud:
    #采用 spring cloud consul注册
    consul:
      discovery:
        service-name: ${spring.application.name}
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
        prefer-ip-address: true
        health-check-path: /actuator/health
        health-check-interval: 10s
        query-passing: true
dubbo:
  registry:
    #挂载到springcloud注册
    address: spring-cloud://localhost
  protocol:
    # -1 表示从从 20880 开始,有冲突递增,其他服务不需要关心所以-1即可
    port: -1
    name: dubbo
  consumer:
    ## 不检查provider是否在注册中心有,服务没有的话消费者无法启动。
    check: false

provider main方法

@SpringBootApplication
@EnableDiscoveryClient
@EnableDubbo(scanBasePackages = "xyz.tiecode") //dubbo包扫描
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

service实现类

@DubboService
public class BizServiceImpl implements BizService {
    @Override
    public RespVO hello(ReqVO reqVO) {
        RespVO respVO = new RespVO();
        respVO.setDesc("return" + reqVO.getName());
        return respVO;
    }
}

consumer模块

bootstrap.yml 和 pom 配置同provider注意修改server.port
Application main 方法同provider一致
调用测试 这里我们写一个controller测试dubbo调用

@RestController
public class HomeController {

    @DubboReference // 这里采用DubboReference注入dubbo接口
    private BizService bizService;

    @GetMapping("url1")
    public RespVO hello(@RequestParam("name") String name){
        ReqVO reqVO = new ReqVO();
        reqVO.setName(name);
        return bizService.hello(reqVO);
    }
}

调用示例

启动consumer/provider项目 consul 中出现服务
Dubbo Spring Cloud 服务级注册 Consul_第1张图片

调用 consumer 正常返回
curl 'http://localhost:9002/url1?name=abcd'
{"desc":"returnabcd"}

你可能感兴趣的:(程序开发,spring,cloud,consul,dubbo)