springCloud学习(四)之主机映射名称修改及IP信息提示及微服务内容完善和Eureka自我保护机制和actuator监控信息完善

主机映射名称:

自定义服务端在Eureka注册中心显示的微服务名称 和IP地址提示::

application.yml添加  instance: 相关信息

eureka:
  client:
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
  instance: 
      instance-id: microservicecloud-dept8001 #注册中心显示名称
      prefer-ip-address: true #访问路径显示ip地址

 

 

微服务信息完善

服务端引入actuator依赖:

        <!-- actuator监控信息完善 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

parent项目POM文件添加build配置:

	<build>
		<finalName>microservicecloud</finalName>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<delimiters>
						<delimit>$</delimit>
					</delimiters>
				</configuration>
			</plugin>
		</plugins>
	</build>

服务端application.yml添加info:

info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  bulid.artifactId: $project.artifactId$
  bulid.version: $project.version$

完整yml:

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
spring:
   application:
    name: microservicecloud-dept                            #eureka微服务注册名称
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB01              # 数据库名称
    username: root
    password: 123456
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间
      
eureka:
  client:
    service-url:
      #defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
  instance: 
      instance-id: microservicecloud-dept8001 #注册中心显示名称
      prefer-ip-address: true #访问路径显示ip地址

info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  bulid.artifactId: $project.artifactId$
  bulid.version: $project.version$
    

配置完毕,点击注册中心服务查看信息:

点击上图红色连接,如果能显示出下图信息且和之前的info配置信息一样 则配置成功:

Eureka自我保护机制:

Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,但是在保护期内如果服务刚好这个服务提供者非正常下线了,此时服务消费者就会拿到一个无效的服务实例,此时会调用失败,对于这个问题需要服务消费者端要有一些容错机制,如重试,断路器等。

我们在单机测试的时候很容易满足心跳失败比例在 15 分钟之内低于 85%,这个时候就会触发 Eureka 的保护机制,一旦开启了保护机制,则服务注册中心维护的服务实例就不是那么准确了,此时我们可以使用eureka.server.enable-self-preservation=false来关闭保护机制,这样可以确保注册中心中不可用的实例被及时的剔除(不推荐)。

自我保护模式被激活的条件是:在 1 分钟后,Renews (last min) < Renews threshold

这两个参数的意思:

  • Renews thresholdEureka Server 期望每分钟收到客户端实例续约的总数
  • Renews (last min)Eureka Server 最后 1 分钟收到客户端实例续约的总数

具体的值,我们可以在 Eureka Server 界面可以看到:

可以看到,我们部署了 3 个 Eureka Server(自注册模式),另外,又部署 7 个服务,注册到 Eureka Server 集群,参数值分别为:

  • Renews threshold:17
  • Renews (last min):20

下面说下Renews thresholdRenews threshold具体计算方式。

Renews threshold 计算代码:

this.expectedNumberOfRenewsPerMin = count * 2;
this.numberOfRenewsPerMinThreshold = (int) (this.expectedNumberOfRenewsPerMin * serverConfig.getRenewalPercentThreshold());

count表示服务的数量,如果 Eureka Server 开启自注册模式,也算一个服务,比如我们上面的示例,count的值就是 10(3 个自注册服务 + 7 个独立服务),serverConfig.getRenewalPercentThreshold()默认是 0.85(可以通过eureka.server.renewal-percent-threshold配置)。

所以,根据上面的分析,我们可以计算出Renews threshold的值:(int)(10 * 2 * 0.85) = (int)17 = 17

Renews (last min)计算方式:count * 2,数值 2 表示每 30 秒 1 个心跳,每分钟 2 个心跳的固定频率因子,所以具体值为:10 * 2 = 20

如果在 1 分钟后,Renews (last min) < Renews threshold,默认需等待 5 分钟(可以通过eureka.server.wait-time-in-ms-when-sync-empty配置),即 5 分钟后你会看到下面的提示信息:

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

解决方式有三种:

  • 关闭自我保护模式(eureka.server.enable-self-preservation设为false),不推荐
  • 降低renewalPercentThreshold的比例(eureka.server.renewal-percent-threshold设置为0.5以下,比如0.49),不推荐
  • 部署多个 Eureka Server 并开启其客户端行为(eureka.client.register-with-eureka不要设为false,默认为true),推荐

Eureka 的自我保护模式是有意义的,该模式被激活后,它不会从注册列表中剔除因长时间没收到心跳导致租期过期的服务,而是等待修复,直到心跳恢复正常之后,它自动退出自我保护模式。这种模式旨在避免因网络分区故障导致服务不可用的问题。例如,两个客户端实例 C1 和 C2 的连通性是良好的,但是由于网络故障,C2 未能及时向 Eureka 发送心跳续约,这时候 Eureka 不能简单的将 C2 从注册表中剔除。因为如果剔除了,C1 就无法从 Eureka 服务器中获取 C2 注册的服务,但是这时候 C2 服务是可用的。

所以,Eureka 的自我保护模式最好还是开启它。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值