Spring cloud registry eureka

Keywords: Spring Java git

brief introduction

Spring Cloud is currently one of the mainstream frameworks for developing micro services. We all know that the most basic and core module in micro services architecture is service registration and discovery.

In Spring Cloud, we can use its Eureka module to realize service registration and discovery. Spring Cloud Eureka is a secondary encapsulation based on Netflix Eureka. It is mainly responsible for the automatic registration and discovery of micro-service instances.

Eureka consists of two components: Eureka Server and Eureka Client

Eureka Server provides service registration service. After each node starts, it will register in Eureka Server. In this way, the service registry in Eureka Server will store the information of all available service nodes, and the information of service nodes can be seen intuitively in the interface.

EurekaClient is a Java client to simplify the interaction of Eureka Server. The client also has a built-in load balancer using round-robin load algorithm. After the application is started, the heartbeat is sent to Eureka Server (the default period is 30 seconds). If Eureka Server does not receive a node's heartbeat in multiple heartbeat cycles, Eureka Server will remove the service node from the service registry (default 90 seconds).

Construction and deployment

eureka server

1. Single point deployment
If it's an idea, you can directly create a new project > > spring initializr > > cloud discovery

By following these steps, you can build a single eureka server

2. Cluster deployment
Generally, the production environment is composed of three or more nodes, and each node in the cluster registers itself as a provider.

application.yml is configured as follows

server:
  port: 8761

spring:
  application:
    name: eureka
  profiles:
    active: dev
  security:
    basic:
      enabled: false # Enabling Identity Authentication
    user:
      name: lipeng # Define user names
      password: lipeng. # Define passwords

---
spring:
  profiles: prd-1

eureka:
  instance:
    hostname: register1
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://lipeng:lipeng.@200.200.0.166:8761/eureka/,http://lipeng:lipeng.@200.200.0.167:8761/eureka/,http://lipeng:lipeng.@200.200.0.171:8761/eureka/
  instance:
    prefer-ip-address: true
    ip-address: 200.200.0.166
  server:  #Configuration attributes, but due to the Eureka self-protection mode and the long heartbeat cycle, often encounter the problem of Eureka Server not eliminating the shut-down nodes.
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 5000

---
spring:
  profiles: prd-2

eureka:
  instance:
    hostname: register2
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://lipeng:lipeng.@200.200.0.166:8761/eureka/,http://lipeng:lipeng.@200.200.0.167:8761/eureka/,http://lipeng:lipeng.@200.200.0.171:8761/eureka/
  instance:
    prefer-ip-address: true
    ip-address: 200.200.0.171
  server:  #Configuration attributes, but due to the Eureka self-protection mode and the long heartbeat cycle, often encounter the problem of Eureka Server not eliminating the shut-down nodes.
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 5000

---
spring:
  profiles: prd-3

eureka:
  instance:
    hostname: register3
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://lipeng:lipeng.@200.200.0.166:8761/eureka/,http://lipeng:lipeng.@200.200.0.167:8761/eureka/,http://lipeng:lipeng.@200.200.0.171:8761/eureka/
  instance:
    prefer-ip-address: true
    ip-address: 200.200.0.167
  server:  #Configuration attributes, but due to the Eureka self-protection mode and the long heartbeat cycle, often encounter the problem of Eureka Server not eliminating the shut-down nodes.
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 5000


Add - spring.profile.active=prd-1/prd-2/prd-3 to the startup command when deployed after packaging
After startup, it is as follows:

If the above two nodes are in unavailable-replicas, then the deployment is successful.

Integration of eureka in k8s

Usually it is not recommended to use eureka in k8s, but sometimes it is better to fill in the domain name when deploying if necessary. Set this variable

eureka client

This is relatively simple. There are many examples on the Internet.
pom dependence

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

Add notes

yml configuration

spring:
  profiles: dev
eureka:
  instance:
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 20
    hostname: ${spring.application.name}
  client:
    serviceUrl:
      defaultZone: http://lipeng:lipeng.@200.200.0.166:8761/eureka/,http://lipeng:lipeng.@200.200.0.167:8761/eureka/,http://lipeng:lipeng.@200.200.0.171:8761/eureka/
    registry-fetch-interval-seconds: 10

In fact, defaultZone only needs to configure one node, and other nodes will automatically synchronize.
Start the service as you can see

See https://gitee.com/lpxs/lp-spring cloud.git for the detailed code, above which is the deployment demo of the configuration center.

Posted by expostfacto on Sun, 08 Sep 2019 02:25:17 -0700