一、项目层级
二、pom依赖添加 / yml配置修改 / 修改host
-
cloud-eureka
pom.xml:
<dependencies> <!-- eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- spring boot security安全认证启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies>
application.yml
server:
port: 8887
eureka:
instance:
#eureka服务端的实例名称
hostname: eureka1.com
client:
# false表示不向注册中心注册自己
register-with-eureka: false
# false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检查服务
fetch-registry: false
#Eureka Server交互地址
service-url:
#修改为集群模式:设置服务中心地址,指向其他注册中心
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka2.com:8888/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka3.com:8889/eureka
spring:
application:
name: cloud-eureka-server1
#设置安全认证
security:
user:
name: root
password: root
-
cloud-eureka2
pom.xml
<!-- eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- spring boot security安全认证启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies>
application.yml
server:
port: 8888
eureka:
instance:
#eureka服务端的实例名称
hostname: eureka2.com
client:
# false表示不向注册中心注册自己
register-with-eureka: false
# false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检查服务
fetch-registry: false
#Eureka Server交互地址
service-url:
#修改为集群模式:设置服务中心地址,指向其他注册中心
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka1.com:8887/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka3.com:8889/eureka
spring:
application:
name: cloud-eureka-server2
security:
user:
name: root
password: root
-
cloud-eureka3
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud-parent</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.sdkj</groupId> <artifactId>cloud-eureka3</artifactId> <dependencies> <!-- eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- spring boot security安全认证启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
application.yml
server:
port: 8889
eureka:
instance:
#eureka服务端的实例名称
hostname: eureka3.com
client:
# false表示不向注册中心注册自己
register-with-eureka: false
# false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检查服务
fetch-registry: false
#Eureka Server交互地址
service-url:
#修改为集群模式:设置服务中心地址,指向其他注册中心
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka1.com:8887/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka2.com:8888/eureka
spring:
application:
name: cloud-eureka-server3
security:
user:
name: root
password: root
-
修改client端 eureka.client.service-url.defaultZone
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka1.com:8887/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka2.com:8888/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka3.com:8889/eureka
-
修改主机映射hosts文件
参考(2条消息) linux修改主机名和修改主机映射_明月&清风的博客-CSDN博客_linux修改host映射
127.0.0.1 eureka1.com
127.0.0.1 eureka2.com
127.0.0.1 eureka3.com
三、增加集群的安全认证
-
WebSecurityConfig(三个节点都要配置)
1 package com.sdkj.config; 2 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 6 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 8 /** 9 * @Author wangshuo 10 * @Date 2022/5/20, 17:16 11 * 重写WebSecurityConfigurerAdapter.configure() 12 */ 13 @EnableWebSecurity 14 @Configuration 15 public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 16 17 @Override 18 protected void configure(HttpSecurity http) throws Exception { 19 20 // Spring Security启用csrf防护后,会对post、delete、put等请求进行安全校验。 21 // 过程是,请求里必须携带crfs的tocken,如果请求里不带有tocken时,服务器会认为该请求非法,请求就会被拒绝。 22 // 默认开启,关闭csrf过滤 23 http.csrf().disable(); 24 25 http 26 .authorizeRequests() 27 .anyRequest().authenticated() 28 .and() 29 .formLogin().and() 30 .httpBasic(); 31 } 32 33 }
-
application.yml
server端(上边的yml里边写好了):
spring:
#设置安全认证
security:
user:
name: root
password: root
client端(这里也贴一下完整配置 包括下边要提到的优雅停服):
# 端口
server:
port: 8002
spring:
application:
name: cloud-order
#安全认证
security:
user:
name: root
password: root
eureka:
client:
# 表示将自己注册进Eureka Server默认为true
register-with-eureka: true
# 是否从Eureka Server抓去已有的注册信息,默认是true
fetch-registry: true
# 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
service-url:
#设置同时向三个eureka注册
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka1.com:8887/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka2.com:8888/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka3.com:8889/eureka
#开启优雅停服
#(必须通过POST请求向Eureka Client发起一个shutdown请求。请求路径为:http://ip:port/shutdown。可以通过任意技术实现,如:HTTPClient,AJAX等。)
management:
endpoint:
shutdown:
enabled: true
server:
# 端口
port: 8001
spring:
application:
name: cloud-payment-service
#安全认证
security:
user:
name: root
password: root
#数据源基本配置
datasource:
url: jdbc:mysql://localhost:3306/test_springcloud
username: root
password: 678678
#druid
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
eureka:
client:
# 表示将自己注册进Eureka Server默认为true
register-with-eureka: true
# 是否从Eureka Server抓去已有的注册信息,默认是true
fetch-registry: true
# 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
service-url:
#同时向三个节点注册
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka1.com:8887/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka2.com:8888/eureka,http://${spring.security.user.name}:${spring.security.user.password}@eureka3.com:8889/eureka
mybatis:
mapper-locations: classpath:mapping/*.xml
#开启优雅停服
#(必须通过POST请求向Eureka Client发起一个shutdown请求。请求路径为:http://ip:port/shutdown。可以通过任意技术实现,如:HTTPClient,AJAX等。)
management:
endpoint:
shutdown:
enabled: true
四、优雅停服
-
在Spring Cloud中,可以通过HTTP请求的方式,通知Eureka Client优雅停服,这个请求一旦发送到Eureka Client,那么Eureka Client会发送一个shutdown请求到Eureka Server,Eureka Server接收到这个shutdown请求后,会在服务列表中标记这个服务的状态为down,同时Eureka Client应用自动关闭。这个过程就是优雅停服。
<!--优雅停服依赖包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
五、Eureka自我保存模式
如果Eureka服务器检测到数量超过预期的注册客户端已以不正当的方式终止了它们的连接,并且同时正等待逐出,则它们将进入自我保存模式。这样做是为了确保灾难性的网络事件不会清除eureka注册表数据,并将其传播到下游的所有客户端。
-
服务保护模式
服务保护模式(自我保护模式):一般情况下,微服务在Eureka上注册后,会每30秒发送心跳包,Eureka通过心跳来判断服务时候健康,同时会定期删除超过90秒没有发送心跳服务。
导致Eureka Server接收不到心跳包的可能:一是微服务自身的原因,二是微服务与Eureka之间的网络故障。通常微服务的自身的故障只会导致个别服务出现故障,一般不会出现大面积故障,而网络故障通常会导致Eureka Server在短时间内无法收到大批心跳。虑到这个区别,Eureka设置了一个阀值,当判断挂掉的服务的数量超过阀值时,Eureka Server认为很大程度上出现了网络故障,将不再删除心跳过期的服务。
那么这个阀值是多少呢?Eureka Server在运行期间,会统计心跳失败的比例在15分钟内是否低于85%,如果低于85%,Eureka Server则任务是网络故障,不会删除心跳过期服务。
这种服务保护算法叫做Eureka Server的服务保护模式。
这种不删除的,90秒没有心跳的服务,称为无效服务,但是还是保存在服务列表中。如果Consumer到注册中心发现服务,则Eureka Server会将所有好的数据(有效服务数据)和坏的数据(无效服务数据)都返回给Consumer。
-
服务保护模式的存在必要性
因为同时保留"好数据"与"坏数据"总比丢掉任何数据要更好,当网络故障恢复后,Eureka Server会退出"自我保护模式"。
Eureka还有客户端缓存功能(也就是微服务的缓存功能)。即便Eureka Server集群中所有节点都宕机失效,微服务的Provider和Consumer都能正常通信。
微服务的负载均衡策略会自动剔除死亡的微服务节点(Robbin)。
只要Consumer不关闭,缓存始终有效,直到一个应用下的所有Provider访问都无效的时候,才会访问Eureka Server重新获取服务列表。
六、eureka常用配置项
1、通用配置
1 spring.application.name=xxxxxxxxxxxxx :应用名称配置,将会出现在 Eureka 注册中心 Application 列 2 server.port=8701 :应用端口,默认值为 8761 3 eureka.instance.hostname= server1 :服务注册中心应用实例主机名 4 eureka.instance.ip-address=127.0.0.1 :应用实例ip 5 eureka.instance.prefer-ip-address=false :客户端向注册中心注册时,相较于 hostname 是否有限使用 ip。在服务中心注册后,鼠标放到服务的 Status 列的链接上,无需点击,左下角能看出配置的变化。 6 eureka.instance.environment=dev :该实例的环境配置 7 eureka.client.register-with-eureka=false :是否将自己注册到 Eureka 注册中心。单机情况下的 Eureka Server 不需要注册,集群的 Eureka Server 以及 Eureka Client 需要注册。默认值 true 8 eureka.client.fetch-registry=false :是否需要从注册中心检索获取服务的注册信息。单机情况下的 Eureka Server 不需要获取。集群的 Eureka Server 以及 Eureka Client 需要获取。默认值 true 9 eureka.client.service-url.defaultZone= http://${spring.security.user.name}:${spring.security.user.password}@server1:8081/eureka/ :Eureka 服务的地址信息,中间的占位符为安全认证开启时使用,如果 Eureka Server 为集群状态,则逗号分隔,依次书写即可。
2、Eureka Server 配置
1 eureka.server.enable-self-preservation = false :是否开启自我保护模式,eureka server默认在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,但是在保护期内如果服务刚好这个服务提供者非正常下线了,此时服务消费者就会拿到一个无效的服务实例,此时会调用失败。默认 true 2 eureka.server.eviction-interval-timer-in-ms=10000 :扫描失效服务的时间间隔。单位 毫秒。 默认值 60 * 1000 3 security.basic.enabled=true :开启 Eureka 安全认证 4 spring.security.user.name=root :安全认证用户名 5 spring.security.user.password=123456 :安全认证密码
3、Eureka Client 配置
1 eureka.client.registry-fetch-interval-seconds=30 :客户端获取服务注册信息时间间隔,单位 秒。默认 30 2 eureka.instance.appname=eureka-client :服务名,默认取 spring.application.name 配置值,如果没有则为 unknown 3 eureka.instance.lease-expiration-duration-in-seconds=90 :服务的失效时间,失效的服务将被注册中心删除。时间间隔为最后一次注册中心接收到的心跳时间。单位 秒,默认 90 4 eureka.instance.lease-renewal-interval-in-seconds=30 :应用实例给注册中心发送心跳的间隔时间,用于表明该服务实例可用。单位 秒。默认30 5 eureka.client.eureka-server-connect-timeout-seconds=5 :client 连接 Eureka 注册中心的超时时间,单位 秒,默认 5 6 eureka.client.eureka-server-read-timeout-seconds=8 :client 对 Eureka 服务器读取信息的超时时间,单位 秒,默认 8 7 eureka.client.eureka-connection-idle-timeout-seconds=30 :client 连接 Eureka 服务端后空闲等待时间,单位 秒,默认 30 8 eureka.client.eureka-server-total-connections=200 :client 到 所有Eureka 服务端的连接总数,默认 200 9 eureka.client.eureka-server-total-connections-per-host=50 :client 到 Eureka 单服务端的连接总数,默认 50
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16294706.html