1、在搭建Eureka Server,配置高可用服务注册中心,配置3个Eureka Server:

下面出现登录问题,需要参照给Eureka Server加上安全验证这篇文章:Spring Cloud (18) | 给Eureka Server加上安全验证

application.yml:

#安全验证
security:
  basic:
    enabled: true
  user:
    name: jack.ma
    password: 1qaz2wsx
spring:
  application:
    name: eureka-server-cluster
  profiles:
    active: server1

application-server1.yml:

server:
  port: 8761

eureka:
  instance:
    hostname: server1
#    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://server2:8200/eureka/,http://server3:8300/eureka/

application-server2.yml:

server:
  port: 8200
#
#spring:
#  profiles:
#    active: server1

eureka:
  instance:
    hostname: server2
#    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://server1:8761/eureka/,http://server3:8300/eureka/

application-server3.yml:

server:
  port: 8300
#
#spring:
#  profiles:
#    active: server1

eureka:
  instance:
    hostname: server3
#    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://server1:8761/eureka/,http://server2:8200/eureka/

2、因为是在本地实现的话,需要在localhost加入三个服务,需要改变etc/hosts,linux系统通过vim /etc/hosts,

windows电脑,在c:/windows/systems/drivers/etc/hosts 修改
加入:

127.0.0.1       server1
127.0.0.1       server2
127.0.0.1       server3

3、如果是在测试或者是线上环境,准备三台应用服务器就可以了

启动工程:
在elipse中启动eureka-server:

java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=server1

java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=server2


java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=server3

4、如果在idea中,可以用简单方法:

在 Edit Configurations中新建3个Spring Boot服务,选择“+“ 新建 “Spring Boot”, 分别是:eurekaServer-1,eurekaServer-2,eurekaServer-3
如图:
server1:
这里写图片描述

server2:
这里写图片描述

server3参照上图。
然后分别启动server1, server2, server3

5、需要特别注意的是:所有注册到Eureka Server的服务,defaultZone要做相应的修改如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://jack.ma:1qaz2wsx@server1:8761/eureka/,http://jack.ma:1qaz2wsx@server2:8200/eureka/,http://jack.ma:1qaz2wsx@server3:8300/eureka/

defaultZone中多个服务之间,用","连接
三个Eureka Server中,按照这个顺序来显示微服务,shutdown其中的任何一个Eureka Server, 会顺次转发到另外一个Eureka Server上

完整项目,“墙裂“推荐看:https://github.com/dangduang/Spring-Cloud-By-Dang/tree/master/spring-cloud-eureka-server

更多系列文章推荐:

  1. Spring Cloud (20) | Spring Cloud Bus 使用kafka消息总线、gitlab添加webhooks实现自动刷新配置
  2. Spring Cloud (19) | Eureka Server 高可用服务注册中心
  3. Spring Cloud (18) | 给Eureka Server加上安全验证
  4. Spring Cloud (15) | Spring Boot、HikariCP、Mybatis和MySQL 配置HikariCP数据库连接池
  5. Spring Cloud (14) | 微服务不能从git/github/gitlab中获取数据库信息 can’t load properties from git/github/gitlab
  6. Spring Cloud (12) | Spring Cloud Zuul网关调用微服务,request请求参数是application/json
  7. Spring Cloud (11) | healthcheck开启健康检查
  8. Spring Cloud (10) | Eureka 各项参数详解
  9. Spring Cloud (8) | 把Spring Boot项目改造成tomcat容器启动
  10. Spring Cloud (7) | Mongodb 微服务
  11. Spring Cloud (6) | spring cloud zuul 跨域问题No ‘Access-Control-Allow-Origin’ header
  12. Spring Cloud (5) | 配置中心 Config Server 配置git or github or gitlab
  13. Spring Cloud (3) | spring cloud bus 消息总线kafka应用
  14. Spring Cloud (1) | java.net.UnknownHostException: eureka-server
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