02_Eureka service registration and discovery, Zookeeper service registration and discovery

3. Eureka service registration and discovery

  • Global brain map structure

3.1 Eureka Basics

3.1.1 what is service governance

What is service governance

  • Spring Cloud encapsulates the Eureka module developed by Netflix to realize service governance

  • In the traditional rpc remote invocation framework, it is complex to manage the dependency between each service and service, so it is necessary to use service governance to manage the dependency between services, which can realize service invocation, load balancing, fault tolerance, service discovery and registration.

3.1.2 what is service registration

What is service registration and discovery

  • Eureka adopts the design architecture of CS. Eureka Server is the server of service registration function, and it is the service registration center. Other microservices in the system use Eureka's client to connect to Eureka Server and maintain heartbeat connection. In this way, the maintenance personnel of the system can monitor whether each micro service in the system is running normally through Eureka Server.
  • In service registration and discovery, there is a registry. When the server starts, it will register its current server information, such as service address and communication address, in the registration center in the form of alias. The other party (consumer | service provider) obtains the actual service communication address from the registry in the way of this alias, and then realizes the local RPC call. The core design idea of RPC remote call framework lies in the registry, because the registry is used to manage a dependency between each service and service (service governance concept). In any RPC remote framework, there will be a registry (storing information related to the service address (interface address))

3.1.3 Eureka two components

  • Eureka consists of two components: Eureka Server and Eureka Client

  • Eureka Server provides service registration services
    After each micro service node is started through configuration, it will be registered in EurekaServer. In this way, the service registry in EurekaServer will store the information of all available service nodes, and the information of service nodes can be seen intuitively in the interface.

  • EurekaClient is accessed through the registry
    It is a Java client to simplify the interaction of Eureka Server. The client also has a built-in load balancer using round robin load algorithm. After the application starts, a heartbeat will be sent to Eureka Server (the default cycle is 30 seconds). If Eureka Server does not receive the heartbeat of a node in multiple heartbeat cycles, Eureka Server will remove the service node from the service registry (90 seconds by default)

3.2 construction steps of single machine Eureka

3.2.1 IDEA generates Eureka server-side service registry

  1. Build Module
    cloud-eureka-server7001
  2. Change POM
<!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

  1. Write YML
server:
  port: 7001


eureka:
  instance:
    hostname: eureka7001.com #Instance name of eureka server
  client:
    register-with-eureka: false     #false means that you do not register yourself with the registry.
    fetch-registry: false     #false means that my client is the registry. My responsibility is to maintain service instances and I don't need to retrieve services
    service-url:
    #Cluster points to other eureka
      #defaultZone: http://eureka7002.com:7002/eureka/
    #A single machine is 7001 itself
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  #server:
    #Close the self-protection mechanism to ensure that unavailable services are kicked out in time
    #enable-self-preservation: false
    #eviction-interval-timer-in-ms: 2000
  1. Main start
    @EnableEurekaServer
  2. test
    ···http://localhost:7001/
    ···Results page

3.2.2 Eureka client cloud provider payment8001

Register with EurekaServer to become a service provider, which is similar to the teaching service provided by Silicon Valley schools

  1. cloud-provider-payment8001
  2. Change POM
  3. Write YML
server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  zipkin:
      base-url: http://localhost:9411
  sleuth:
    sampler:
    #The sample rate value is between 0 and 1, and 1 means all collection
    probability: 1
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # Current data source operation type
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql driver package
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 333


eureka:
  client:
    #Indicates whether to register yourself with EurekaServer. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      #Stand alone version
      defaultZone: http://localhost:7001/eureka
      # Cluster version
      #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
      instance-id: payment8001
      #The access path can display the IP address
      prefer-ip-address: true
      #The time interval between Eureka client sending heartbeat to the server, in seconds (30 seconds by default)
      #lease-renewal-interval-in-seconds: 1
      #The upper limit of waiting time of Eureka server after receiving the last heartbeat, in seconds (90 seconds by default), and the service will be rejected when it times out
      #lease-expiration-duration-in-seconds: 2


mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities    # Package of all Entity alias classes




  1. Main start
  • @EnableEurekaClient
  1. test
    ···Start EurekaServer first
    ···http://localhost:7001/

    ···Microservice registration name configuration description

3.2.3 cloud-consumer-order80 of Eureka client

You will register with EurekaServer to become a consumer of services, similar to the students who come to Silicon Valley for class and consumption

  1. cloud-consumer-order80
  2. POM
  3. YML
