Govern Service is a lightweight, low-cost Service registry, Service discovery, and configuration Service SDK that uses Redis in your existing infrastructure without the additional costs and burdens of an o&M deployment. Leveraging the high performance of Redis, Govern Service delivers ultra-high TPS&QPS. Govern Service combines local process caching strategy and Redis PubSub to implement real-time process cache refresh, providing unparalleled QPS performance and real-time consistency between process cache and Redis.
Service discovery
- Maven dependency. Spring-cloud-starter-loadbalancer is also required for the following inter-service calls
<dependency>
<groupId>me.ahoo.govern</groupId>
<artifactId>spring-cloud-starter-discovery</artifactId>
</dependency>
Copy the code
Service Provider
- Service provider interface
public class DemoController { @GetMapping("/get") public String demo() { return "hello provider"; }}Copy the code
- Yml configuration file, specifying the redis address
spring:
cloud:
govern:
redis:
mode: standalone
url: Redis: / / 127.0.0.1:6379
application:
name: provider
Copy the code
Service consumer
- Yml configuration file, specifying the redis address
spring:
cloud:
govern:
redis:
mode: standalone
url: Redis: / / 127.0.0.1:6379
application:
name: consumer
Copy the code
- Interface call demo
public class DemoController {
private final RestTemplate restTemplate;
@GetMapping
public String req(a) {
String url = "http://provider/get";
returnrestTemplate.getForEntity(url, String.class).getBody(); }}Copy the code
Call testing
curl http://localhost:8090
hello world
Copy the code
Configuration management
- Maven rely on
<dependency>
<groupId>me.ahoo.govern</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Copy the code
- Application. Yml configuration
spring:
application:
name: config
cloud:
govern:
config:
config-id: config.yml
redis:
mode: standalone
url: redis://localhost:6379
Copy the code
- Code injection configures the key
@RefreshScope
public class DemoController {
@Value("${config.key}")
private String key;
@GetMapping
public String demo(a){
returnkey; }}Copy the code
- New Configuration Management
conclusion
-
The author uses the infrastructure Redis as the registration and configuration center to realize the micro-service registration discovery and configuration management based on Spring Cloud Commons standard. Govern Service source code is very concise, for beginners to learn reference Spring Cloud core source code design is very helpful.
-
During the Spring Festival, Based on Spring Cloud Commons, pig-Mesh implements all Spring Cloud abstractions (Service discovery, configuration management, load balancing, language heterogeneity), which can be studied together with Government Service.