SpringCloud service discovery component Eureka practice

Eureka is an open source Netflix service discovery component itself is a REST-based services. It contains two parts Server and Client. Spring Cloud integrate it in subprojects Spring Cloud Netflix in order to achieve registration and found that the micro-services.
Eureka characteristics are as follows:
1.Eureka consists of two components: Eureka Server and Client Eureka;
2.Eureka Server provides the ability to service discovery, when the individual micro service starts, it will register their information to the Eureka Server, Eureka Server will store this information;
3.Eureka client is a Java client for simplifying interaction with the Eureka Server;
below demonstrates Eureka common scene in practical work.
A. Eureka Server prepared
following the preparation of a Eureka Server component
1. Create a SpringBoot project named Eureka-ZZ
2. Import dependent maven

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

3. Start writing class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

4. Add the following in the configuration file application.yml

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

5. Start verification
visit http: // localhost: 8761, the following display interface proved successful launch
Here Insert Picture Description
seen, the Home Eureka Server display a lot of information, including the system state of the current instance, registered to the service instance on Eureka Server, common information, instance information

II. Eureka Client prepared
following the preparation of a Eureka Client, Eureka Server to register and achieve the above
1. Create a SpringBoot project named User-Provider-ZZ
2. Import dependent maven

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

3. Start writing class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class SimpleProviderUserApplication {
	public static void main(String[] args) {
		SpringApplication.run(SimpleProviderUserApplication.class, args);
	}
}

Note: @EnableDiscoveryClient declaration This is a Eureka Client, you can use annotations @EnableEurekaClient alternative @EnableDiscoveryClient
difference between the two:
@EnableEurekaClient show Eureka is the Client, the annotations are spring-cloud-netflix project notes, and only Eureka working together
@EnableDiscoveryClient various service components include Zookeeper, Consul and other support provided, the annotations are spring-cloud-commons project notes
4. Add the following to the configuration file in application.yml

spring:
  application:
    name: zz-provider-user
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

5. Start verification
visit again http: // localhost: 8761, the following display interface proved successful registration. At this point Eureka Client has been successfully registered with the Eureka Server
Here Insert Picture Description
three .Eureka Server high availability deployment of
single-node Eureka Server is not suitable for production line environments. Eureka Client connection Eureka Server will regularly get news of the registry and cache, always use the local cache data services and remote micro-consumption, so in general Eureka Server downtime will not affect the calls between the service. But if Eureka Server goes down, some micro service is also down, Eureka Client cache is updated if not, it could affect the micro-calling services, and even affect the high availability of the entire cluster. Therefore, a need to deploy a high-availability Eureka clusters. Writing a highly available below Server Eureka
1. host configuration file, add a line
127.0.0.1 peer1 peer2
2. Modify application.yml

spring:
  application:
    name: zz-eureka
---
spring:
  profiles: peer1                                 # 指定profile=peer1
server:
  port: 8761
eureka:
  instance:
    hostname: peer1                               # 指定当profile=peer1时,主机名是peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/      # 将自己注册到peer2这个Eureka上面去
---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/

3. Start the Eureka clusters
will serve Eureka labeled jar package to start the command mode

java -jar zz-eureka-1.0-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar zz-eureka-1.0-SNAPSHOT.jar --spring.profiles.active=peer2

4. just Eureka-Client registered on the Eureka clusters
modify Eureka-Client is configured to:

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8761/eureka/

Eureka-Client can restart

Four .Eureka User Authentication Server's
default, Eureka Server to allow anonymous access, so for security reasons, user authentication can be configured to Eureka Server, perform the following practice.
1. Import dependent maven

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

2. placed application.yml

security:
  basic:
    enabled: true               # 开启基于HTTP basic的认证
  user:
    name: admin                  # 配置登录的账号是admin
    password: 123456       # 配置登录的密码是123456
spring:
  application:
    name: zz-eureka
---
spring:
  profiles: peer1                                 # 指定profile=peer1
server:
  port: 8761
eureka:
  instance:
    hostname: peer1                               # 指定当profile=peer1时,主机名是peer1
  client:
    serviceUrl:
      defaultZone: http://admin:123456@peer2:8762/eureka/      # 将自己注册到peer2这个Eureka上面去
---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://admin:123456@peer1:8761/eureka/

3. Start Eureka Server cluster
start-up mode as above, the package start with java -jar command
4. Eureka-Client register to require authentication Eureka Server
only need to configure eureka.instance.client.defaultZone to
HTTP: // the User : password @EUREKA_HOST: EUREKA_PORT / eureka
after configuring the start, both the Eureka-Client registered on the Eureka Server clusters

Five .Eureka self-protection mode
by default, if Eureka Server does not receive a heartbeat micro service instances within a certain time, Eureka Server will log off the instance (default 90 seconds). But when the network partition failure occurs between the micro and the Eureka Server service can not communicate properly, which may become very dangerous because micro ---- service itself is healthy, then this should not be written off this micro-services.
Eureka Server to solve this problem through the "limp home mode" ---- When Eureka Server node losing too much in a short time a client (network partition failure may have occurred), then the node goes into self-preservation mode. Once in this mode, Eureka Server service will protect the information in the registry, delete data no longer service registry (which is not written off any micro-services). When the network fault recovery, the Eureka Server node will automatically exit the self-protection mode.
Self-protection mode is enabled by default. Can be disabled through the following configuration

eureka:
  server:
    enable-self-preservation: false

Six .Eureka health check
heartbeat mechanism between Eureka Server and Eureka Client to determine the status of Eureka Client, by default, as long as the server and client to maintain a normal heartbeat, the application will remain "up" state. However, the above mechanism does not fully reflect the state of the application. For example heartbeat normal service between the micro and Eureka Server, Eureka Server considers the micro-services "up", but the problem (for example, because the network jitter, Rom data source) data source for the micro-services took place, simply can not work. Spring Boot Actuator offers / health endpoint, which can show the application of health information, so if you can spread the healthy state of the endpoint to Eureka Server can
To do this, just need to start Eureka health check. In this way, the application will spread to their own health status Eureka Server. A healthy start by checking the following configuration

eureka:
  client:
    healthcheck:
      enabled: true
Published 19 original articles · won praise 20 · views 5856

Guess you like

Origin blog.csdn.net/qq_15898739/article/details/88955497