Spring Cloud入门二:eureka集群

Spring cloud eureka集群

Eureka帮助我们在spring cloud上进行服务注册和服务发现。但是,对于很多项目来说,只对服务在一个Spring Cloud Server注册是不合理的。当Server当掉时,服务将全部不能被发现。因此,我们在这里介绍对eureka进行集群,服务进行多注册。


多服务Server

  • 新建两个Spring cloud Server项目,pom中添加依赖。
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
spring:
  application:
    name: eureka-server
server:
  port: 1110
eureka:
  instance:
    hostname: peer1
  client:
    service-url:
      defaultZone: http://peer2:1111/eureka/
  application-peer2.yml

spring:
  application:
    name: eureka-server
server:
  port: 1111
eureka:
  instance:
    hostname: peer2
  client:
    service-url:
      defaultZone: http://peer1:1110/eureka/
  • 显然这里http://peer1:1110http://peer2:1111的peer1和peer2代表两个注册中心的ip地址,此时是不可用的。我们需要修改host文件,添加以下行:
127.0.0.1 peer1 
127.0.0.1 peer2
  • 在两个项目的application.yml分别激活其中一个配置文件
spring:
  profiles:
    active: peer1/peer2
  • 在Application.java文件中,加入@EnableEurekaServer开启服务中心的功能。
  • 启动项目,分别访问peer1:1110, peer2:1111
    peer1
    peer2

Eureka Client

  • 新建Spring cloud Eureka client项目,pom中添加依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
spring:
  application:
    name: eureka-client
server:
  port: 2222
eureka:
  client:
    service-url:
      defaultZone: http://peer2:1111/eureka/
  • 在Application.java添加@EnableDiscoveryClient注册为client项目。
  • 启动项目,分别访问服务注册中心。
    peer1
    这里写图片描述
  • 关闭client注册到的peer2:1111,访问peer1,可以看到服务依然注册到。服务不会因为其中一个注册中心shutdown而关闭。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值