preface

In our last blog post, we introduced Eureka Service Registration and Discovery in Spring Cloud Series I, which introduced the basic concepts and knowledge of Spring Cloud Eureka as a service registry. But the above services, which only apply to a single point of service, do not meet our requirements in a production environment.

In a distributed environment with a microservices architecture, we need to take into account the failure scenario, so in a production environment, the components must be highly available for deployment. Therefore, in this article, we will focus on how to improve eureka-Server

This time we aim to improve the service into the following structure:

Eureka Server

Maven dependency

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

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

Maven’s dependencies are the same as in the previous article, mainly introducing spring-cloud-starter-Netflix-Eureka-Server

2. Properties configuration

We need to configure a highly available Eureka-Server so, in a standalone environment, we differentiate between multiple services by distinguishing properties-profile

2.1 application. The properties

## Service application name
spring.applicaiton.name = spring-cloud-eureka-server

## Register with the registry
eureka.client.register-with-eureka = true
## To get registration information
eureka.client.fetch-registry = true

Copy the code

This is where the common properties are recorded and there is no need to distinguish between services.

In the previous article, we set both eureka.client.register-with-eureka and eureka.client.fetch-registry to false to avoid registering yourself in the registry in standalone case. In a distributed environment, we need these two parameters to correlate registries

2.2 application – peer1. Properties

## peer 1 Port 9090
server.port = 9090

## peer 2 host: localhost, port 9091
peer2.server.host = localhost
peer2.server.port = 9091

# Eureka registration information
eureka.client.serviceUrl.defaultZone = http://${peer2.server.host}:${peer2.server.port}/eureka
Copy the code

Configure information about the first registry. As you can see, we use the eureka. Client. ServiceUrl. DefaultZone = http://${peer2. Server. The host} : ${peer2. Server. The port} / eureka this address to register the service, Register the local service in the Peer2 registry.

2.3 application – peer2. Properties

Configure the server port
## Peer 2 Port 9091
server.port = 9091

## peer 1 host: localhost, port 9090
peer1.server.host = localhost
peer1.server.port = 9090

# Eureka registration information
eureka.client.serviceUrl.defaultZone = http://${peer1.server.host}:${peer1.server.port}/eureka
Copy the code

3. Start the service

In starting the class add @ EnableEurekaServer SpringCloudAvaliabilityEurkaApplication annotation

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

Set the launch parameters in Run Configurations, and launch multiple instances

Startup results:

Take note of the following:

As you can see, a copy of the registry serves our peer2

Eureka Client

Next we retrofit the Eureka-Client service to a highly available registry

Maven dependency

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

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

2. Properties configuration

# service name
spring.application.name = spring-cloud-eureka-client
server.port = 8083


Configure the connection to the Eureka server
eureka.client.serviceUrl.defaultZone = http://localhost:9090/eureka,http://localhost:9091/eureka

## Adjust the interval between obtaining all application meta-information
eureka.client.registryFetchIntervalSeconds = 5

## Adjust the application meta message interval
eureka.client.instanceInfoReplicationIntervalSeconds = 5

Copy the code

Here, multiple Eureka servers are set to pass through and separated. Please refer to EurekaClientConfigBean:

	public List<String> getEurekaServerServiceUrls(String myZone) {
		String serviceUrls = this.serviceUrl.get(myZone);
		if (serviceUrls == null || serviceUrls.isEmpty()) {
			serviceUrls = this.serviceUrl.get(DEFAULT_ZONE);
		}
		if(! StringUtils.isEmpty(serviceUrls)) { final String[] serviceUrlsSplit = StringUtils.commaDelimitedListToStringArray(serviceUrls); List<String> eurekaServiceUrls = new ArrayList<>(serviceUrlsSplit.length);for (String eurekaServiceUrl : serviceUrlsSplit) {
				if(! endsWithSlash(eurekaServiceUrl)) { eurekaServiceUrl +="/";
				}
				eurekaServiceUrls.add(eurekaServiceUrl);
			}
			return eurekaServiceUrls;
		}

		return new ArrayList<>();
	}
Copy the code

3. Start the service

Conclusion:

Source address: https://github.com/jaycekon/Spring-Cloud

Reference: https://juejin.cn/post/6844903651324985351 http://blog.didispace.com/springcloud6/ https://segmentfault.com/ls/1650000011386794