Euraka server one 和Euraka server two 都是注册中心,它们又相互注册。
创建eureka-server-one和eureka-server-two这两个注册中心。
eureka-server-one的配置文件application.yml:
# 配置服务器端口
server:
port: 8001
# 配置eureka实例主机名
eureka:
instance:
hostname: eureka-server-one
# 表明是eureka server
client:
register-with-eureka: false
fetch-registry: false
# 设置默认的eureka server访问url
# 默认情况下erureka server也是一个eureka client ,必须要指定一个 server
service-url:
defaultZone: http://localhost:8002/eureka/
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
eureka-server-two的配置文件application.yml:
# 配置服务器端口
server:
port: 8002
# 配置eureka实例主机名
eureka:
instance:
hostname: eureka-server-two
# 表明是eureka server
client:
register-with-eureka: false
fetch-registry: false
# 设置默认的eureka server访问url
# 默认情况下erureka server也是一个eureka client ,必须要指定一个 server
service-url:
defaultZone: http://localhost:8001/eureka/
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
目的是为了让eureka-server-one 和 eureka-server-two 相互注册。
修改eureka-provider-one的配置文件:
# 配置服务器端口
server:
port: 8910
spring:
application:
name: cloud-eureka-provider
# 注册服务
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
目的是让eureka-provider-one 连上注册中心eureka-server-one
修改eureka-provider-two的配置文件:
# 配置服务器端口
server:
port: 8911
spring:
application:
name: cloud-eureka-provider
# 注册服务
eureka:
client:
service-url:
defaultZone: http://localhost:8002/eureka/
目的是让eureka-provider-two 连上注册中心eureka-server-two
在eureka-provider-two 中的HelloController新加一个方法two()如下:
package com.wy.eurekaprovidertwo.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/index/{name}")
public String index(@PathVariable("name") String name) {
return "hello "+name+",this is second messge";
}
@RequestMapping("/test")
public String index() {
return "hello this is second messge";
}
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",this is second messge";
}
@RequestMapping("/two")
public String two(@RequestParam String name) {
return "hi "+name+",this is second messge";
}
}
修改eureka-consumer-feign的配置文件:
# 配置服务器端口
server:
port: 8921
spring:
application:
name: cloud-eureka-consumer-feign
# 注册服务
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
目的是让eureka-consumer-feign 连上注册中心eureka-server-one
接下来我们来调用eureka-provider-two中的two()方法,如果调用成功,说明双节点注册中心搭建成功。
在eureka-consumer-feign 中的HelloService接口新加two接口,如下:
package com.wy.eurekaconsumerfeign.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "CLOUD-EUREKA-PROVIDER")
public interface HelloService {
@RequestMapping(value = "/hello/hello")
String hello(@RequestParam(value = "name") String name);
@RequestMapping(value = "/hello/test")
String test();
@RequestMapping(value = "/hello/two")
String two(@RequestParam(value = "name") String name);
}
在TestController中调用这个方法,代码如下:
package com.wy.eurekaconsumerfeign.controller;
import com.wy.eurekaconsumerfeign.remote.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/feign")
public class TestController {
@Autowired
HelloService helloService;
@RequestMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
return helloService.hello(name);
}
@RequestMapping("/test")
public String index() {
return helloService.test();
}
@RequestMapping("/two/{name}")
public String two(@PathVariable("name") String name) {
return helloService.two(name);
}
}
一次启动eureka-server-one、eureka-server-two、eureka-provider-one、eureka-provider-two、eureka-consumer-feign。
访问http://localhost:8921/feign/two/wy
到这里说明双节点注册中心搭建成功。
这时,我们访问http://localhost:8001
我们访问http://localhost:8002
我们发现两个注册中心都有eureka-server-one和eureka-server-two提供的服务。因此eureka-consumer-feign只连上eureka-provider-one,也能调用eureka-provider-two的服务。