Eureka Server configuration
Import dependence
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Copy the code
Add the @enableeurekaserver annotation to the startup class
@EnableEurekaServer// Add this annotation
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Copy the code
The configuration application. Yml
server:
port: 8080
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Copy the code
eureka.instance.hostname
Specify the IP address and domain name of the Eureka server.eureka.client.register-with-eureka
Whether to register the service with the Eureka servereureka.client.fetch-registry
Whether to obtain service information from Eureka servereureka.client.serviceUrl.defaultZone
Specify 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.defaultZone
The client registry address is configured, not the console address.
Eureka Client configuration
Import dependence
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code
Add the @enableeurekaclient annotation to the startup class
@EnableEurekaClient// Add this annotation
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); }}Copy the code
The configuration application. Yml
server:
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
eureka:
server:
enable-self-preservation: false
Copy the code
Eureka Server cluster
A cluster is a cluster of Eureka servers registered with each other
configuration
The following two points are different from non-clustered Eureka Server
eureka.client.register-with-eureka
andeureka.client.fetch-registry
Set to trueeureka.client.serviceUrl.defaultZone
Addresses of other registries, multiple registries separated by commas
server:
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
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
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
spring:
security:
user:
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
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
/ / disable CSRF
http.csrf().disable();
super.configure(http); }}Copy the code
Other configuration
eureka.client.enabled
Whether to enable Eureka Client trueeureka.client.register-with-eureka
Indicates whether to register yourself with Eureka Server Trueeureka.client.fetch-registry
Indicates whether to obtain registered service information from Eureka Server trueeureka.client.serviceUrl.defaultZone
Configure the Eureka Server address for registering and obtaining serviceshttp://localhost:8761/eurekaeureka.client.registry-fetch-interval-seconds
The default value is 30 seconds. That is, the Eureka Server obtains and caches services every 30 secondseureka.instance.lease-renewal-interval-in-seconds
Interval for sending heartbeat messages to Eureka Server, in seconds, for service renewal. The default value is 30 secondseureka.instance.lease-expiration-duration-in-seconds
Defines 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 secondseureka.server.enable-self-preservation
Used to enable the Eureka Server self-protection function Trueeureka.client.instance-info-replication-interval-seconds
Interval at which instance information is updated to the Eureka server, in seconds. The default value is 30 secondseureka.client.eureka-service-url-poll-interval-seconds
Interval for polling Eureka server address changes, in seconds. Default: 300 secondseureka.instance.prefer-ip-address
Indicates that IP is not the domain name. The default value is falseeureka.client.healthcheck.enabled
By 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.