SpringCloud-day04-Eureka高可用集群配置

5.4Eureka高可用集群配置

在高并发的情况下一个注册中心难以满足,因此一般需要集群配置多台。

再新建两个module  eureka-server-7002,eureka-server-7003,然后配置,最终的配置结果结构如图:

具体步骤:

第一步:每个模块的pom.xml添加如下依赖:

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.cloud</groupId>
 4             <artifactId>spring-cloud-starter-eureka-server</artifactId>
 5         </dependency>
 6         <!-- 修改后立即生效,热部署 -->
 7         <dependency>
 8             <groupId>org.springframework</groupId>
 9             <artifactId>springloaded</artifactId>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-devtools</artifactId>
14         </dependency>
15     </dependencies>

第二步:编写7002  7003的主启动类EurekaServerApplication_7002,EurekaServerApplication_7003,可以复制EurekaServerApplication_7001的启动类修改名称;

第三步:前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,这里有简单办法,直接配置本机hosts,来实现本机域名映射;

找到 C:WindowsSystem32driversetc  打开hosts,加配置 

127.0.0.1  eureka7001.wfd360.com

127.0.0.1  eureka7002.wfd360.com

127.0.0.1  eureka7003.wfd360.com

注意:在修改hosts文件时,建议先拷贝出来,修改好后再替换原来的hosts文件。

第四步:修改三个项目的application.yml文件,主要是修改 hostname和defaultZone,

7001 的 application.yml文件

server:
  port: 7001
  context-path: /

# 注册中心服务端配置
eureka:
  instance:
    #hostname: localhost #注册中心主机地址(单机)
    hostname: eureka7001.wfd360.com # 集群
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #不需要去检索服务,注册中心的职责就是维护服务实例
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置注册中心地址(单机)
      defaultZone: http://eureka7002.wfd360.com:7002/eureka/,http://eureka7003.wfd360.com:7003/eureka/ # 集群(互相注册)
View Code

7002 的 application.yml文件

server:
  port: 7002
  context-path: /

# 注册中心服务端配置
eureka:
  instance:
    #hostname: localhost #注册中心主机地址(单机)
    hostname: eureka7002.wfd360.com # 集群
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #不需要去检索服务,注册中心的职责就是维护服务实例
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置注册中心地址(单机)
      defaultZone: http://eureka7001.wfd360.com:7001/eureka/,http://eureka7003.wfd360.com:7003/eureka/ # 集群(互相注册)
View Code

7003 的 application.yml文件

server:
  port: 7003
  context-path: /

# 注册中心服务端配置
eureka:
  instance:
    #hostname: localhost #注册中心主机地址(单机)
    hostname: eureka7003.wfd360.com # 集群
  client:
    register-with-eureka: false #不向注册中心注册自己
    fetch-registry: false #不需要去检索服务,注册中心的职责就是维护服务实例
    service-url:
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置注册中心地址(单机)
      defaultZone: http://eureka7001.wfd360.com:7001/eureka/,http://eureka7002.wfd360.com:7002/eureka/ # 集群(互相注册)
View Code

第五步:修改服务提供者 ticket-provider-6001 项目的application.yml,主要修改eureka.client.service-url.defaultZone,修改后的文件如下

server:
  port: 6001
  context-path: /

# 数据源配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_station
    username: root
    password: admin
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  thymeleaf:
    cache: false

# eureka 注册中心配置
eureka:
  instance:
    hostname: localhost #eureka客户端主机实例名称
    appname: service-ticket #客户端服务名称(可以随意取)
    instance-id: service-ticket:6001 #客户端实例名称(可以随意取)
    prefer-ip-address: true #显示ip地址
  client:
    service-url:
      #defaultZone: http://localhost:7001/eureka #eureka的服务器地址(单机)
      defaultZone: http://eureka7001.wfd360.com:7001/eureka/,http://eureka7002.wfd360.com:7002/eureka/,http://eureka7003.wfd360.com:7003/eureka/ # 集群

# 服务提供者信息
info:
  version: v2
  WeChat: 851298348
  负责人: 姿势帝
View Code

第六步:测试

启动三个注册中心,以及服务提供者项目;

然后浏览器地址栏输入:http://eureka7001.wfd360.com:7001/ 

或者  http://eureka7002.wfd360.com:7002/ 

或者 http://eureka7003.wfd360.com:7003/ 

界面如下,则集群成功

这里本质是三个服务注册中心都有我们服务提供者的信息,等后面讲到服务发现和服务调用,我们通过一些策略(默认轮询),会去找对应的服务注册中心;通过集群,能减轻每个服务注册中心的压力;

eureka 集群配置就先到这里,其他的后面在补充。

springCloud课程与代码下载:https://www.cnblogs.com/newAndHui/p/13210228.html

完美!

原文地址:https://www.cnblogs.com/newAndHui/p/10621062.html