The following example uses a project to enable three Eureka-Servers as a cluster, extending the same

The three ports are 7001, 7002, and 7003

1. Create a SpringBoot project and add the Eureka-Server dependency

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

Add @enableEurekaserver to the startup class to enable the Eureka-serve function

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

2. Configure the cluster in the resouces/application.yml configuration file

Server: port: ${port:7001} tomcat: uri-encoding: UTF-8 Application: name: spring-cloud-eureka-cluster eureka: instance: hostname eureka-cluster-${server.port} other-node-port2: ${p2:7002} other-node-port3: ${p3:7003} client: register-with-eureka: [default] [default] [default] [default] [default] [default] [default] http://eureka-cluster-${eureka.other-node-port2}:${eureka.other-node-port2}/eureka/,http://eureka-cluster-${eureka.other -node-port3}:${eureka.other-node-port3}/eureka/Copy the code

Note: Other-node-port2 is a custom name, not inherent to Eureka, so idea editor is not associated with it.

Key point 1: server.port defines ports by passing parameters rather than writing them to death.

Key point 2: other-node-port Specifies the port of other nodes. The port is also defined in parameter passing mode.

Key point 3: Eureka.instance. hostname also reads the port number ${server.port} defined above in placeholder form.

Key point 4: service-url.defaultzone configure multiple cluster addresses that do not include the current 7001. Write the other two 7002 and 7003 addresses

I have configured hosts for this address http://eureka-cluster-7001:7001, as follows:

127.0.0.1 eureka-cluster-7001
127.0.0.1 eureka-cluster-7002
127.0.0.1 eureka-cluster-7002
Copy the code

Start the project after the configuration is complete

Click on the Edit Configurations…

For the first startup, the name of 7001 should be Application by default, but I changed it to Eureka-cluster-7001 to distinguish it

Select Eureka-cluster-7001, click the copy button above, and change the copied Name to Eureka-cluster-7002

Enter the startup parameters -dport = 7002-dp2 =7001 -DP3=7003 on VM Options

Repeat the preceding steps to replicate the cluster Name and change the Name to Eureka-cluster-7003

-dport =7003 -DP2=7001 -DP3=7002 in VM Options

Be careful not to write wrong boot parameters!

When done, click OK.

The Settings of 7001, 7002 and 7003 are as follows:



If the three services are enabled and can be accessed, the configuration is successful

http://eureka-cluster-7001:7001/

http://eureka-cluster-7002:7002/

http://eureka-cluster-7003:7003/