In a few simple steps, build a micro service registry based on spring cloud, including several features:
-Provide registration center to be registered by service provider
-Service consumer finds the service location in the registry
-High Availability (cluster) configuration
1, the introduction of dependence:
Two dependencies need to be introduced:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
..... </dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactid>spring-cloud-dependencies</ artifactid>
<version>Camden.SR2</version>
<type>pom</type>
<scope >import</scope>
</dependency>
</dependencies>
</dependencymanagement >
2. Configuration:
In order to configure a highly available registry cluster, 3 registries are configured to spare each other. Assume that the host is:
-192.168.0.9
-192.168.0.10
-192.168.0.18
Port is 58000.
Spring:
Profiles:
active:dev
application:
name:eureka-service
server:
port:58000
Management:
port:59000
Eureka:
client:
#register-with-eureka:false
fetch-registry:false
#instance:
# Prefer-ip-address:true
---
# development environment Configuration
Spring:
profiles:dev
---
# Development Service 0 Configuration
Spring:
profiles:dev0
Eureka:
instance:
hostname:192.168.0.9
Client:
service-url:
defaultzone:http://192.168.0.10:58000/eureka/,http://192.168.0.18:58000/eureka/
---
# Development Service 1 Configuration
spring:
profiles:dev1
Eureka:
instance:
hostname:192.168.0.10
client:
service-url:
defaultzone:http://192.168.0.9:58000/eureka/,http://192.168.0.18:58000/ eureka/
---
# Development Service 2 configuration
Spring:
profiles:dev2
Eureka:
instance:
hostname: 192.168.0.18
Client:
service-url:
defaultzone:http://192.168.0.10:58000/eureka/,http:// 192.168.0.9:58000/eureka/
3, Code:
Just one main class is all you need:
@EnableEurekaServer
@SpringBootApplication Public
class Registryapplication {public
static void main ( String[] args) {
springapplication.run (registryapplication.class, args);
}
}
4. Deployment and Inspection results:
Deploy three service centers to three servers, and then open the Administration page:
Http://192.168.0.9:58000/
You can see if the individual standby (replicas) registry is available if it has started normally.