Spring Cloud was used in this paper as 2.1.8RELEASE, version=Greenwich.SR3

1. Create a service registry

1.1 Creating a Spring Boot Project: Eureka-Server

1.2 Pom.xml depends on jar packages

<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-server</artifactId>
</dependency>
Copy the code

1.3 EurekaServerApplication Add the annotation @enableeurekaserver

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

@enableEurekaserver: Enable the EurekaServer configuration

1.4 Adding the content of the configuration file: application.yml

spring:
  application:
    name: eureka-server

server:
  port: 8701
Start with no parameters
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    register-with-eureka: false Set this parameter to false and default to true
    fetch-registry: false Set to false and default to true to not retrieve registered services from the service center
  server:
    eviction-interval-timer-in-ms: 5000 # clean interval (in milliseconds, default 60*1000)
    enable-self-preservation: true # Default to true, set to false, disable self-protection
    # Eureka Server: During the run, it will count whether the heartbeat failure rate is less than 85% within 15 minutes
    renewal-percent-threshold: 0.49 # 0.85 by default
Copy the code

In single-machine deployment mode, the value of register-with-Eureka and fetch-registry should be false. Otherwise, an error message is displayed: Cannot execute Request on any known server. The reason is that, by default, the Eureka service registry attempts to register itself as a client.

1.5 Starting the Service

Open the browser and enter http://localhost:8701. The following information is displayed:

The red box indicates that no instance has been registered

conclusion

At this point, a simple standalone service registry is built.

Set up the service registry cluster

To ensure high availability of the service, we need to change the standalone application to a cluster application. Next, we create a simple Eureka Server cluster.

1.1 Modifying a Local Host

  • 127.0.0.1 eureka1.server.com
  • 127.0.0.1 eureka2.server.com
  • 127.0.0.1 eureka3.server.com

1.2 Adding the application.yml configuration file

Add application-server1.yml and application-server2.yml files, and modify the original application.yml file as follows:

  • application.yml
spring:
  application:
    name: eureka-server

server:
  port: 8701
Start with no parameters
eureka:
  instance:
    hostname: eureka1.server.com
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
    register-with-eureka: false
    fetch-registry: true
  server:
    eviction-interval-timer-in-ms: 5000
    enable-self-preservation: true
    renewal-percent-threshold: 0.49 
Copy the code
  • application-server1.yml
spring:
  application:
    name: eureka-server

server:
  port: 8702
Start with no parameters
eureka:
  instance:
    hostname: eureka2.server.com
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
    register-with-eureka: false 
    fetch-registry: true 
  server:
    eviction-interval-timer-in-ms: 5000 
    enable-self-preservation: true 
    renewal-percent-threshold: 0.49
Copy the code
  • application-server2.yml
spring:
  application:
    name: eureka-server

server:
  port: 8703
Start with no parameters
eureka:
  instance:
    hostname: eureka3.server.com
  client:
    service-url:
         defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
    register-with-eureka: false 
    fetch-registry: true
  server:
    eviction-interval-timer-in-ms: 5000
    enable-self-preservation: true
    renewal-percent-threshold: 0.49 # 0.85 by default
Copy the code

1.3 Adding the Startup Service to the IDEA

Set start services. Add services in the sequence 1234 in the screenshot

1.4 Start the Three Eureka Server services in sequence

After starting the service, visit eureka1.server.com:8701, eureka1.server.com:8702, eureka1.server.com:8703 respectively, as shown in the figure:

Three pages as shown in the figure above indicate that all services have been started successfully. At this point, a simple service center cluster is built.

conclusion

Yml and application-server2.yml can be added to the original application. Yml configuration by using the ‘–‘ three-bar mode and spring.profiles mode. Also add the startup parameter: –spring.profiles.active=config-server1. In this paper, an application. Yml method is used to facilitate future maintenance.

In this paper, we simply set up the single and cluster application of the service registry, and have a simple understanding of Eureka as a service registry. We will update other content of Eureka later.

Code warehouse

Making the address


  • Eureka Server Service Registry tutorial for Spring Cloud 2.x
  • Eureka Client Service Provider tutorial for Spring Cloud 2.x
  • Spring Cloud 2.x Ribbon Service Discovery Tutorial (with Integrated Hystrix Fuse)
  • Feign Service Discovery Tutorial with Integrated Hystrix Circuit breaker in Spring Cloud 2.x
  • Spring Cloud 2.x Zuul Routing Gateway Tutorial
  • Spring Cloud 2.x Config Distributed Configuration Center tutorial
  • Spring Cloud 2.x Hystrix Dashboard Circuit breaker tutorial

Please indicate the source of reprint,