两个 Eureka 注册中心部署在同一台主机上,每个注册中心监听不同的端口。
4.0.0
com.example
Eureka
0.0.1-SNAPSHOT
jar
Eureka
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
Finchley.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
src/main/resources/application.properties:
spring.application.name=eureka-server
server.port = 1111
eureka.instance.prefer-ip-address=true
eureka.instance.ipAddress=127.0.0.1
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
#eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/,http://localhost:1112/eureka/
eureka.client.service-url.defaultZone=http://localhost:1112/eureka/,http://127.0.0.1:1111/eureka/
#eureka.instance.lease-renewal-interval-in-seconds=5
#eureka.instance.lease-expiration-duration-in-seconds=5
eureka.client.registry-fetch-interval-seconds=5
注意:
两个注册中心的主机名不能一样(我的理解是标示注册中心的主机或IP不能一致),如果主机名一样会导致两个注册中心通信出问题无法同步服务清单。由于两个注册中心在同一个主机上,所以第一个节点使用IP地址配置,第二个节点使用主机名配置。
EurekaApplication.java
package com.example.Eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
节点2的 java 项目名称是 Eurek2。
pom.xml
4.0.0
com.example
Eureka2
0.0.1-SNAPSHOT
jar
Eureka2
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
UTF-8
UTF-8
1.8
Finchley.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
src/main/resources/application.properties:
spring.application.name=eureka-server
server.port = 1112
#eureka.instance.prefer-ip-address=true
#eureka.instance.ipAddress=127.0.0.1
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
#eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/,http://localhost:1111/eureka/
eureka.client.service-url.defaultZone=http://127.0.0.1:1111/eureka/,http://localhost:1112/eureka/
#eureka.instance.lease-renewal-interval-in-seconds=5
#eureka.instance.lease-expiration-duration-in-seconds=5
eureka.client.registry-fetch-interval-seconds=5
EurekaApplication2:
package com.example.Eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication2 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication2.class, args);
}
}
pom.xml
4.0.0
com.example
server1
0.0.1-SNAPSHOT
jar
server1
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.13.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka
1.4.4.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
1.4.4.RELEASE
log4j
log4j
com.fasterxml.jackson.core
jackson-annotations
2.9.6
com.fasterxml.jackson.core
jackson-core
2.9.6
com.fasterxml.jackson.core
jackson-databind
2.9.6
org.springframework.cloud
spring-cloud-dependencies
Brixton.SR5
pom
import
org.springframework.boot
spring-boot-maven-plugin
src/main/resources/application.properties:
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1112/eureka/,http://localhost:1111/eureka/
eureka.client.registry-fetch-interval-seconds=5
HellApplication.java
package com.example.server1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class HellApplication {
public static void main(String[] args) {
SpringApplication.run(HellApplication.class, args);
System.out.println("Hell word");
}
}
HelloController.java
package com.example.server1;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(HelloController.class);
@Autowired
private DiscoveryClient client;
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String index() {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello,host:" + instance.getHost() +", service_id:"
+ instance.getServiceId());
return "Hello World";
}
}
启动两个注册中心节点,然后启动服务提供者。通过 http://localhost:1111/ 或 http://localhost:1112/访问两个服务注册中心,都可以看到服务注册信息。