Govern Service 是一個輕量級、低成本的服務注冊、服務發(fā)現(xiàn)、 配置服務 SDK,通過使用現(xiàn)有基礎設施中的 Redis (相信你已經(jīng)部署了 Redis),不用給運維部署帶來額外的成本與負擔。 借助于 Redis 的高性能, Govern Service 提供了超高 TPS&QPS。Govern Service 結合本地進程緩存策略 + Redis PubSub,實現(xiàn)實時進程緩存刷新,兼具無與倫比的 QPS 性能、進程緩存與 Redis 的實時一致性。
服務發(fā)現(xiàn)
image
- maven 依賴,如下服務間調用還需要添加 spring-cloud-starter-loadbalancer
<dependency>
<groupId>me.ahoo.govern</groupId>
<artifactId>spring-cloud-starter-discovery</artifactId>
</dependency>
服務提供方
- 服務提供方接口
public class DemoController {
@GetMapping("/get")
public String demo() {
return "hello provider";
}
}
- application.yml 配置文件,指定 redis 地址
spring:
cloud:
govern:
redis:
mode: standalone
url: redis://127.0.0.1:6379
application:
name: provider
服務消費方
- application.yml 配置文件,指定 redis 地址
spring:
cloud:
govern:
redis:
mode: standalone
url: redis://127.0.0.1:6379
application:
name: consumer
- 接口調用演示
public class DemoController {
private final RestTemplate restTemplate;
@GetMapping
public String req() {
String url = "http://provider/get";
return restTemplate.getForEntity(url, String.class).getBody();
}
}
調用測試
curl http://localhost:8090
hello world
配置管理
image
- maven 依賴
<dependency>
<groupId>me.ahoo.govern</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- application.yml 配置
spring:
application:
name: config
cloud:
govern:
config:
config-id: config.yml
redis:
mode: standalone
url: redis://localhost:6379
- 代碼注入配置 key
@RefreshScope
public class DemoController {
@Value("${config.key}")
private String key;
@GetMapping
public String demo(){
return key;
}
}
- 新增配置管理
image
總結
作者使用基礎設施 redis 作為注冊、配置中心,實現(xiàn)基于 Spring Cloud Commons 標準的 微服務注冊發(fā)現(xiàn)、配置管理。 Govern Service 源碼非常的簡潔,對于初學者學習參考 Spring Cloud 核心源碼設計非常有幫助。
筆者曾在春節(jié)期間,基于 Spring Cloud Commons 實現(xiàn)了一套 pig-mesh 基本實現(xiàn)了全部的 Spring Cloud 抽象 (服務發(fā)現(xiàn)、配置管理、負載均衡、語言異構) 可以和 Govern Service 一并參考學習。