原来:

高可用:

高可用:服务可一直被使用,当一个坏了之后就不能用了,所以需要搭集群,他们之间相互注册
创建两个新模块eureka_server_1和eureka_server_2
![]()
添加依赖
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
- dependency>
- dependencies>
添加配置文件
- server:
- port: 8761/8762 两个server需要端口做区分
-
- #web控制台默认地址 http://localhost:8761
-
- eureka:
- instance:
- hostname: eureka-server-1 #主机名儿一样无法构成集群
- 另一个server: hostname:eureka-server_2
-
- #http://eureka-server-1:8761/eureka
- #http://eureka-server-2:8762/eureka
-
- 需要在系统host文件中把这个名字设置为回环地址
-
- client:
- service-url:
- defaultZone:http://eureka-server-1:8761/eureka
- 或者http://eureka-server-2:8762/eureka #相互注册对方地址
-
- registerWithEureka: true
- fetchRegistry: true
-
- #两个集群名字一样
- spring:
- application:
- name: eureka_server_ha
添加启动类
- package com.gao.eureka;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
-
-
- @SpringBootApplication
-
- //启用 EurekaServer
- @EnableEurekaServer
- public class EurekaApp {//两个server的启动类不能一样
- public static void main(String[] args){
- SpringApplication.run(EurekaApp.class,args);
- }
- }

consumer
- server:
- port: 9000
-
- eureka:
- instance:
- hostname: localhost
- client:
- service-url:
- #defaultZone: http://localhost:8761/eureka
- defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka # 注册
-
- spring:
- application:
- name: eureka-consumer #设置当前应用的名称。将来会在eureka中application显示,将来需要使用该名称来获取路径
provider
- server:
- port: 8000
-
-
- eureka:
- instance:
- hostname: localhost
- prefer-ip-address: true #是否将自己的ip注册到eureka中。(默认false 注册主机名)
- ip-address: 127.0.0.1 # 设置当前实例ip(之后会向server注册这个ip)
- instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} #修改instance-id显示(设置web控制台显示的 实例id)
- lease-renewal-interval-in-seconds: 30 # 每一次eureka client 向 eureka server发送心跳的时间间隔
- lease-expiration-duration-in-seconds: 90 # 如果90秒内eureka server没有收到eureka client的心跳包,则剔除该服务
-
- client:
- service-url:
- # defaultZone: http://localhost:8761/eureka
- defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka # 注册
-
- server:
- enable-self-preservation: false #关闭保护机制 默认是true
- eviction-intrerval-timer-in-ms: 3000 #检查服务的时间间隔
-
- spring:
- application:
- name: eureka-provider #设置当前应用的名称。将来会在eureka中application显示,将来需要使用该名称来获取路径

DsReplicas 为高可用
当两个server中一个坏了的时候,还可以继续使用
两个服务之间也是互通的

域名,不能含有下划线
域名可以由(a-z、A-Z 大小写等价)26个英文字母、数字(0-9)以及连接符“-”组成,但是域名的首位必须是字母或数字。对于域名的长度也有一定的限制:
国际通用顶级域名长度不得超过26个字符
中国国家顶级域名长度不得超过20个字符