After setting up the environment, we can start microservice development by creating a Module and running it as the Eureka Server, which acts as the registry and supports cluster deployment to improve the stability of the registry. We first use a single Eureka Server to provide the service, and then demonstrate the cluster form

Monomer had Server

Creating eureka is a simple, four-step process:

  1. To build the module.

Create a Maven Module, let’s say we call it Cloud-Eureka-Server7001. 7001 is the port number, because I’m demonstrating on my computer, I’m using different port numbers for different services for the sake of demonstration.

  1. Write the pom

Add spring Boot and Eureka Server dependencies

  <dependencies>
    <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-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
    </dependency>
  </dependencies>  
Copy the code
  1. Change to yml

To change yML is to change the module configuration file, in the form of YML. Details are as follows:

server:
  port: 7001

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:7001/eureka
Copy the code

The port number for the service is first specified through port, followed by the declaration of Eureka Server. Because the service itself provides service registration function, so there is no need to register again, so set register-with-eureka to false, and it does not act as a service provider or consumer, so there is no need to pull registration information, so fetch-registry is also false. Finally, specify the registration address using defaultZone

  1. The main start

Add a startup class and declare it as eureka server. The startup class simply creates a class with a main method and marks it as SpringBootApplication, for example

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

This allows you to run the class to start the registry. If all is well, you should see information about the service if you open http://localhost:7001.

Eureka cluster server

Individual registries are certainly not stable, so we can create clusters. The steps for creating a cluster are the same as those for singleton, except that some configurations need to be paid attention to. Details are as follows:

  1. To build the module

Create a Maven Module cloud-eureka-Server7002. Note that this time we need to change the port number to 7002.

  1. Write the pom

It’s exactly the same dependence as the monomer.

  1. Change to yml

This is a bit different from a single eureka server, because we are building a cluster of Eureka Servers, so they need to register with each other to form a cluster. By mutual registration, defaultZone is changed to the URL of another server. For example, IF I have three server services A,B, and C. Then A defaultZone is the address of B and C,B defaultZone is the address of A and C, and C is the same.

So we need to change the defaultZone for both servers to register with each other. The details are as follows: Server7001

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka
Copy the code

server7002

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
Copy the code

From the above we can see that the defaultZone of server 7001 points to 7002 and server 7002 points to 7001. In addition, it should be noted that our hostname is no longer localhost, but changed to the suffix of their respective ports. This is mainly because it is easier to differentiate when there are many services, rather than showing localhost. You can map localhost to your own name by modifying hosts as follows: open the c: Windows \ System32 \drivers\etc\hosts file. Then add the following mapping

127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
Copy the code

4. As for the main boot, add two marks as follows:

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

This way, when you start two servers, you should see that they contain each other’s services