“This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1, the introduction of

Non-partitioned cluster deployment is a simple cluster deployment mode of Eureka. In this mode, the Eureka Server in the cluster is not partitioned. Normally, if our Eureka servers are all in the same room, we can cluster in this way.

2. Modify the hosts file

Since I started multiple Eureka services in a Windows environment to simulate cluster deployment, I first modified the C:\Windows\System32\drivers\etc\hosts file for proper domain name addressing (using 127.0.0.1 without modifying it is also fine).

127.0.0.1       eureka18881.com
127.0.0.1       eureka18882.com
127.0.0.1       eureka18883.com
127.0.0.1       eureka18884.com
Copy the code

3. Setup of Eureka Server

I have built a total of four Eureka servers. In order to understand the cluster mode and startup process of the whole Eureka Server, I have built four Eureka servers, namely eureka-01, Eureka-02, Eureka-03 and Eureka-04. If there are too many services, you can create different profiles. You can specify different profiles when starting Eureka Server.

Step 1: Introduce POM dependencies. I use the Spring-Cloud version Greenwich.SR1

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Liziba < / groupId > < artifactId > spring - the cloud - netflix - demo < / artifactId > < packaging > pom < / packaging > < version > 1.0 - the SNAPSHOT < / version > < modules > < module > Eureka - 01 < module > <module>Eureka-02</module> <module>Eureka-03</module> <module>Eureka-04</module> </modules> <properties> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>Copy the code

Step 2: Create the startup class application_Eurekaserver01.java and use the @enableEurekaserver annotation to start EurekaServer

/** * <p> ** EurekaServer01 </p> ** @author: Liziba * @date: 2021/10/30 10:46 */ @SpringBootApplication @EnableEurekaServer public class Application_EurekaServer01 { public static void main(String[] args) { SpringApplication.run(Application_EurekaServer01.class, args); }}Copy the code

Step 3: Create application. Yml/application. The properties configuration file, in the following configuration file configuration class current Eureka Server port information, service name, instance, whether registered address, the cluster array itself and the registration information, Here I registered the Eureka Server itself with the current Eureka Server.

Spring: Application: name: eureka-01 ## Eureka: instance: hostname: eureka18881.com client: service-url: defaultZone: http://eureka18882.com:18882/eureka/,http://eureka18883.com:18883/eureka/,http://eureka18884.com:18884/eureka/ register-with-eureka: true fetch-registry: trueCopy the code

Step 4: Create three more identical applications and modify their port information, service name, and cluster address. For example, the Application_EurekaServer02 configuration file is shown below.

Spring: Application: name: eureka-02 ## Alter instance hostname Eureka: instance: hostname Eureka18882.com client: service-url: ## defaultZone http://eureka18881.com:18881/eureka/,http://eureka18883.com:18883/eureka/,http://eureka18884.com:18884/eureka/ register-with-eureka: true fetch-registry: trueCopy the code

Step 5: Start services. Start four Eureka servers one by one. After all services are started successfully, access the Eureka Dashboard to view cluster information. Each Eureka Dashboard can see four service registration information displayed in Instances Currently Registered with Eureka. Application is the name of the Application configuration provided in the configuration file. In the DS Replicas, the defaultZone cluster information is displayed, and the displayed name is Eureka.instance.hostname configured in the Eureka Server configuration file

4. Build Eureka Client

The Eureka Server is not partitioned. Therefore, the Eureka Client does not need to consider partitioning configurations in the Eureka Server.

Step 1: Create the Client service and introduce the Eureka Client dependency

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

Step 2: Create the startup class and use @enableDiscoveryClient to enable service discovery

/ * * * < p > * client application startup * < / p > * * @ Author: Liziba * @ the Date: 2021/10/30 15:34 */ @SpringBootApplication @EnableDiscoveryClient public class Application_client01 { public static void  main(String[] args) { SpringApplication.run(Application_client01.class, args); }}Copy the code

Step 3: Create a configuration file and specify the cluster service address in the configuration file

server: port: 19991 spring: application: name: client-01 eureka: client: service-url: defaultZone: http://eureka18881.com:18881/eureka/,http://eureka18882.com:18882/eureka/,http://eureka18883.com:18883/eureka/,http://eu reka18884.com:18884/eureka/Copy the code

Step 4: Check the service registration information through the Eureka Dashboard. You can see that client-01 services are registered on each Eureka Server