Springboot监控之一:SpringBoot四大神器之Actuator之2--覆盖修改spring cloud的默认的consul健康检查规则
支付项目中,调用银行的支付接口时,协议不同时:

出口网关:与外围银行对接的模块(出口网关)是socket长连接与支付公司对接,该网关需要提供http接口给内部系统调用,当socket没有建立连接时(网关服务的高可用是haProxy搭建的,有些服务的socket可能未连上支付公司),此时网关的http服务不让内部其它调用系统发现。(因为,出口网关没有存储和缓存,而且交易事件必须实时的完成,返回给trade等业务系统)
gradle构建的spring cloud项目
build.gradle中增加:
compile 'org.springframework.retry:spring-retry:1.1.2.RELEASE'
compile 'org.springframework.boot:spring-boot-actuator:1.4.5.RELEASE'
工程结构:

将spring-cloud-consul-core中的有关健康检查的两个java文件按如下修改后,再拷贝到自己的工程中如上图。
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.cloud.consul; import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.actuate.autoconfigure.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Retryable;
import org.springframework.retry.interceptor.RetryInterceptorBuilder;
import org.springframework.retry.interceptor.RetryOperationsInterceptor; import com.ecwid.consul.v1.ConsulClient; /**
* @author Spencer Gibb
*/
@Configuration
@EnableConfigurationProperties
@ConditionalOnConsulEnabled
public class ConsulAutoConfiguration { @Bean
@ConditionalOnMissingBean
public ConsulProperties consulProperties() {
return new ConsulProperties();
} @Bean
@ConditionalOnMissingBean
public ConsulClient consulClient(ConsulProperties consulProperties) {
return new ConsulClient(consulProperties.getHost(), consulProperties.getPort());
} @Configuration
@ConditionalOnClass(Endpoint.class)
protected static class ConsulHealthConfig { @Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint("consul")
public ConsulEndpoint consulEndpoint(ConsulClient consulClient) {
return new ConsulEndpoint(consulClient);
} @Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledHealthIndicator("consul")
public PosConsulHealthIndicator consulHealthIndicator(ConsulClient consulClient) {
return new PosConsulHealthIndicator(consulClient);
}
} @ConditionalOnClass({ Retryable.class, Aspect.class, AopAutoConfiguration.class })
@Configuration
@EnableRetry(proxyTargetClass = true)
@Import(AopAutoConfiguration.class)
@EnableConfigurationProperties(RetryProperties.class)
protected static class RetryConfiguration { @Bean(name = "consulRetryInterceptor")
@ConditionalOnMissingBean(name = "consulRetryInterceptor")
public RetryOperationsInterceptor consulRetryInterceptor(
RetryProperties properties) {
return RetryInterceptorBuilder
.stateless()
.backOffOptions(properties.getInitialInterval(),
properties.getMultiplier(), properties.getMaxInterval())
.maxAttempts(properties.getMaxAttempts()).build();
}
}
}
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.cloud.consul; import java.util.List;
import java.util.Map; import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health; import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.Self;
import com.ecwid.consul.v1.agent.model.Self.Config;
import com.dxz.IoSessionHelper;
import com.dxz.utils.SpringUtil; /**
* @author Spencer Gibb
*/
public class PosConsulHealthIndicator extends AbstractHealthIndicator { private ConsulClient consul; private IoSessionHelper ioSessionHelper; public PosConsulHealthIndicator(ConsulClient consul) {
this.consul = consul;
} private IoSessionHelper getIoSessionHelper() {
if(null == ioSessionHelper) {
ioSessionHelper = SpringUtil.getBean("ioSessionHelper", IoSessionHelper.class);
}
return ioSessionHelper;
} private boolean ioSessionCheck() {
return getIoSessionHelper().ioSessionCheck();
} @Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
try {
Response<Self> self = consul.getAgentSelf();
Config config = self.getValue().getConfig();
Response<Map<String, List<String>>> services = consul
.getCatalogServices(QueryParams.DEFAULT);
builder
.withDetail("services", services.getValue())
.withDetail("advertiseAddress", config.getAdvertiseAddress())
.withDetail("datacenter", config.getDatacenter())
.withDetail("domain", config.getDomain())
.withDetail("nodeName", config.getNodeName())
.withDetail("bindAddress", config.getBindAddress())
.withDetail("clientAddress", config.getClientAddress()); if(ioSessionCheck()) {
builder.up();
} else {
builder.outOfService()
.withDetail("description", "ioSession not available");
} }
catch (Exception e) {
builder.down(e);
}
}
}
覆盖doHealthCheck()方法,增加socket连接校验返回健康检查结果给consul,其它微服务客户端感知到哪个网关服务是可用的。
Springboot监控之一:SpringBoot四大神器之Actuator之2--覆盖修改spring cloud的默认的consul健康检查规则的更多相关文章
- Springboot监控之一:SpringBoot四大神器之Actuator之3-springBoot的监控和管理--指标说明
Spring Boot包含很多其他的特性,它们可以帮你监控和管理发布到生产环境的应用.你可以选择使用HTTP端点,JMX或远程shell(SSH或Telnet)来管理和监控应用.审计(Auditing ...
- Springboot监控之一:SpringBoot四大神器之Actuator之2--springboot健康检查
Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring Boot 内置了一些 HealthIndicator. ...
- Springboot监控之一:SpringBoot四大神器之Actuator之2--spring boot健康检查对Redis的连接检查的调整
因为项目里面用到了redis集群,但并不是用spring boot的配置方式,启动后项目健康检查老是检查redis的时候状态为down,导致注册到eureka后项目状态也是down.问下能不能设置sp ...
- Spring Boot]SpringBoot四大神器之Actuator
论文转载自博客: https://blog.csdn.net/Dreamhai/article/details/81077903 https://bigjar.github.io/2018/08/19 ...
- Springboot监控之一:SpringBoot四大神器之Actuator
介绍 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供 ...
- SpringBoot四大神器之Actuator
介绍 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供 ...
- SpringBoot四大神器之Starter
SpringBoot的starter主要用来简化依赖用的.本文主要分两部分,一部分是列出一些starter的依赖,另一部分是教你自己写一个starter. 部分starters的依赖 Starter( ...
- SpringBoot四大神器之auto-configuration
SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...
- Springboot整合Spring Cloud Kubernetes读取ConfigMap,支持自动刷新配置
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Docker & Kubernetes相关文章:容器技术 之前介绍了Spring Cloud Config的用法,但 ...
随机推荐
- 软件工程 week 01
一.安装与使用Git First项目地址: https://git.coding.net/kefei101/First.git 二.针对以下三个问题,作为大三新生,谈谈我的感想 问题1:你为什么选择计 ...
- rsync命令
1.rsync命令(文件同步工具,可以理解为动态备份): rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsy ...
- 学习笔记TF014:卷积层、激活函数、池化层、归一化层、高级层
CNN神经网络架构至少包含一个卷积层 (tf.nn.conv2d).单层CNN检测边缘.图像识别分类,使用不同层类型支持卷积层,减少过拟合,加速训练过程,降低内存占用率. TensorFlow加速所有 ...
- Myelipse中xml约束文件的导入(以spring为例)
为了在电脑处于未联网状态下,beans.xml中书写标签具有提示功能,需要在电脑本地导入约束文件,下面上图 注意:将location后缀添加到key中beans的后面 注意:导入 context,ao ...
- vue全家桶+Koa2开发笔记(1)--vuex
1. 安装webpack的问题: webpack坑系列--安装webpack-cli 2. vue-cli(vue脚手架)超详细教程 3. 在命令行中使用 touch 执行新建文件: 4. 关 ...
- MongoDB高可用集群搭建(主从、分片、路由、安全验证)
目录 一.环境准备 1.部署图 2.模块介绍 3.服务器准备 二.环境变量 1.准备三台集群 2.安装解压 3.配置环境变量 三.集群搭建 1.新建配置目录 2.修改配置文件 3.分发其他节点 4.批 ...
- MySQL--Checkpoint基础
===================================================== Checkpint 分两种:Sharp Checkpoint : 在服务器正常关闭时,将所有 ...
- Where关键词的用法
where(泛型类型约束) where关键词一个最重要的用法就是在泛型的声明.定义中做出约束. 约束又分为接口约束.基类约束.构造函数约束.函数方法的约束,我们慢慢介绍. 接口约束 顾名思义,泛型参数 ...
- Git SVN Clone 旧项目迁移到 Git 上
Git SVN Clone 旧项目迁移到 Git 上 很久使用的是 SVN,但由于项目重启,想改为 Git. 之前的 SVN 仓库是本地,所以在 git svn clone 一直不成功. 正确的方式: ...
- dojo:为数据表格添加复选框
一.添加复选框 此时应该选用EnhancedGrid,而不是普通的DataGrid.添加复选框需要设置EnhancedGrid的plugins属性,如下: gridLayout =[{ default ...