SpringCloud - (四)双节点注册中心

SpringCloud - (四)双节点注册中心

在这里插入图片描述
Euraka server one 和Euraka server two 都是注册中心,它们又相互注册。

项目总览:
在这里插入图片描述

创建eureka-server-oneeureka-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的服务。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值