第十篇: 高可用的服务注册中心(Finchley版本)V2.0_dev

一、准备工作

Eureka通过运行多个实例,使其更具有高可用性。事实上,这是它默认的熟性,你需要做的就是给对等的实例一个合法的关联serviceurl。

二、创建eureka-server

引入依赖

<dependency>
      <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

添加配置信息

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
spring:
  application:
    name: eurka-server

运行主类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

分别启动3个eureka-server实例,
第一步:启动以第1个eureka-server实例:
-Dserver.port=8761
配置信息修改如下:

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
spring:
  application:
    name: eurka-server

第一步:启动以第1个eureka-server实例:
-Dserver.port=8762
配置信息修改如下:

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
spring:
  application:
    name: eurka-server

第三步:启动以第1个eureka-server实例:
-Dserver.port=8763
配置信息修改如下:

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/,http://localhost:8761/eureka/
spring:
  application:
    name: eurka-server

依次访问:localhost:8761、localhost:8761、localhost:8761

可以看到运行了3个eureka-serser实例

创建eureka-client工程

 <dependency>
     <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

启动主类

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceHiApplication.class, args);
    }
}

配置文件

server:
  port: 8765

spring:
  application:
    name: service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

web访问

@RestController
public class HiController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "gblfy") String name) {
        return "hi," + name + ",i am from port" + port;
    }
}

启动eureka-client,

再依次访问:localhost:8761、localhost:8761、localhost:8761

发现eureka-client已经成功注册到3个eureka-server服务端了,本次策略采取的是,eureka-server两两注册,就算其中一个宕机了,也不会影响服务的发现和注册。

当然eureka-client建议采用集群策略,以达到高可用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gblfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值