Moment For Technology

SpringCloud Eureka tutorial

Posted on Sept. 23, 2022, 2:40 a.m. by Barry Harris
Category: The back-end Tag: Spring Cloud

Eureka Server configuration

Import dependence

Copy the code

Add the @enableeurekaserver annotation to the startup class

@EnableEurekaServer// Add this annotation
public class EurekaServerApplication {

    public static void main(String[] args) {, args); }}Copy the code

The configuration application. Yml

  port: 8080
    hostname: localhost
    register-with-eureka: false
    fetch-registry: false
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code
  • eureka.instance.hostnameSpecify the IP address and domain name of the Eureka server.
  • eureka.client.register-with-eurekaWhether to register the service with the Eureka server
  • eureka.client.fetch-registryWhether to obtain service information from Eureka server
  • eureka.client.serviceUrl.defaultZoneSpecify the address of the Eureka server

Eureka.client. register-with-Eureka and Eureka.client.fetch-registry are the key to distinguish Eureka Server from Eureka client. For Eureka Server, these two values are usually set to false. This varies depending on the cluster, as described below.

The effect

Access localhost:8080, noticeeureka.client.serviceUrl.defaultZoneThe client registry address is configured, not the console address.

Eureka Client configuration

Import dependence

Copy the code

Add the @enableeurekaclient annotation to the startup class

@EnableEurekaClient// Add this annotation
public class EurekaClientApplication {
    public static void main(String[] args) {, args); }}Copy the code

The configuration application. Yml

  port: 8082Name: provider eureka: client: #true
    register-with-eureka: true
    fetch-registry: trueServiceUrl: defaultZone: HTTP://localhost:8080/eureka/
Copy the code

The effect

Visit localhost:8080 and you can see that the provider service is registered

Eureka Server protection mode

Let's turn off the provider service and look at the console after 90 seconds.The sentence in red indicates that the Eureka Server has enabled self-protection. When the service provider fails to send heartbeat packets for renewal due to network fluctuations, the Eureka Server does not delete the service but keeps the service. At this point, however, it is impossible to determine whether the service provider has really hung up, and it is likely that an error will be reported when a consumer requests the service.

The Eureka Server disables self-protection. Procedure

    enable-self-preservation: false
Copy the code

Eureka Server cluster

A cluster is a cluster of Eureka servers registered with each other


The following two points are different from non-clustered Eureka Server

  • eureka.client.register-with-eurekaandeureka.client.fetch-registrySet to true
  • eureka.client.serviceUrl.defaultZoneAddresses of other registries, multiple registries separated by commas
  port: 8081Spring: Application: name: eurekaServer Eureka: #falseInstance: hostname: Peer2 Client: # Cluster modetrueThe default istrue
    #register-with-eureka: false
    #fetch-registry: falseIf we have three registries, write the addresses of the other two for defaultZone: HTTP://peer1:8080/eureka/,http://peer3:8082/eureka/
      defaultZone: http://peer1:8080/eureka/
Copy the code

Note that the eureka.instance.hostname of the two registries is different, so the DS Replicas will have content. In real deployment, registries are usually located on different servers

The effect

Access Peer1:8080 or Peer2:8081

Eureka Server security authentication

Implemented through Spring Security

Import dependence

Copy the code

The simplest configuration

You can also intercept urls for authentication in the Security configuration class by adding the following to application.yml

      name: user
      password: 123456
Copy the code

Now need to access the console input password, registered address also need to carry the account password, service won't be able to register on eureka. Client. ServiceUrl. DefaultZone values should be for http:// account: password @ peer2:8081 / eureka

Close the CSRF

After security authentication is enabled, services cannot be registered without CSRF being disabled, but the console can be opened

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity http) throws Exception {
        / / disable CSRF
        super.configure(http); }}Copy the code

Other configuration

  • eureka.client.enabledWhether to enable Eureka Client true
  • eureka.client.register-with-eurekaIndicates whether to register yourself with Eureka Server True
  • eureka.client.fetch-registryIndicates whether to obtain registered service information from Eureka Server true
  • eureka.client.serviceUrl.defaultZoneConfigure the Eureka Server address for registering and obtaining serviceshttp://localhost:8761/eureka
  • eureka.client.registry-fetch-interval-secondsThe default value is 30 seconds. That is, the Eureka Server obtains and caches services every 30 seconds
  • for sending heartbeat messages to Eureka Server, in seconds, for service renewal. The default value is 30 seconds
  • the service expiration time, which is the number of seconds after the Eureka Server detects that the Eureka Client has no heartbeat (the Client goes offline unexpectedly). The default value is 90 seconds
  • eureka.server.enable-self-preservationUsed to enable the Eureka Server self-protection function True
  • eureka.client.instance-info-replication-interval-secondsInterval at which instance information is updated to the Eureka server, in seconds. The default value is 30 seconds
  • eureka.client.eureka-service-url-poll-interval-secondsInterval for polling Eureka server address changes, in seconds. Default: 300 seconds
  • eureka.instance.prefer-ip-addressIndicates that IP is not the domain name. The default value is false
  • eureka.client.healthcheck.enabledBy default, the Erueka Server detects the health status of the Eureka Client using the heartbeat. By setting this parameter to true, the Eeureka Server can use the/Health endpoints of the Eeureka Server to detect the health status of the Client.
About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.