Registration and discovery of Spring Cloud services Eureka

Spring Cloud

Service registration and discovery

Use of Eureka Server

  1. add dependencies
  2. The startup class is added to @EnableEurekaServer
  3. Add the following to the configuration file

    server
        port: 8761
    
    eureka:
        client:
            registerWithEureka: false
            fetchRegistry: false
            serviceUrl:
                defaultZone: http://localhost:8761/eureka/

    Configuration instructions

    • registerWithEureka: Whether to register itself to the server, the default is true. The current service does not need to register itself to itself.
    • fetchRegistry: Whether to obtain registration information from Eureka Server, the default is true. It is currently a single-point Eureka Server and does not need to synchronize other nodes.
    • defaultZone: Set the address for interacting with Eureka Server. The query and registration services rely on this address. Multiple addresses are separated by commas.
  4. Startup project. Visit http://localhost:8761/ to see the list of registered services

Use of Eureka Client

  1. add dependencies
  2. Add the following to the configuration file

    spring:
        application:
            name: xxxxxx
    
    euraka:
        client:
            serverUrl: http://localhost:8761/eureka/
        instance:
            prefer-ip-address: true 

    Configuration instructions

    • name: the name registered to the Eureka Server
    • prefer-ip-address: Whether to register your own IP with Eureka Server. Default false, use the hostname of the operating system to register
  3. Startup class adding @EnableDiscoveryClient(discover: discover) or @EnableEurekaClient

    illustrate:

    • @EnableDiscoveryClient is an abstract interface that can be used with other service components. Such as: Zookeeper, Consul, etc.
    • @EnableEurekaClient is an Eureka-specific annotation

Eureka Server cluster

  • Eureka Server can implement a cluster by running multiple instances to register with each other.

    Modify the configuration file:

    spring:
        application:
    
            name: eureka-server-cluster
    ---
    
    spring:
        profiles: cluster1
    server:
        post: 8761
    eureka:
        instance:
            hostname: cluster1
        client:
            serverUrl:
    
                defaultZone: http://localhost:8762/eureka/
    ---
    
    spring:
        profiles: cluster2
    server:
        port: 8762
    erueka:
        instance:
            hostname: cluster2
        client:
            serverUrl:
                defaultZone: http://localhost:8761/eureka/

    Description: Use a hyphen (-) to divide application.yml into three parts. The first part does not declare that profiles are globally effective.

  • Run the program.

    java -jar eureka-server-cluster.jar –spring.profiles.active=clusert1
    java -jar eureka-server-cluster.jar –spring.profiles.active=clusert2

Eureka Client register cluster

Even if Client registers a single node, Eureka Server can synchronize with each other. It is recommended to configure multiple nodes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324643387&siteId=291194637