In the field of distributed systems, there is a famous CAP theorem (C- data consistency; A- Service availability; Fault tolerance of p-service to network partition failure, these three characteristics cannot be satisfied in any distributed system at the same time, at most two at the same time); Eureka is an AP, and ZooKeeper is a CP. For service discovery, availability is more important than data consistency — AP over CP

Consul zookeeper euerka etcd
Service health check Service status, memory, hard disk, etc (weak) long connection, keepalive Can match support Connect the heart
Multi-data center support
Kv storage service support support support
consistency raft paxos raft
CAP ca cp ap cp
Using interfaces (multilingual capability) Supports HTTP and DNS The client HTTP (sidecars) http/grpc
Watch support (client observes service provider change) Full/Long polling is supported support Long polling/ most increments is supported Support long polling
Their monitoring metrics metrics metrics
security acl /https acl HTTPS support (weak)
Spring cloud integration Have support Have support Have support Have support
  • Raft: Raft relies heavily on the availability of the Leader node to ensure consistency of cluster data.

  • Paxos: The first time the commit Leader sends a prepare message to all the other servers, most of them are ready to accept writes if they reply with a promise promise. The second submitter issues a formal propose VETO to all servers, most of which are considered successful if the reply has been received.

  • Long polling: a long polling process in which the client sends an Ajax request to the server. After receiving the request, the server holds the connection until it receives a new message and closes the connection. After the client processes the response, it sends a new request to the server.

  • Metrics: As a metrics library for monitoring metrics, metrics provides modules for supporting statistics for third-party libraries and applications. Metrics can also be sent to Ganglia and Graphite for graphical monitoring

  • Eureka Server collects statistics on whether the rate of heartbeat failure is lower than 85% within 15 minutes during the Eureka Server running. If the rate is lower than 85% (in the production environment, it is usually caused by network instability), The Eureka Server protects the current instance registration information with a warning. The protection mode is used to protect a group of clients and the Eureka Server when the network is divided. Once in protected mode, Eureka Server will attempt to protect the information in its service registry and will not delete the data in the service registry (that is, will not unregister any microservices). So Eureka’s philosophy is that it’s better to keep both “good data” and “bad data” than to throw away any “good data,” so this model works very well in practice. .

Why not use ZooKeeper for discovery services?

  1. ZooKeeperIs a distributed coordination service, its responsibility is to ensure that data (note: configuration data, state data) under its jurisdiction between all services to maintain synchronization, consistency; (Strong consistency)
  2. At the heart of discovering services should be the need to emphasize the high availability of services
  3. ZooKeeperUse a single master processLeaderUsed to process all transaction requests of the clientZAB agreementBroadcast server count status as a transaction to allFollowerOn; What if two of three services failleader;1 is not greater than 3/2 is equal to 1.
  4. Correct setup and maintenanceThe ZooKeeper serviceIt’s very difficult
  5. A network segmentation fault occurs in the cluster (the subnets under the switch cannot communicate with each other due to a switch fault)ZooKeeperIt takes them all out of its scope, and the outside world can’t access those nodes, which itself are"Health"Can provide services
  6. The discovery service is returnedIs containing false information better than returning nothing at all(Unable to find available server due to temporary network failure)

Therefore, Eureka can cope with the loss of some nodes due to network failure, rather than the whole registration service collapse like ZooKeeper.

Spring Cloud Eureka (Service Registration)

  • Add the dependent
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
Copy the code
  • Enabling Service Registration

The @enableeurekaserver annotation is used to start a service registry for other applications to talk to. This annotation needs to be added to the SpringBoot project startup Application class

    package io.ymq.example.eureka.server;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
        public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Copy the code

Spring Cloud Service Provider

  • Service Provider
  • Register your services in the Eureka registry so that service consumers can find them
  • Add the dependent
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
Copy the code
  • Enabling Service Registration

    In the application main class by adding@EnableEurekaClient, but onlyEurekaYes, you can use it@EnableDiscoveryClient. You need to configure to find itEureka Registry server

    discovery serviceThere are many implementations (Eureka, Consul, zookeeperEtc.)

    @EnableDiscoveryClientBased on thespring-cloud-commons.

    @EnableEurekaClientBased on thespring-cloud-netflix.

    Is if the registry you choose iseureka, then recommend@EnableEurekaClient.

    For other registries, it is recommended@EnableDiscoveryClient.
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaProviderApplication {

    @RequestMapping("/")
    public String home(a) {
        return "Hello world";
    }

	public static void main(String[] args) { SpringApplication.run(EurekaProviderApplication.class, args); }}Copy the code
  • Yml Add Configuration Find Eureka server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: eureka-provider
server:
  port: 8081
Copy the code

Spring Cloud Service Consumer

By registering with Eureka, consumers enable load balancing using the Ribbon

What is the Ribbon?

The Ribbon is an open source project released by Netflix. Its main function is to provide a client-side software load balancing algorithm that connects Netflix’s middle-tier services together. The Ribbon client component provides a comprehensive set of configuration options such as connection timeout and retry. Simply put, the Ribbon lists all the machines behind the Load Balancer in the configuration file. The Ribbon automatically helps you connect to these machines based on certain rules (e.g. simple polling, random linking, etc.). It’s also easy to implement custom load balancing algorithms using the Ribbon.

