The Spring Cloud registry can be implemented by Eureka, Consul, Zookeeper, ETCD, etc. It is recommended to use Spring Cloud Eureka to implement the registry, which is based on Netfilix Eureka secondary encapsulation. Complete the function of service governance in distributed service, service registration and discovery in microservice system are managed through this registry.

Introduce Eureka Server dependencies

Add the Spring Cloud dependencies from the previous article, and now add the registry Eureka Server dependencies.

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>
Copy the code

Add a startup class to enable Eureka Server

Add the startup class to the root directory:

@EnableEurekaServer @SpringBootApplication public class RegisterApplication { public static void main(String[] args) { new SpringApplicationBuilder(RegisterApplication.class).bannerMode(Banner.Mode.LOG) .run(args); }}Copy the code

@enableeurekaserver The annotation enables the registry server function.

The Eureka configuration is added

Add the following configuration to application.yml:

spring: 
  application: 
    name: register-center
  profiles: 
    active: register-center1

eureka:
  instance: 
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    lease-expiration-duration-in-seconds: ${lease-expiration-duration-in-seconds}
    lease-renewal-interval-in-seconds: ${lease-renewal-interval-in-seconds}
  server: 
    enable-self-preservation: ${enable-self-preservation}  
    eviction-interval-timer-in-ms: ${eviction-interval-timer-in-ms}
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl: 
      defaultZone: ${register-center.urls}

---  
spring: 
  profiles: register-center1

server: 
  port: ${register-center1.server.port}

---
spring: 
  profiles: register-center2

server: 
  port: ${register-center2.server.port}
Copy the code

Here did two registry of the high availability configuration register – center1, register – center2, can also do more than one, since it is the high availability, each registry registry themselves to the other.

Maven filter configuration

The configuration in ${} is packaged and controlled by Maven Resource filter. Different environments use different configuration files.

For example, filter-dev.properties:

# url register - center1. Server. The IP = 192.168.1.22 register - center2. Server IP = 192.168.1.23 register-center.urls=http://${register-center1.server.ip}:${register-center1.server.port}/eureka/,http://${register-cent er2.server.ip}:${register-center2.server.port}/eureka/ #port register-center1.server.port=7001 register-center2.server.port=7002 #config enable-self-preservation=false eviction-interval-timer-in-ms=5000 lease-expiration-duration-in-seconds=20 lease-renewal-interval-in-seconds=6Copy the code

Spring Cloud configuration details

Refer to the Spring Boot series for configuration, and only the configuration used by Spring Cloud is explained here.

Spring.application. name: Configures the application name, the service registration name displayed in the registry.

Spring. Cloud. Client. IpAddress: get the client’s IP address.

Eureka.instance. prefer-ip-address: Set this parameter to true to indicate the preferred IP address. That is, the connection registry uses the IP address or HOSTNAME, but this parameter is not recommended in the production environment.

Eureka.instance. instance-id: indicates the ID of the unique instance registered in the registry.

Eureka. Instance. lease-expiration- durations-in-seconds: Indicates the time (in seconds) that the Eureka server will wait after receiving the last heartbeat before deleting this instance from this view and disabling traffic to this instance. Setting this value too long may mean that traffic can be routed to the instance, even if the instance does not exist. Setting this value too small may mean that the instance may be untrafficked due to a temporary network failure. This value will be set to a value at least higher than that specified in lease-renewal- interval-seconds.

Eureka. Instance. lease-renewal-interval-in-seconds: Indicates that the Eureka client needs to send a heartbeat to the Eureka server to indicate how often it still exists, in seconds. If the heartbeat line is not received within the period specified in lease- expiration-durations-in-seconds, the Eureka server deletes the instance from its view and therefore does not allow traffic to the instance. Note that if the instance implements HealthCheckCallback and then decides to make itself unavailable, the instance may still be unable to access traffic.

Eureka.server. enable-self-preservation: configures whether to enable the self-preservation function of services in the registry.

Eurekag.server. eviction- interval-timer-in-MS: specifies the time interval for the registry to clear invalid nodes. Default is 60000 ms, 60 seconds.

Register-with-eureka: Set to true instructs this instance to register its information with the Eureka server for discovery by others. In some cases, you don’t want to discover instances, and you just want to discover other instances configured to false.

Eureka.client.fetch-registry: indicates whether the client should fetch eureka registry information from the Eureka server.

Eureka. Client. ServiceUrl. DefaultZone: had the address of the server.

Start the registry

Thus, a Eureka Server with two registries is constructed, starting with different profiles to specify different ports.

spring-boot:run -Drun.profiles=register-center1  -P dev
spring-boot:run -Drun.profiles=register-center2  -P dev
Copy the code

Recommended reading

Dry goods: Free 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.