Detailed spring cloud and netflixEureka Integration (Registry)

  • 2021-06-28 12:51:41
  • OfStack

Base Dependency


compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter') 

eureka (Single)

Server:

rely on


compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') 

application.yml Configuration


spring:
 application:
  name: dev
eureka:
 client:
  service-url:
   defaultZone: http:// Native Machine ip address :8761/eureka/ # Registry Address 
  register-with-eureka: false # Indicates whether the instance should register its information with the Eureka server for discovery.In some cases , You don't want your instance to be discovered and you want to discover other instances.Default to true
  fetch-registry: false # Indicates whether the client should obtain Eureka registry information from the Eureka server.Default to true
server:
 port: 8761

Startup Class Add @EnableEurekaServer

Client:

Primary Dependency


compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') 

application.yml Configuration


eureka:
 client:
  service-url:
   defaultZone: http://eureka Service Address :8761/eureka/ 

Startup class adds @EnableDiscoveryClient (Registry General Client Note) or @EnableEurekaClient (available only to eureka clients)

eureka (Cluster)

Server

Primary Dependency


compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') 

application.yml Configuration


server:
 port: 8761
eureka:
 client:
  register-with-eureka: false
  fetch-registry: false
 server:
  enable-self-preservation: false # Use self-protection, default true
  peer-node-read-timeout-ms: 5000 # Node read timeout, default 200 Millisecond 
---
spring:
 application:
  name: eureka1
 profiles: eureka1
eureka:
 client:
  service-url:
   defaultZone: http://eureka2:8761/eureka/,http://eureka3:8761/eureka/
 instance:
  hostname: eureka1
---
spring:
 application:
  name: eureka2
 profiles: eureka2
eureka:
 client:
  service-url:
   defaultZone: http://eureka1:8761/eureka/,http://eureka3:8761/eureka/
 instance:
  hostname: eureka2
---
spring:
 application:
  name: eureka3
 profiles: eureka3
eureka:
 client:
  service-url:
   defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/
 instance:
  hostname: eureka3

eureka.client.service-url.defaultZone and eureka.instance.hostsname eureka1, eureka2, eureka3 are all written in the native hosts file.

For example, the hosts file on the eureka1 server
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 eureka1
xxx.xxx.x.xx eureka2
xx.xxx.xxx.xx eureka3

The startup class also adds @EnableEurekaServer Note: If the program is required to receive command line parameters when running the jar package, 1 must pass in the args parameter in the SpringApplication.run() method of the main method

For example:


public static void main(String[] args) {
   SpringApplication.run(QnbbsConsumer.class,args);
}

Enter java-jar xxx.jar on the command line if not passed in -- parameter name=parameter value parameter will not work and will affect the environment configuration you cannot choose to write

Client:

Depends on the same as a single machine

application.yml Configuration


eureka:
 instance:
  prefer-ip-address: true # Is it displayed ip
  ip-address:  Native Machine ip # Fill in Native Machine ip address 
 client:
  service-url:
   defaultZone: http://eureka1:8761/eureka/,http://eureka2:8761/eureka/ , http://eureka3:8761/eureka/

Startup Class Annotations vs. Stand-alone Version 1

The client registers a registry and registers with another cluster to demonstrate success!

eureka Add spring-security Permission Authentication

rely on


compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') 

application.yml Configuration


compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') 
0

Create a class and inherit the WebSecurityConfigurerAdapter class, add @EnableWebSecurity and @Configuration annotations on it, override the configure method


compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') 
1

Startup Class Add Comment


compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') 
2

Offline Service

http://eureka Address: Port/eureka/apps/Service Name/Service Address: Service Port Number

Examples are as follows:

erueka Registry ip: 10.100.1.100

Port: 12000

Service name: CPS-RISK-SERVER

Instance id: 192.168.10.54:18883

Send the delete command through http to the following url to delete the specified service instance:
http://10.100.1.100:12000/eureka/apps/CPS-RISK-SERVER/192.168.10.54:18883

Change Service Status

http://eureka Address: Port/eureka/apps/Service Name/Service Address/status?value=${value}

Where ${value} is OUT_OF_SERVICE, DOWN, UP


Related articles: