Spring cloud builds microservice cluster + Zuul server-side load balancing

Keywords: Java Spring SpringBoot

  • Summary
    Recently, I studied the microservice cluster of springcloud, mainly using the service discovery and server-side load balancing of springcloud. All projects use springboot,
    It can be seamlessly docked with spring cloud.

  • Technology selection

    Service discovery: eureka
    Load balancing: zuul

  • Realization
    Eureka cluster (eurekaServer): 8001, 8002, 8003
    eurekaClient: 9001, 9002
    Server load balancing service: 9100

    1. Create a new project eureka server, create a springboot project with idea, and introduce eureka dependency

    Here are the configuration files for the three servers
    //First stage
    server:
      port: 8001
    
    spring:
      application:
        name: eureka-server
    
    eureka:
      server:
        use-read-only-response-cache: false  #Disable readonlymap, divided into ReadWriteMap and readonlymap
        eviction-interval-timer-in-ms: 3000  #Active failure detection time
      instance:
        hostname: server8001
      client:
    #    register-with-eureka: false  #false means not to register yourself as a service to eureka
    #    fetch-registry: false
        service-url:
          #Cluster setting, set up eureka cluster, separated by commas
          defaultZone: http://localhost:8002/eureka/,http://localhost:8003/eureka/
    
    //Second stations
    server:
      port: 8002
    
    spring:
      application:
        name: eureka-server
    
    eureka:
      server:
        use-read-only-response-cache: false
        eviction-interval-timer-in-ms: 3000
      instance:
        hostname: server8002
      client:
    #    register-with-eureka: false #Indicates that you do not register yourself as a service in eureka
    #    fetch-registry: false
        service-url:
          #Set up the eureka cluster, separated by commas. defaultZone is of map type, so there is no prompt
          defaultZone: http://localhost:8001/eureka/,http://localhost:8003/eureka/
    
    //Third stations
    server:
      port: 8003
    
    spring:
      application:
        name: eureka-server
    
    eureka:
      server:
        use-read-only-response-cache: false
        eviction-interval-timer-in-ms: 3000
      instance:
        hostname: server8003
      client:
    #    register-with-eureka: false #Indicates that you do not register yourself as a service in eureka
    #    fetch-registry: false
        service-url:
          #Set up the eureka cluster, separated by commas. defaultZone is of map type, so there is no prompt
          defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/
    
    Add EnableEurekaServer annotation at the entry

     

     
    2. Service provider
    //First stage
    server:
      port: 9001
    
    spring:
      application:
        name: eureka-client
    
    eureka:
      client:
        service-url:
          # Specify cluster server list
          defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/,http://localhost:8003/eureka/
      instance:
        instance-id: client9001.com
        prefer-ip-address: true
        lease-renewal-interval-in-seconds: 5       #Send heartbeat package service refresh time
        lease-expiration-duration-in-seconds: 15  #Service expiration time
    
    //Second stations
    server:
      port: 9002
    spring:
      application:
        name: eureka-client
    
    eureka:
      client:
        service-url:
          # Specify cluster server list
          defaultZone: http://localhost:8001/eureka/
      instance:
        instance-id: client9002.com
        prefer-ip-address: true
        lease-renewal-interval-in-seconds: 5       #Send heartbeat package service refresh time
        lease-expiration-duration-in-seconds: 15  #Service expiration time
    

    Add EnableEurekaClient annotation and api business test interface

     

     
    3. zuul load balancing service

    server:
      port: 9100
    
    spring:
      application:
        name: zuul-gateway
    
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8001/eureka,http://localhost:8002/eureka,http://localhost:8003/eureka
        registry-fetch-interval-seconds: 5  #Pull service list time
      instance:
        instance-id: gateway-9100.com
        prefer-ip-address: true
    
    zuul:
      prefix: /gateway
      ignored-services: eureka-client
      routes:
        eureka-client.serviceId: eureka-client
        eureka-client.path: /client/**
    

    Start Eureka server, service provider client, and zull service in turn. The console to access one of the Eureka servers is shown as follows

     

     


     

    Question:
    1) when configuring eureka, the defaultZone is a map type and does not provide detection. Therefore, it will be found that there is no prompt when configuring, and you can write directly;
    2) the function of the host name of eureka server is not clear at present. The application name is displayed on the console. The application.name of three eureka clusters and two service providing clients are the same;
    3) the service list of eureka server is divided into ReadWriteMap and ReadOnlyMap. Other services are read from ReadOnlyMap. When there is service registration, the service list is written to ReadWriteMap, and then it will be synchronized to
    ReadOnlyMap, this is to solve the problem of read-write lock. When a service provider application goes down, it will be found that the console still has the information of this server, indicating that the service is not offline, and the service list does not delete this server
    The information of the server causes zuul to call the down application sometimes during load balancing, resulting in an exception. The solution is to disable the ReadOnlyMap of the eureka server, set the active failure detection time, and make the server timed
    To detect the service, the service provider sets the service expiration time and the time to send heartbeat packets, and zuul sets the time interval to pull the service list
    4) zuul routing setting, setting the matching relationship between path prefix and service

  • test

     

     

     

    Keep refreshing the page, it will switch between 9001 and 9002, which proves that the configuration is successful, close 9002 application, refresh the page, and there will be access failure at the beginning, and it will be stable after a period of time,
    There will be a time difference. You can also configure a fuse for zuul to perform service degradation or forward requests to other normal services.

Posted by philippo on Sun, 08 Dec 2019 22:25:01 -0800