Govern Service 是一个轻量级、低成本的服务注册、服务发现、 配置服务 SDK,通过应用现有基础设施中的 Redis (置信你曾经部署了 Redis),不必给运维部署带来额定的老本与累赘。 借助于 Redis 的高性能, Govern Service 提供了超高 TPS&QPS。Govern Service 联合本地过程缓存策略 + Redis PubSub,实现实时过程缓存刷新,兼具无可比拟的 QPS 性能、过程缓存与 Redis 的实时一致性。

服务发现

  • 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:8090hello world

配置管理

  • 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
@RefreshScopepublic class DemoController {    @Value("${config.key}")    private String key;    @GetMapping    public String demo(){        return key;    }}
  • 新增配置管理

总结

  • 作者应用基础设施 redis 作为注册、配置核心,实现基于 Spring Cloud Commons 规范的 微服务注册发现、配置管理。 Govern Service 源码十分的简洁,对于初学者学习参考 Spring Cloud 外围源码设计十分有帮忙。
  • 笔者曾在春节期间,基于 Spring Cloud Commons 实现了一套 pig-mesh 根本实现了全副的 Spring Cloud 形象 (服务发现、配置管理、负载平衡、语言异构) 能够和 Govern Service 一并参考学习。