What is high availability

High Availability (HA) is one of the factors that must be considered in the architecture design of distributed systems. It usually refers to the design to reduce the time when the system cannot provide services.

Ii. How does Eureka achieve high availability

To complete the first registry project, the environment does not need to change. This article high availability is a simulation operation, so our action is to start both registry projects and modify the configuration files to achieve the same effect.

First modify the hosts file and add the following two lines of code under the file

C: hosts file in \Windows\System32\drivers\etc

Modify the Server code

Open the cloud-Demo-Eureka-server project and create application-server-1.yml and application-server-2.yml under SRC /main/resource

Note the changes to the hosthome node in the YML file

Application-server-1. yML content is as follows

server:
 port: 8761 # Your port
spring:
  security:
    user:
      name: admin
      password: admin
  application:
    name: service-registry
eureka:
 instance:
   hostname: server1 # Your address
 client:
   registerWithEureka: true
   fetchRegistry: true # fetchRegistry indicates whether registration information is fetched from the Eureka server
   serviceUrl:
     #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
     defaultZone: http://admin:admin@server2:8762/eureka/
Copy the code

Application-server-2. yML content is as follows

server:
 port: 8762 # Your port
spring:
  security:
    user:
      name: admin
      password: admin
  application:
    name: service-registry
eureka:
 instance:
   hostname: server2 # Your address
 client:
   registerWithEureka: true
   fetchRegistry: true # fetchRegistry indicates whether registration information is fetched from the Eureka server
   serviceUrl:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      defaultZone: http://admin:admin@server1:8761/eureka/
Copy the code

DefaultZone:http://admin:admin@server1:8761/eureka/The configuration here is for both registry servers to register with each other. The registerWithEureka and fetchRegistry properties are set to true

Yml :(start this configuration file twice and modify it. Start server-1 and server-2 respectively.)

spring:
  profiles:
    active:
    - server-1
Copy the code

The startup project is modified

Starting the first project will definitely cause an error because the other service cannot be found due to mutual registration. Don’t panic, start both services. Visit http://localhost:8761 or http://localhost:8762, and DS Replicas shows up as another service

Here you can see that the service-Registry SERVICE has already been started twice. Now I’m going to modify the client cloud-Demo-Eureka-client code

Modify the client code

Open the cloud – demo – eureka – client project, open the SRC/main/resource/application. The yml, modify the content, will fill in both the registry address in defaultZone node, and start the service, as the chart shows that the content, the start success.

spring:
  application:
    name: service-eureka-client # service name
server:
  port: 8800 # port
eureka:
  client:
    service-url:
      defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/
Copy the code

Let’s close eureka-Server for server1 and visit http://localhost:8762

Let’s start eureka-Server for server1 again and visit http://localhost:8762

You can see that our simulated high availability has been implemented