Service governance under microservice architecture: how to realize service registration and discovery in spring cloud framework

Time:2022-1-7

Service governance

  • The core design idea of RPC remote procedure call protocol:lie inRegistry,becauseRegistry: manages a dependency between each service and the service
  • 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

    • SpringCloudThree types of book centers are supported:

      • Consul (go language)
      • Eureka
      • Zookeeper
    • DubboTwo types of registries are supported:

      • Zookeeper
      • Redis
  • Registry concept:Store service address related information (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 (registration is required for clustering)
    eureka. client. Fetch registry = false # do you need to retrieve service information from the registry
  • Registry project:

    1. Mark the @ enableeurekaserver annotation on the main class to start the eurekaserver service and the registry

    Service registration

  • Register service information with the registry
  • Profile:

    server. Port = 8001 # service port number of service provider
    spring. application. Name = ticket service # service alias, name registered in 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 # do you want to register yourself with the registry
    eureka. client. Fetch registry = true # do you need to retrieve service information from the registry
  • Service provider project:

    1. Mark the @ enableeurekaclient annotation on the main class to register the service provider service in the registry

    Service discovery

  • Get service information from the registry
  • Profile:

    server. Port = 8200 # service port number of service consumer
    spring. application. Name = ticket user # service alias, name registered in 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 # do you want to register yourself with the registry
    eureka. client. Fetch registry = true # do you need to retrieve service information from the registry
  • Service consumer items:

    1. There are two ways to call services in spring cloud: rest fegin (spring cloud)
  • Resttemplate is the web component of springboot and integrates ribbon load balancer by default The bottom layer is the httpclient technology
  • Create resttemplate and label @ bean. Add method to create HTTP service for communication
  • There are two ways to call resttemplate: using service alias call and direct URL call
    restTemplate. Getforobject (“providername (alternate IP address) / providerurl”, string class)
    2. Mark @ enableeurekaclient (@ enablediscoveryclient) on the main class to enable the service consumer to discover services from the registry
    3. To call by alias using rest, you need to rely on the ribbon load balancer and mark it on the resttemplate method
    @Loadbalanced, which enables resttemplate to have the load balancing capability of the client when requesting

  • 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 multiple service consumers call the interface, they use the service alias to obtain the RPC remote call address from the registry
      • After the service consumer obtains the RPC remote call address,First use the ribbon load balancer to achieve load balancingThen use the local httpclient technology to realize the call
    • Basic load balancing strategy:Polling mechanism (default)

      colony

  • Design of RPC remote procedure call protocol for microservicecore:Service governance: Registry
  • buildRegistry cluster:It can solve the problem that the entire microservice environment is unavailable due to registry failure
  • Eureka high availability principle:

    • By default, Eureka is the service registry that allows services to register, and does not register itself
    • Eureka high availability is to register itself with other registries as a service,Form a groupMutual registrationService registry,Realize the mutual synchronization of service lists,Achieve 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 = the name of the server on the euraka # 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 with the registry (registration is required for clustering)
      eureka. client. Fetch registry = true # do you need to retrieve service information from the registry

      Eureka self-protection mechanism

  • Eureka self-protection mechanism:To preventWhen eurekaclient can run normally, when the network cannot communicate with eurekaserver, eurekaserver mistakenly rejects Eureka client service

    1. By default, Eureka client sends heartbeat packets to Eureka server regularly
    2. If eurekaserver does not receive the heartbeat packet sent by Eureka client within < a certain time >, the service will be directly removed from the service registration list
    3. If a large number of heartbeat packets of service instances are lost in < short time >, eurekaserver will start the self-protection mechanism and will not eliminate eurekaclient
  • 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:

    Profile:
    Eurekaserver side:
    eureka. server. Enable self preservation = false # whether to enable self-protection mechanism (true by default) 
    eureka. server. Occurrence interval timer in MS = 2000 # rejection interval: 2 seconds
    
    Eurekaclient - service 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 # time interval for Eureka client to send heartbeat to Eureka server in seconds
    eureka. instance. Lease expiration duration in seconds = 2 # eurekaserver side waits for seconds after receiving the last heartbeat. 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 registryTemporary nodetype
  • The temporary node is associated with the life cycle. If the service is disconnected, the temporary node will be deleted automatically

    Profile:
    Zookeeperclient - service 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 # register the URL to the zookeeper registry 
    
    Zookeeperclient - service 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 calls the service
    
    
    1. Zookeeperclient - the service provider marks the @ enablediscoveryclient annotation on the main class to register the service with the registry
    2. Zookeeperclient - the service consumer marks the @ enablediscoveryclient annotation on the main class and calls the service from the registry
    3.. Mark @ loadbalanced on the method of calling the service. Turn on the load balancing function of the ribbon 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
    • Setting environment variables: add the directory where consul is located
    • CMD start: 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

      Profile:
      Consulclient - service provider:
      server. Port = 8780 # service port number
      spring. application. Name = Consumer Ticket # service alias, the name registered with the registry
      spring. cloud. consul. Host = 127.0.0.1 # consult service URL address
      spring. cloud. consul. Port = 8500 # consult service port number
      spring. cloud. consul. discovery. Hostname = 192.168.66.128 # service IP address displayed in the registry (by default, the service is registered in the registry and the address is generated randomly)
      
      Consumclient - service consumer:
      server. Port = 8099 # service port number
      spring. application. Name = consumer user # service alias, the name registered with the registry
      spring. cloud. zookeeper. Connect string = 127.0.0.1: 2181 # service invokes the URL address of the consul registry of the service
      
      
      1. Zookeeperclient - the service provider marks the @ enablediscoveryclient annotation on the main class to register the service with the registry
      2. Zookeeperclient - the service consumer marks the @ enablediscoveryclient annotation on the main class and calls the service from the registry
      3.. Mark @ loadbalanced on the method of calling the service. Turn on the load balancing function of the ribbon to call the service

      DiscoveryClient

  • Get service information of the registry
  • For local load balancing

    @Autowired // auto 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;
    }

Recommended Today

Proper memory alignment in go language

problem type Part1 struct { a bool b int32 c int8 d int64 e byte } Before we start, I want you to calculatePart1What is the total occupancy size? func main() { fmt.Printf(“bool size: %d\n”, unsafe.Sizeof(bool(true))) fmt.Printf(“int32 size: %d\n”, unsafe.Sizeof(int32(0))) fmt.Printf(“int8 size: %d\n”, unsafe.Sizeof(int8(0))) fmt.Printf(“int64 size: %d\n”, unsafe.Sizeof(int64(0))) fmt.Printf(“byte size: %d\n”, unsafe.Sizeof(byte(0))) fmt.Printf(“string size: %d\n”, […]