This is the 10th day of my participation in Gwen Challenge

A, had been

1, an overview of the

  • Service governance: In traditional GRP remote invocation, the relationship between services is complex and management is complex. Therefore, service governance is used to manage the dependency relationship between services, which can realize service invocation, load balancing, fault tolerance, service discovery and registration.
  • Service Registration: Eureka is C/S architecture design, the registry as service side, producers and consumers, as the client and the server to keep the heart connection, the server is started, the registry information registered in the form of an alias to the registry, consumers or producers to the alias registry access to the actual service communication address, The local RPC remote call is then implemented.

Two components

  • Eureka Server: Provides service registration
  • Eureka Client: The Eureka Client is accessed through the registry. After the application is started, the Eureka Client sends heartbeat messages to the Server. If the Server does not receive a heartbeat from a node in multiple heartbeat cycles, the Server removes the node (default: 90 seconds).

2. Environment construction

Registry Server side

Rely on

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Copy the code

configuration

server:
  port: 7001
eureka:
  instance:
    # Eureka registry instance name
    hostname: localhost
  client:
    # Don't register yourself
    register-with-eureka: false
    # Don't search for services
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code

Server primary startup class

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class, args); }}Copy the code

Registry Client

Rely on

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code

The Client configuration

server:
  port: 8001

spring:
  application:
    Identify the name of the current service in the registry
    name: cloud-payment-service


eureka:
  instance:
    Change the host name
    prefer-ip-address: true
    instance-id: payment8001
  client:
    # Register to the registry
    register-with-eureka: true
    Fetch your own registration information from the server
    fetch-registry: true
    service-url:
      Address of the registered server
      defaultZone: http://localhost:7001/eureka
Copy the code

The Client main start

@EnableEurekaClient
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class, args); }}Copy the code

Registry cluster

To avoid a host of problems caused by a registry outage, change the registry to cluster mode. The load balancing mode is enabled.

In the Server configuration, multiple registries are set to register with each other

eureka:
  instance:
    # Eureka registry instance name
    hostname: localhost
  client:
    # Don't register yourself
    register-with-eureka: false
    # Don't search for services
    fetch-registry: false
    service-url:
      # Set up a cluster of registries, multiple registries watch out for each other, other registries also register the current registry
      defaultZone: http://xxx-PC:7002/eureka/
Copy the code

The client is configured to register with multiple registries simultaneously

client:
  # Register to the registry
  register-with-eureka: true
  Fetch your own registration information from the server
  fetch-registry: true
  service-url:
    Address of the registered server
    defaultZone: http://localhost:7001/eureka,http://xxx-PC:7001/eureka
Copy the code

Configure the Configuration class to set up remote calls

@SpringBootConfiguration
public class ApplicationConfig {
    @Bean
    @LoadBalanced // Enable load balancing
    public RestTemplate getRestTemplate(a){
        return newRestTemplate(); }}Copy the code

The registry allows multiple consumers to invoke other service providing modules in their registry with load balancing capabilities.

Second, the Zookeeper

Import dependencies and eliminate dependency conflicts

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </exclusion>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>

        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.9</version>
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
Copy the code

configuration

server:
  port: 8080


spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      connect-string: localhost:2181
Copy the code

Create producer and consumer, register with ZooKeeper, annotate @enableDiscoveryClient with the main startup class. Remote call using RestTemplate. The implementation calls the provider through the consumer.

Third, Consul

  • Install and start Consul
  • Default access addresshttp://localhost:8500/

Rely on

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
Copy the code

configuration

server:
  port: 8080
spring:
  application:
    name: cloud-cousumer-payment
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
Copy the code

4. Differences between the three registries

  • CAP theory (for data) :
    • C: Strong consistency
    • A: Availability
    • P: fault tolerance of partitions
  • Eureka works in AP mode. If the service is temporarily unavailable and the heartbeat is not sent for a period of time, Eureka will not delete the service but temporarily retain it.
  • Consul\ ZooKeeper is in CP mode, which emphasizes data consistency and deletes a service from the registry immediately if it becomes unavailable.