SpringCloud搭建Eureka集群

介绍

上一篇讲了下如何搭建Eureka服务注册中心,那个是单机版的,本篇介绍下集群版搭建。

搭建Eureka集群

1.新建模块:cloud-eureka-server7002

参考cloud-eureka-server7001新建模块cloud-eureka-server7002。

2.修改hosts文件

路径:C:\Windows\System32\drivers\etc\hosts,在最后面追加以下内容:

# spring cloud
127.0.0.1  eureka7001.com
127.0.0.1  eureka7002.com

3.修改yml

  • 修改模块7001的配置
server:
  port: 7001
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/
  • 修改模块7002的配置
server:
  port: 7002
eureka:
  instance:
    hostname: eureka7002.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
  • 修改cloud-provider-payment8001的配置
server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/db_payment?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root123
    druid:
      test-while-idle: true
      validation-query: SELECT 1
mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.sgtech.springcloud.entity    # 所有Entity别名类所在包

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机版
      # defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001

4.新建模块:cloud-payment8002

参考模块:cloud-payment8001新建cloud-payment8002。

  • 修改配置
server:
  port: 8002

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/db_payment?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root123
    druid:
      test-while-idle: true
      validation-query: SELECT 1
mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.sgtech.springcloud.entity    # 所有Entity别名类所在包

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      #单机版
      # defaultZone: http://localhost:7001/eureka
      # 集群版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

5.测试

  • 启动配置中心的2个服务:7001和7002
  • 启动cloud-provider-payment8001
  • 分别访问注册中心7001和7002:http://eureka7001.com:7001/http://eureka7002.com:7002/会发现多了一个注册中心副本和2个实例

在这里插入图片描述

  • 访问8001接口:http://localhost:8001/payment/get/22http://localhost:8002/payment/get/22

在这里插入图片描述

项目代码

https://gitee.com/indexman/cloudstudy

猜你喜欢

转载自blog.csdn.net/IndexMan/article/details/119207080