server:
  port: 80

spring:
    application:
        name: cloud-order-service
    zipkin:
      base-url: http://localhost:9411
    sleuth:
      sampler:
        probability: 1

eureka:
  client:
    #Indicates whether to register yourself with EurekaServer. The default value is true.
    register-with-eureka: false
    #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      #stand-alone
      #defaultZone: http://localhost:7001/eureka
      # colony
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   #Cluster version

  1. Main start
    @EnableEurekaClient
  2. test
    ···Start EurekaServer 7001 service first
    ···To start the service provider again, the 8001 service
    ···eureka server

    ···http://localhost/consumer/payment/get/31

3.3 Eureka cluster construction steps

3.3.1 Eureka cluster principle description


Question: what is the core of microservice RPC remote service invocation
High availability. Imagine that your registry has only one only one. If it fails, ha ha ( ̄▽  ̄) "will lead to the unavailability of the whole service environment, so

Solution: build Eureka registry cluster to realize load balancing + fault tolerance

3.3.2 steps to build eurekaserver cluster environment

  1. Refer to cloud Eureka server7001

  2. Create a new cloud Eureka server7002

  3. Change POM

  4. Modify mapping configuration
    ···Locate the hosts file in the path C:\Windows\System32\drivers\etc

    ···Modify the mapping configuration and add it to the hosts file
    ······127.0.0.1 eureka7001.com
    ······ 127.0.0.1 eureka7002.com

  5. Write YML (previously stand-alone)
    ···7001

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com #Instance name of eureka server
  client:
    register-with-eureka: false     #false means that you do not register yourself with the registry.
    fetch-registry: false     #false means that my client is the registry. My responsibility is to maintain service instances and I don't need to retrieve services
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/

···7002

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com #Instance name of eureka server
  client:
    register-with-eureka: false     #false means that you do not register yourself with the registry.
    fetch-registry: false     #false means that my client is the registry. My responsibility is to maintain service instances and I don't need to retrieve services
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

3.3.3 publish the payment service 8001 micro service to the above two Eureka cluster configurations

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # Current data source operation type
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql driver package
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

eureka:
  client:
    #Indicates whether to register yourself with EurekaServer. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer is true by default. It doesn't matter if a single node is used. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   #Cluster version

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities    # Package of all Entity alias classes

3.3.4 publish the order service 80 micro service to the above two Eureka cluster configurations

server:
  port: 80

spring:
    application:
        name: cloud-order-service

eureka:
  client:
    #Indicates whether to register yourself with EurekaServer. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer is true by default. It doesn't matter if a single node is used. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   #Cluster version

3.3.5 construction of 8001 cluster environment for payment service providers

  1. Refer to cloud provider payment8001
  2. Create a new cloud provider payment8002
  3. Change POM
  4. Write YML
server:
  port: 8002

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # Current data source operation type
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql driver package
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
    
eureka:
  client:
    #Indicates whether to register yourself with EurekaServer. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   #Cluster version
      #defaultZone: http://localhost:7001/eureka # standalone

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities    # Package of all Entity alias classes

  1. Main start
    ···@SpringBootApplication
    ···@EnableEurekaClient
  2. Business class
  3. Modify the Controller of 8001 / 8002

3.3.6 load balancing

  1. Use the @ LoadBalanced annotation to give RestTemplate the ability of load balancing
  2. ApplicationContextBean

3.4 improvement of actor microservice information

3.4.1 host name: service name modification

  1. Current issues


2. Modify cloud provider payment8001

eureka:
  client:
    #Indicates whether to register yourself with EurekaServer. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   #Cluster version
      #defaultZone: http://localhost:7001/eureka # standalone
  instance:
    instance-id: payment8001
  1. After modification

3.4.2 IP information prompt in access information

  1. Current issues
    No IP prompt
  2. Modify cloud provider payment8001
eureka:
  client:
    #Indicates whether to register yourself with EurekaServer. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka   #Cluster version
      #defaultZone: http://localhost:7001/eureka # standalone
  instance:
    instance-id: payment8001
    prefer-ip-address: true     #The access path can display the IP address

3.5 service Discovery

3.5.1 modify the Controller of cloud provider payment8001

	@Resource
	private DiscoveryClient discoveryClient;
	@GetMapping(value = "/payment/discovery")
	    public Object discovery()
	    {
	        List<String> services = discoveryClient.getServices();
	        for (String element : services) {
	            System.out.println(element);
	        }
	
	        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
	        for (ServiceInstance element : instances) {
	            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
	                    + element.getUri());
	        }
	        return this.discoveryClient;
	    }

