Dubbo Spring Cloud 服務(wù)級注冊 Consul

前言

最近在做新項目,決定采用Dubbo作為微服務(wù)調(diào)用的協(xié)議,同時由于還有部分業(yè)務(wù)需要采用Feign的調(diào)用方式。所以采用Spring Cloud + Dubbo 的服務(wù)架構(gòu)。

眾所周知Dubbo3以下為接口級別注冊。這樣會給注冊中心帶來較大的壓力,同時筆者的業(yè)務(wù)場景是Feign 調(diào)用和Dubbo調(diào)用同時存在,這樣Dubbo接口的健康檢查和Feign的健康檢查就是2個指標(biāo)。本質(zhì)上來說,Spring Cloud 的服務(wù)發(fā)現(xiàn)健康度可以反映當(dāng)前服務(wù)中Dubbo接口的健康度,因此引入 Spring Cloud Starter Dubbo

全新服務(wù)發(fā)現(xiàn)模型

Apache-Dubbo-服務(wù)自省架構(gòu)設(shè)計

版本信息

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

依賴中間件環(huán)境

consul 啟動命令 consul agent -dev -ui -client 0.0.0.0

項目結(jié)構(gòu)

parent pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.5.RELEASE</version>
    <relativePath/>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

provider-api模塊定義接口

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

provider 模塊

pom.xml

<dependencies>
<!--        采用start-web的原因是consul的健康檢查-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- consul 健康檢查 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-dubbo</artifactId>
    </dependency>

    <dependency>
        <groupId>xyz.tiecode</groupId>
        <artifactId>provider-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </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 開始,有沖突遞增,其他服務(wù)不需要關(guān)心所以-1即可
    port: -1
    name: dubbo
  consumer:
    ## 不檢查provider是否在注冊中心有,服務(wù)沒有的話消費(fèi)者無法啟動。
    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實現(xiàn)類

@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一致
調(diào)用測試 這里我們寫一個controller測試dubbo調(diào)用

@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);
    }
}

調(diào)用示例

啟動consumer/provider項目 consul 中出現(xiàn)服務(wù)


WX20211217-100331@2x.png

調(diào)用 consumer 正常返回
curl 'http://localhost:9002/url1?name=abcd'
{"desc":"returnabcd"}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容