This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

Version, environment and other information

Springboot: 2.5.2

Springcloud: 2020.0.3

The JDK: 1.8

Maven: 3.5.9

Build the Eureka server

Create a SpringBoot project and name it Eureka-Server

Pm.xml introduces eureka-server: spring-cloud-starter-netflix-eureka-server

Add the @enableEurekaserver annotation to the startup class to declare the application as a server for Eureka, that is, a registry

Configure eureka correlation in yamL files

# application.yml
spring:
  application:
    name: eureka-server
server:
  port: 8100
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
# Disable search services
    fetch-registry: false
# Do not register yourself with the registry
    register-with-eureka: false
Copy the code

Set up eureka client (service provider, producer)

Create a SpringBoot project and name it Eureka-client-Provider

Pom.xml introduces eureka-client: spring-cloud-starter-netflix-eureka-client

XML must import spring-boot-starter-web, otherwise error will be reported, do not ask why, ask is this pit I step on ╥﹏╥…

Add the service discovery annotation @enableDiscoveryClient to the startup class

Configure client-related properties in YAML

# application.yml
spring:
  application:
    name: eureka-client-provider
server:
  port: 8300
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
  instance:
# register with IP address
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}
Copy the code

Note 1

Most of the material is written in the Properties format

One used by the configuration items eureka. Client. ServiceUrl. DefaultZone = http://localhost:8761/eureka/, corresponding yml service – the url is map structure, You need to add a property value with a key for defaultZone

Note 2

Service – the url defaultZone configuration, particular demand ends in eureka, the specific reason is not found in the source code, source code in the default defaultZone = “http://localhost:8761/eureka/”

See the source code below:

// EurekaClientConfigBean.class
public static final String DEFAULT_URL = "http://localhost:8761" + DEFAULT_PREFIX + "/"; // line: 55

// EurekaConstants.class
public static final String DEFAULT_PREFIX = "/eureka"; // line: 29
Copy the code

Build eureka client (service consumption, consumer)

Create a SpringBoot project and name it Eureka-client-consumer

Copy the client in the previous step and change the IP address and application-name

The RestTemplate is used for HTTP access to request the service provision, where the URL part uses the service name that the service provider registered with Eureka

@GetMapping("/call")
public String hello(a) {
	return restTemplate.getForObject("http://eureka-client-provider/user/hello", String.class);
}
Copy the code

Use a request balancing policy for RestTemplate

@Bean
@LoadBalanced
public RestTemplate restTemplate(a) {
	return new RestTemplate();
}
Copy the code

Note that is used in load balancing org. Springframework. Cloud. Client. Loadbalancer. LoadBalanced, and Ribbon, Feign has nothing to do

Password authentication is enabled in the registry

Both the server and client of Eureka require the introduction of the Spring-Security package

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
Copy the code

eureka-serverProject configuration

Adding Configuration Data

# application.yml
spring:
  security:
    user:
      name: admin
      password: admin
Copy the code

Added the Security configuration class

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        / / close CSRF
        http.csrf().disable();
        / / support httpBasichttp.authorizeRequests().anyRequest().authenticated().and().httpBasic(); }}Copy the code

Note:

  • After security authentication is enabled, login to eureka’s registry requires an account password

  • The registration configuration in the Eureka client also requires authentication configuration

eureka-client-xxProject configuration

eureka:
  client:
    service-url:
If the eureka server is enabled with password authentication, the client URL connection needs to carry verification information
      defaultZone: http://admin:admin@localhost:8100/eureka
Copy the code

Security validation data extraction variable optimization

# application.yml
security:
  uname: admin
  pwd: admin
eureka:
  client:
    service-url:
If the eureka server is enabled with password authentication, the client URL connection needs to carry verification information
      defaultZone: http://${security.uname}:${security.pwd}@localhost:8100/eureka
Copy the code

Extract to the configuration file

Or, put the configuration into application-security.yml, and then we import it through the configuration file

# application-security.yml
  security:
    uname: admin
    pwd: admin
Copy the code
# application.yml
spring:
  profiles:
    include:
    - security
#...
Copy the code

Eureka registry cluster setup

Start multiple servers and register them with each other

An easy way to do this is to start eureka registries with different ports by enabling different configuration files

The server configuration file is modified

# appliction-master.yml
server:
  port: 8101
eureka:
  client:
    service-url:
      Register with slave
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:8102/eureka
Copy the code
#application-slave.yml
server:
  port: 8102
eureka:
  client:
    service-url:
      Register with master
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:8102/eureka
Copy the code

Modify the client configuration file

Modify eureka client, add multiple server URL, string form, with, split

eureka:
  client:
    service-url:
If the eureka server is enabled with password authentication, the client URL connection needs to carry verification information
      defaultZone: 
        http://${security.uname}:${security.pwd}@localhost:8101/eureka,
        http://${security.uname}:${security.pwd}@localhost:8102/eureka
Copy the code

Eureka’s self-protection model andInstanceIDconfiguration

Turn off self-protection

# eureka-server add configuration
eureka:
  server:
# Disable the automatic protection mode
    enable-self-preservation: false
Copy the code

Modified for instance registrationinstanceID

Add instance configuration to client
eureka:
  instance:
# register with IP address
    prefer-ip-address: true
# Change the name displayed when the example is registered
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
Copy the code

Eureka was developed to quickly remove failed services

Development use only

Server side configuration

eureka:
  server:
# Disable the automatic protection mode
    enable-self-preservation: false
Default 60s
    eviction-interval-timer-in-ms: 5000
Copy the code

Client Configuration

Spring-boot-starter -actuator is required

eureka:
  client:
    healthcheck:
      enabled: true
  instance:
# Heartbeat sending frequency: default 30s
    lease-renewal-interval-in-seconds: 5
# Heartbeat timeout: default 30s
    lease-expiration-duration-in-seconds: 5
Copy the code

More Instance configuration class in the configuration information source for reference: org.springframework.cloud.net flix. Eureka. EurekaInstanceConfigBean

More configuration class in the Server configuration information source for reference: org.springframework.cloud.net flix. Eureka. Server EurekaServerConfigBean