3.5.2 8001 main startup

  • @EnableDiscoveryClient

3.6 Eureka self protection

3.6.1 fault phenomenon

summary

  • The protection mode is mainly used to protect a group of clients and Eureka Server in the scenario of network partition. Once in protected mode,
    Eureka Server will try to protect the information in its service registry and will not delete the data in the service registry, that is, it will not log off any micro services.

  • If you see the following prompt on the homepage of Eureka Server, Eureka has entered the protection mode:
    EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT.
    RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE

3.6.2 causes

Why Eureka self-protection mechanism?

  • In order to prevent Eureka client from running normally, but not connected to Eureka server, Eureka server will not remove Eureka client service immediately

What is the self-protection model?

  • By default, if EurekaServer does not receive the heartbeat of a micro service instance within a certain period of time, EurekaServer will log off the instance (90 seconds by default). However, when the network partition fails (delay, jamming, congestion), the micro service and EurekaServer cannot communicate normally, and the above behavior may become very dangerous - because the micro service itself is actually healthy, it should not be cancelled at this time. Eureka solves this problem through "self-protection mode" - when Eureka server node loses too many clients in a short time (network partition failure may occur), the node will enter self-protection mode.

  • In self-protection mode, Eureka Server will protect the information in the service registry and will not unregister any service instances.
    Its design philosophy is to retain the wrong service registration information rather than blindly cancel any possible healthy service instances. In one sentence: it's better to live than to die

  • To sum up, self-protection mode is a security protection measure to deal with network abnormalities. Its architectural philosophy is that it would rather retain all micro services at the same time (both healthy and unhealthy micro services will be retained) than blindly cancel any healthy micro services. Using self-protection mode can make Eureka cluster more robust and stable.

3.6.3 how to prohibit self-protection

  1. Registration Center eurakeserver 7001
  • Use eureka.server.enable-self-preservation = false to disable self-protection mode
server:
  port: 7001
spring:
  application:
    name: eureka-cluster-server
eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      #defaultZone: http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
      defaultZone: http://eureka7001.com:7001/eureka
  server:
    #Close the self-protection mechanism to ensure that unavailable services are kicked out in time
       enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

4. Zookeeper service registration and discovery

4.1 what do you do when Eureka stops updating

https://github.com/Netflix/eureka/wiki

4.2 spring cloud integrates Zookeeper instead of Eureka

4.2.1 registration center Zookeeper

  1. zookeeper is a distributed coordination tool that implements registry functions
  2. Turn off the Linux server firewall and start the zookeeper server
  3. The zookeeper server replaces the Eureka server and zk serves as the service registry

4.3.2 service providers

  1. Create a new cloud provider payment8004
  2. POM
<!-- SpringBoot integration zookeeper client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
  1. YML
#8004 indicates the port number of the payment service provider registered with the zookeeper server
server:
  port: 8004
#Service alias -- register zookeeper to the registry name
spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: 192.168.111.144:2181

  1. Main startup class
  • @EnableDiscoveryClient
    //This annotation is used to register services when using consumer or zookeeper as the registry
  1. Controller
  2. Start 8004 to register with zookeeper
  • Resolve the jar package conflict of zookeeper version
  • Exclude zk conflicting new POM S
<!-- SpringBoot integration zookeeper client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--Exclude your own first zookeeper3.5.3-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--add to zookeeper3.4.9 edition-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
        </dependency>


Is the service node a temporary node or a persistent node
Temporary node

4.3.3 service consumers

  1. Create a new cloud-consumer zk-order80
  2. POM
<!-- SpringBoot integration zookeeper client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--Exclude your own first zookeeper-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--add to zookeeper3.4.9 edition-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
        </dependency>

  1. YML
server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
  cloud:
  #Register to zookeeper address
    zookeeper:
      connect-string: 192.168.111.144:2181
  1. Main start
  2. Business class
    ···Configure Bean
	@Configuration
	public class ApplicationContextBean
	{
	    @Bean
	    @LoadBalanced
	    public RestTemplate getRestTemplate()
	    {
	        return new RestTemplate();
	    }
	}

···Controller

  1. Verification test

  2. Access test address

  • http://localhost/consumer/payment/zk

Tags: Zookeeper RESTful eureka microservice

Posted on Wed, 20 Oct 2021 00:50:41 -0400 by forcer