The core components of the Ribbon

The Ribbon uses ServerList to get a list of all available services. The Ribbon uses ServerListFilter to filter some addresses, and IRule to select a server from the remaining addresses.

  • ServerList: Used to obtain the address list. It can be static (providing a fixed set of addresses) or dynamic (periodically querying the address list from the registry).
  • ServerListFilter: Only when dynamic is usedServerListIs used to exclude some IP addresses in the original service list.
  • IRule: Select a final service address asResults LB. The selection strategies arePolling, weighted by response time, circuit breakers (when Hystrix is available)And so on.

The main load balancing policies provided by the Ribbon

  • Simple round load balancing (RoundRobin) : allocates requests to different servers in RoundRobin mode. That is, perform I = (I + 1) mod n each time and select the ith server.
  • Random load balancing: Servers in the UP state are randomly selected
  • WeightedResponseTime: A weight is assigned according to the corresponding time, and the longer the corresponding time, the smaller the weight and the less likely it is to be selected.
  • Zone aware Polling load Balancing: Select a Server based on the performance of the zone where the Server is located and the availability of the server

Service Provider (providing services)

  • Enable service registration register three units
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaProviderApplication {
    @Value("${server.port}")
    String port;
    @RequestMapping("/")
    public String home(a) {
        return "Hello world ,port:" + port;
    }
    public static void main(String[] args) { SpringApplication.run(EurekaProviderApplication.class, args); }}Copy the code
  • Add config application. Yml port 8081,8082,8083
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: eureka-provider

server:
  port: 8081
Copy the code

Service consumer (dependent on other services)

  • Pom.xml adds dependencies
<! -- Client load balancing -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

<! -- Eureka client -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
Copy the code
  • Enable SERVICE load balancing. Register with the service registry by @enableDiscoveryClient. And inject a bean: restTemplate into the program’s IOC and indicate that this restRemplate turns on load balancing via the @loadBalanced annotation
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {
    @LoadBalanced
    @Bean
    RestTemplate restTemplate(a) {
        return new RestTemplate();
    }
	public static void main(String[] args) { SpringApplication.run(RibbonConsumerApplication.class, args); }}Copy the code
  • Consuming provider approach
/** * call the provider's 'home' method **/
@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/hello")
    public String hello(a) {
        return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody(); }}Copy the code
  • Yml specifies the registry address of the service, and configures its own service port and service name
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: ribbon-consumer

server:
  port: 9000
Copy the code

Start services in sequence(Eureka service, three Service Providers and Service Consumers)To see theribbonWhether to enable load balancing

Spring Cloud Consul (Service Governance implementation for Consul)

Consul provides its own server, so we don’t need to create a service registry like we did when implementing Eureka. Instead, we can download Consul’s server application and use it. Consul comes with a built-in service registration and Discovery framework (one-stop) with the following attributes (see list above) :

  • Distributed consistency protocol implementation
  • Health check
  • The Key/Value store
  • Multi-data center solution

The advantage of the Consul

  • Use the Raft algorithm to ensure consistency, which is more straightforward than the complex Paxos algorithm
  • Supports multiple DCS, and uses different ports for listening on Intranet and extranet services
  • Multi-dc cluster can avoid single point of failure of a single DC, and its deployment needs to consider network latency and fragmentation
  • Support health check
  • Supports HTTP and DNS interfaces
  • The official web management interface is provided

The role of the Consul

  • Client: a stateless client that forwards HTTP and DNS interface requests to the server cluster on the LAN. All services registered with the current node are forwarded to the server, which does not persist the information
  • Server: server, save configuration information, high availability cluster, in the LOCAL area network and local client communication, function and client are the same, the only difference is that it will put all the information persistent local, so encounter a fault, information can be retained. Communicate with other data centers over the wan. The recommended number of servers for each data center is 3 or 5.
  • Server-leader: indicates that the server is the leader of the node. Different from other servers, it is responsible for synchronizing the registered information to other servers and monitoring the health of each node.
  • Raft: Ensures data consistency between server nodes. Raft is used as the consistency protocol, Paxos is used by ZooKeeper and Taft is used by ETCD.
  • Service discovery protocol: Consul uses HTTP and DNS, and ETCD supports only HTTP
  • Service registration: Two methods are supported for service registration, consul official recommends using the second method.
  1. One is to register via Consul’s service HTTP API, which the service calls itself,
  2. The other way is to register the services in JSON configuration files.
  • Service discovery: Supports two ways to implement service discovery,
  1. One is to query what services are available through an HTTP API,
  2. The other option is to use the DNS on Consul Agent (port 8600). The domain NAME is in the format of name.service. consul. NAME is the service NAME in the defined service profile. DNS You can check services by using check.
  • Communication protocol between services: Consul uses the Gossip protocol to manage membership and broadcast messages to the entire cluster

conclusion

There is a full Spring Cloud deployment on Github. Other related articles Spring Cloud (1)- Introduction and options Spring Cloud (2)- Service Discovery (Eureka, Consul) Spring Cloud (3)- Load Balancing (Feign, Ribbon Spring Cloud (4)- Hystrix Spring Cloud (5)- Zuul Spring Cloud (6)- Configuration Management and Refresh (Config, Bus) Give a star ~ personal blog ~ Jane book ~