Service governance
- The core design idea of RPC remote procedure call protocol: lies in the registry, because the registry: manages a dependency between each service and services
- Service governance: in the traditional RPC remote procedure call protocol, managing the dependency between each service is very complex Service governance technology can be used to manage a dependency between each service and service It can realize local load balancing, service discovery and registration, fault tolerance, etc
Service registration and discovery
Registration Center
- In the RPC remote procedure call protocol, there is a registry
- Spring cloud supports three kinds of album centers:
- Consul(go language)
- Eureka
- Zookeeper
- Dubbo supports two registries:
- Concept of registration center: store information related to service address (interface address) and obtain it through alias registration
- Principle:
- Start the registry first
- When a service provider starts, it registers the current service information in the registry as an alias
- When calling the interface, the service consumer uses the service alias to obtain the RPC remote call address from the registry
- After obtaining the RPC remote call address, the service consumer uses the local HttpClient technology to realize the call
- Profile:
server.port=8761 # Service port number
eureka.instance.hostname=127.0.0.1 # Registry IP address
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ # Registration url address
eureka.client.register-with-eureka=false # Whether to register yourself in the registry (you need to register when clustering)
eureka.client.fetch-registry=false # Do you need to retrieve service information from the registry
1.Label on main class@EnableEurekaServer Annotation on EurekaServer service,Open registry
Service registration
- Register service information with the registry
- Profile:
server.port=8001 # Service provider service port number
spring.application.name=Ticket-Service # Service alias, name registered with the registry: serviceId
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ # The url address at which the service provider registers with the Eureka registry
eureka.client.register-with-eureka=true # Register yourself with the registry
eureka.client.fetch-registry=true # Do you need to retrieve service information from the registry
- Service provider project:
1.Label on main class@EnableEurekaClient The annotation will the service provider(Provider)Service registration to registry
Service discovery
- Get service information from the registry
- Profile:
server.port=8200 # Service consumer service port number
spring.application.name=Ticket-User # Service alias, name registered with the registry: serviceId
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ # The service provider invokes the url address of the service Eureka registry
eureka.client.register-with-eureka=true # Register yourself with the registry
eureka.client.fetch-registry=true # Do you need to retrieve service information from the registry
1.stay SpringCloud There are two ways to invoke a service:Rest Fegin(SpringCloud)
Rest:
- use RestTemplate,yes SpringBoot of web assembly,Default consolidation Ribbon Load Balancer .The bottom layer is adopted HttpClient technology
- establish RestTemplate And mark@Bean Add method creation Http Service to communicate
- RestTemplate There are two ways to call:Call with service alias direct url call
restTemplate.getForObject("providerName(replace IP address)/providerUrl",String.class)
2.Label on main class@EnableEurekaClient(@EnableDiscoveryClient)Annotation open service consumer(Consumer)Discover service features from registry
3.use Rest Calling by alias requires dependency Ribbon Load Balancer ,stay RestTemplate Label on method
@LoadBalanced,Give Way RestTemplate Have the ability to load balance the client at the time of request
- Ribbon load balancing:
- In cluster operations:
- Start the registry first
- When multiple service providers start, they register the current service information in the registry as an alias
- When calling the interface, multiple service consumers use the service alias to obtain the RPC remote call address from the registry
- After obtaining the RPC remote call address, the service consumer first uses the Ribbon load balancer to realize load balancing, and then uses the local HttpClient technology to realize the call
- Basic load balancing strategy: polling mechanism (default)
colony
- The core of microservice RPC remote procedure call protocol: service governance: Registry
- Building a registry cluster: it can solve the problem of unavailability of the whole microservice environment caused by registry failure
- Eureka high availability principle:
- By default, Eureka is the service registration center that allows services to register, and does not register itself
- Eureka high availability is to register itself as a service with other registration centers to form a group of mutually registered service registration centers, so as to realize the mutual synchronization of service lists and achieve the effect of high availability
- Registry cluster:
- During the registration service, only one registration center can be guaranteed to have the corresponding service information data. Only after the registration center is down, can the synchronization data be started to other registration centers
- Profile:
server.port=9000 # Service port number
spring.application.name=euraka # The name of the server on the registry cluster should be consistent
eureka.instance.hostname=127.0.0.1 # Registry IP address
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8761/eureka/ # , url address to register with other registries
eureka.client.register-with-eureka=true # Whether to register yourself in the registry (you need to register when clustering)
eureka.client.fetch-registry=true # Do you need to retrieve service information from the registry
Eureka self-protection mechanism
- Eureka self-protection mechanism: in order to prevent Eureka server from mistakenly rejecting Eureka client service when the network cannot communicate with Eureka server when Eureka client can run normally
1.By default,EurekaClient Terminal timing direction EurekaServer Send heartbeat packet to client
2.If EurekaServer End in<Certain time>Not received within EurekaClient Heartbeat packet sent by client,The service will be directly removed from the service registration list
3.stay<short time>If a large number of service instance heartbeat packets are lost,EurekaServer The client will turn on the self-protection mechanism,Not culled EurekaClient end
- In the local development environment, it is recommended to turn off the EurekaServer self-protection mechanism during testing to ensure that unavailable services are eliminated in time:
configuration file:
EurekaServer end:
eureka.server.enable-self-preservation=false # Enable self-protection mechanism (true by default)
eureka.server.eviction-interval-timer-in-ms=2000 # Elimination interval: 2 seconds
EurekaClient end-Service provider(Provider):
# Heartbeat detection and renewal time. When testing the program, set the value lower to ensure that the registration center will eliminate the service in time after the service is closed
eureka.instance.lease-renewal-interval-in-seconds=1 # The time interval between Eureka client sending heartbeat to Eureka server is seconds
eureka.instance.lease-expiration-duration-in-seconds=2 # The maximum waiting time of EurekaServer after receiving the last heartbeat is seconds. If it exceeds, the service will be rejected
Zookeeper
- Eureka closed source, using Zookeeper instead of Eureka as the registry
- Zookeeper is a distributed coordination tool, which can realize the function of the registry and adopts the temporary node type of zookeeper
- The temporary node is associated with the life cycle. If the service is disconnected, the temporary node will be deleted automatically
configuration file:
ZookeeperClient-Service provider(Provider):
server.port=8090 # Service port number
spring.application.name=zk-ticket # Service alias, the name registered with the registry
spring.cloud.zookeeper.connect-string=127.0.0.1:2181 #The url to register with the zookeeper registry
ZookeeperClient-Service consumers(Consumer):
server.port=8020 # Service port number
spring.application.name=zk-user # Service alias, the name registered with the registry
spring.cloud.zookeeper.connect-string=127.0.0.1:2181 #The url address of the zookeeper registry that invokes the service
1.ZookeeperClient-Service provider(Provider)Label on main class@EnableDiscoveryClient Note register services with the registry
2.ZookeeperClient-Service consumers(Consumer)Label on main class@EnableDiscoveryClient Annotation invokes the service from the registry
3..Mark on the method calling the service@LoadBalanced open Ribbon The load balancing function of is used to call the service
Consul
- Consul is an open source distributed service discovery and configuration management system developed by HashiCorp in Go language
- characteristic:
- Based on raft protocol, it is relatively simple
- Support health check
- Support Http and DNS protocols
- Support WAN clustering across data centers
- Provide graphical interface
- Cross platform
- Consul environment construction:
- Download Consul
- Set environment variable: add the directory where Consul is located
- cmd startup: consumer agent - dev - ui - node = CY (- Dev: development server mode startup - node: node name - ui interface access support, enabled by default)
- Visit Consul: http://localhost:8500
configuration file:
ConsulClient-Service provider(Provider):
server.port=8780 # Service port number
spring.application.name=consul-ticket # Service alias, the name registered with the registry
spring.cloud.consul.host=127.0.0.1 # Consul service url address
spring.cloud.consul.port=8500 # Consul service port number
spring.cloud.consul.discovery.hostname=192.168.66.128 # IP address of the service displayed in the registry (by default, the service is registered in the registry and the address is generated randomly)
ConsulClient-Service consumers(Consumer):
server.port=8099 # Service port number
spring.application.name=consul-user # Service alias, the name registered with the registry
spring.cloud.zookeeper.connect-string=127.0.0.1:2181 #The url address of the service invoke service Consul registry
1.ZookeeperClient-Service provider(Provider)Label on main class@EnableDiscoveryClient Note register services with the registry
2.ZookeeperClient-Service consumers(Consumer)Label on main class@EnableDiscoveryClient Annotation invokes the service from the registry
3..Mark on the method calling the service@LoadBalanced open Ribbon The load balancing function of is used to call the service
DiscoveryClient
- Get service information of the registry
- For local load balancing
@Autowired // automatic assembly
private DiscoveryClient discoveryClient; // The Discovery interface is used to obtain the service information of the registry
@RequestMapping("/discoveryClientMember")
public List<ServiceInstance> discoveryClientMember(){
List<ServiceInstance> instances=discoveryClient.getInstance("consul-ticket");
for(ServiceInstance serviceInstance:instances){
System.out.println("URI:"+serviceInstance.getUri());
}
return instance;
}