On the basis of the previous article, realize the Eureka simple cluster configuration to achieve high availability requirements. Eureka the cluster logic, and zookeeper the difference, do not do too much introduction
First, modify the "Ershuai-eureka-server" of the Application.yml
Spring:
application:
name:ershuai-eureka-server
Profiles:
active:1001 # This specifies the application-1001/to start 1002/1003.yml
second, the new "Application-1001/1002/1003.yml"
Register-with-eureka, fetch-registry default is True
Server:
port:1001
Eureka:
client:
service-url:
Defaultzone:http://localhost:1002/eureka, Http://localhost:1003/eureka
#register-with-eureka:false # Whether to register itself to the Eureka server
#fetch-registry:false # Whether to get registration information from Eureka
instance:
hostname:localhost
Instance-id: ${spring.cloud.client.ipaddress}:${ Server.port} # Specifies that IP prefer-ip-address:true # for this instance is
registered using IP instead of hostname
#server:
#enable-self-preservation: False # Turn on the protection mechanism
Server:
port:1002
Eureka:
client:
service-url:
Defaultzone:http://localhost:1001/eureka, Http://localhost:1003/eureka
#register-with-eureka:false # Whether to register itself to the Eureka server
#fetch-registry:false # Whether to get registration information from Eureka
instance:
hostname:localhost
Instance-id: ${spring.cloud.client.ipaddress}:${ Server.port} # Specifies that IP prefer-ip-address:true # for this instance is
registered using IP instead of hostname
#server:
#enable-self-preservation: False # Turn on the protection mechanism
Server:
port:1003
Eureka:
client:
service-url:
defaultzone:http://localhost:1001/ Eureka,http://localhost:1002/eureka
#register-with-eureka:false # Whether to register itself to the Eureka server
#fetch-registry: False # whether to get registration information from Eureka
instance:
hostname:localhost
Instance-id: ${spring.cloud.client.ipaddress }:${server.port} # Specifies that IP prefer-ip-address:true # for this instance is
registered using IP instead of hostname
#server:
# Enable-self-preservation:false # Open protection mechanism
Third, start the 1001/1002/1003 port service separately
Project right-click or Main method right---run as--configurations-Arguments
Set Name (custom)
Set Program arguments parameter:--spring.profiles.active=1001
Click "Run" to start
This 1001/1002/1003 is started separately, need run as three times, each start a port service
Access http://localhost:1001/1002/1003 Ports separately
You can see that the three-port services are registered with each other.
Iv. Registration of services
Create a "ershuai-eureka-client" sub-project
Pom.xml file
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency >
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-actuator</artifactid>
</dependency>
</dependencies>
< build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid >
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${ spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
APPLICATION.YML file
Spring:
application:
name:ershuai-eureka-client
server:
port:9001
Eureka:
client:
Service-url:
defaultzone:http://localhost:1001/eureka,http://localhost:1002/eureka,http://localhost:1003/ Eureka # Multiple separated by ', '. You can also just fill in a
#register-with-eureka:false # Whether to register itself to Eureka server
#fetch-registry:false # Whether to get registration information
from Eureka Instance:
Instance-id: ${spring.cloud.client.ipaddress}:${server.port} # Specifies the IP prefer-ip-address for this instance
: True # use IP instead of host name when registering
Test controller
@RestController public
class TestController {
@Autowired
discoveryclient discoveryclient;
@GetMapping ("/test") public
String test () {
String services = "Services:" + this.discoveryClient.getServices ();
SYSTEM.OUT.PRINTLN (services);
return services;
}
}
Main start
@EnableDiscoveryClient
@SpringBootApplication Public
class Eurekaclientapplication {public
static void Main (string[] args) {
springapplication.run (eurekaclientapplication.class, args);
}
}
Visit http://localhost:1001/1002/1003 again and you can see that 9001 is registered to Eureka
Visit http://localhost:9001/test
You can see that the service is OK
Try to shut down 1001/1002/1003, any one or two services, Access Http://localhost:9001/test, still can run normally