前言
最近在做新項目,決定采用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
版本信息
- 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"}