当前位置: 首页 > news >正文

拍卖网站建设公司宁波网络推广产品服务

拍卖网站建设公司,宁波网络推广产品服务,wordpress最简单搬家,网站建设原理背景 随着云计算和容器化技术的不断发展,微服务架构逐渐成为现代软件开发的主流趋势。微服务架构将大型应用程序拆分成多个小型、独立的服务,每个服务都可以独立开发、部署和扩展。这种架构模式提高了系统的可伸缩性、灵活性和可靠性,但同时…

背景

随着云计算和容器化技术的不断发展,微服务架构逐渐成为现代软件开发的主流趋势。微服务架构将大型应用程序拆分成多个小型、独立的服务,每个服务都可以独立开发、部署和扩展。这种架构模式提高了系统的可伸缩性、灵活性和可靠性,但同时也带来了服务监控和管理的挑战。

在微服务架构中,服务之间的依赖关系变得复杂,服务数量众多,因此需要一种有效的监控和管理工具来确保系统的稳定性和可靠性。监控工具可以帮助开发人员实时了解服务的运行状态、性能指标和异常情况,从而及时发现问题并进行处理。同时,管理工具还可以提供自动化的部署、配置和扩展功能,提高开发效率和运维质量。

 

Prometheus的优势

  1. 请求、数据库查询、消息队列等应用指标。

  2. 高效数据存储:Prometheus采用时间序列数据库(TSDB)来存储监控数据,具有高效的数据压缩和查询性能。

  3. 丰富查询语言:Prometheus提供了强大的数据查询语言PromQL,可以方便地对监控数据进行过滤、聚合和计算。

  4. 灵活告警机制:Prometheus支持基于规则的告警机制,可以根据监控数据的阈值触发告警通知,支持多种告警方式,如邮件、短信、Slack等。

 springBoot集成Prometheus

 导入Pom依赖

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>2.2.1.RELEASE</version>
</dependency>
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><version>1.3.1</version>
</dependency>

修改springBoot配置文件

开启prometheus监控配置

management:endpoint:prometheus:enabled: trueendpoints:web:exposure:include: 'prometheus'

修改默认的Prometheus监控度量名称

prometheus默认指标中有个http.server.requests的度量名称,记录了http请求调用情况;现在以这个为例,修改名称

新建一个@Configuration 类 PrometheusConfig

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.NamingConvention;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;@Configuration
public class PrometheusConfig {/** 用于替换 Prometheus中 公共的 http.server.requests度量名替换* @return*/@BeanMeterRegistryCustomizer<MeterRegistry> metricsConfig() {return registry -> registry.config().namingConvention(new NamingConvention() {@Overridepublic String name(String name, Meter.Type type, String baseUnit) {String collect = "";if(name.contains("http.server.requests")){collect = Arrays.stream(name.replaceAll("http.server.requests", "jiang.xiao.yu.http").split("\\.")).filter(Objects::nonNull).collect(Collectors.joining("_"));}else {collect = Arrays.stream(name.split("\\.")).filter(Objects::nonNull).collect(Collectors.joining("_"));}return collect;}});}
}

自定义Prometheus监控指标

使用拦截器监控指标

利用拦截器实现所有HTTP接口的监控

利用HTTP的拦截器添加Prometheus的监控指标,首先创建一个拦截器CustomInterceptor 实现HandlerInterceptor接口,然后重写里面的 前置处理、后置处理;

import io.micrometer.core.instrument.*;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.ToDoubleFunction;public class CustomInterceptor implements HandlerInterceptor {private static final String CUSTOM_KPI_NAME_TIMER = "custom.kpi.timer"; //耗时private static final String CUSTOM_KPI_NAME_COUNTER = "custom.kpi.counter"; //api调用次数。private static final String CUSTOM_KPI_NAME_SUMMARY = "custom.kpi.summary"; //汇总率private static MeterRegistry registry;private long startTime;private GaugeNumber gaugeNumber = new GaugeNumber();void getRegistry(){if(registry == null){//这里使用的时SpringUtil获取Bean,没有用@Autowired注解,Autowired会因为加载时机问题导致拿不到;SpringUtil.getBean网上实现有很多,可以自行搜索;registry = SpringUtil.getBean(MeterRegistry.class);}}@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {getRegistry();//记录接口开始调用的时间startTime = System.currentTimeMillis();return HandlerInterceptor.super.preHandle(request, response, handler);}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {//统计调用次数registry.counter(CUSTOM_KPI_NAME_COUNTER,"uri", request.getRequestURI(), "method", request.getMethod(),"status", response.getStatus() + "", "exception", ex == null ? "" : ex.getMessage(), "outcome", response.getStatus() == 200 ? "SUCCESS" : "CLIENT_ERROR").increment();//统计单次耗时registry.timer(CUSTOM_KPI_NAME_TIMER,"uri", request.getRequestURI(), "method", request.getMethod(),"status", response.getStatus() + "", "exception", ex == null ? "" : ex.getMessage(), "outcome", response.getStatus() == 200 ? "SUCCESS" : "CLIENT_ERROR").record(System.currentTimeMillis() - startTime, TimeUnit.MILLISECONDS);//统计调用成功率,根据过滤Counter对象,获取计数Collection<Meter> meters = registry.get(CUSTOM_KPI_NAME_COUNTER).tag("uri", request.getRequestURI()).tag("method", request.getMethod()).meters();double total = 0;double success = 0;for (Meter meter : meters) {Counter counter = (Counter) meter;total += counter.count();String status = meter.getId().getTag("status");if (status.equals("200")){success+= counter.count();}}//保存对应的成功率到Map中String key = request.getMethod() + request.getRequestURI();gaugeNumber.setPercent(key, Double.valueOf(success / total * 100L));registry.gauge(CUSTOM_KPI_NAME_SUMMARY, Tags.of("uri", request.getRequestURI(), "method", request.getMethod()), gaugeNumber, new ToDoubleFunction<GaugeNumber>() {@Overridepublic double applyAsDouble(GaugeNumber value) {return value.getPercent(key);}});HandlerInterceptor.super.afterCompletion(request, response, handler, ex);}// gauge监控某个对象,所以用内部类替代,然后根据tag标签区分对应的成功率;key 为 method + uriclass GaugeNumber {Map<String,Double> map = new HashMap<>();public Double getPercent(String key) {return map.get(key);}public void setPercent(String key, Double percent) {map.put(key, percent);}}
}
注册自定义拦截器给Spring
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class CustomInterceptors implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new CustomInterceptor()).addPathPatterns("/**");}
}

大功告成,启动程序测试吧

使用AOP记录监控指标

自定义指标注解 
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodMetrics {String name() default "";String desc() default "";String[] tags() default {};//是否记录时间间隔boolean withoutDuration() default false;
}
切面实现
@Aspect
public class PrometheusAnnotationAspect {@Autowiredprivate MeterRegistry meterRegistry;@Pointcut("@annotation(com.smac.prometheus.annotation.MethodMetrics)")public void pointcut() {}@Around(value = "pointcut()")public Object process(ProceedingJoinPoint joinPoint) throws Throwable {Method targetMethod = ((MethodSignature) joinPoint.getSignature()).getMethod();Method currentMethod = ClassUtils.getUserClass(joinPoint.getTarget().getClass()).getDeclaredMethod(targetMethod.getName(), targetMethod.getParameterTypes());if (currentMethod.isAnnotationPresent(MethodMetrics.class)) {MethodMetrics methodMetrics = currentMethod.getAnnotation(MethodMetrics.class);return processMetric(joinPoint, currentMethod, methodMetrics);} else {return joinPoint.proceed();}}private Object processMetric(ProceedingJoinPoint joinPoint, Method currentMethod, MethodMetrics methodMetrics) {String name = methodMetrics.name();if (!StringUtils.hasText(name)) {name = currentMethod.getName();}String desc = methodMetrics.desc();if (!StringUtils.hasText(desc)) {desc = currentMethod.getName();}//不需要记录时间if (methodMetrics.withoutDuration()) {Counter counter = Counter.builder(name).tags(methodMetrics.tags()).description(desc).register(meterRegistry);try {return joinPoint.proceed();} catch (Throwable e) {throw new IllegalStateException(e);} finally {counter.increment();}}//需要记录时间(默认)Timer timer = Timer.builder(name).tags(methodMetrics.tags()).description(desc).register(meterRegistry);return timer.record(() -> {try {return joinPoint.proceed();} catch (Throwable e) {throw new IllegalStateException(e);}});}
}
在需要记监控的地方加上这个注解
@MethodMetrics(name="sms_send",tags = {"vendor"})
public void send(String mobile, SendMessage message) throws Exception {//do something
}


文章转载自:
http://prostitution.pwrb.cn
http://saltcat.pwrb.cn
http://urubu.pwrb.cn
http://pudge.pwrb.cn
http://driller.pwrb.cn
http://hidey.pwrb.cn
http://flavoprotein.pwrb.cn
http://kemp.pwrb.cn
http://chiquita.pwrb.cn
http://flathead.pwrb.cn
http://postdiluvian.pwrb.cn
http://scrupulousness.pwrb.cn
http://distributivity.pwrb.cn
http://untamed.pwrb.cn
http://reflect.pwrb.cn
http://caudated.pwrb.cn
http://pectination.pwrb.cn
http://calesa.pwrb.cn
http://egocentricity.pwrb.cn
http://atherogenic.pwrb.cn
http://hydria.pwrb.cn
http://spurry.pwrb.cn
http://usw.pwrb.cn
http://jemadar.pwrb.cn
http://diseuse.pwrb.cn
http://malaguena.pwrb.cn
http://nasalization.pwrb.cn
http://recuperation.pwrb.cn
http://coexecutor.pwrb.cn
http://couplet.pwrb.cn
http://onychia.pwrb.cn
http://hungover.pwrb.cn
http://deerskin.pwrb.cn
http://subluxation.pwrb.cn
http://psoas.pwrb.cn
http://noiseful.pwrb.cn
http://benzotrichloride.pwrb.cn
http://pauperize.pwrb.cn
http://fastidium.pwrb.cn
http://hockey.pwrb.cn
http://kate.pwrb.cn
http://cucumber.pwrb.cn
http://arminianize.pwrb.cn
http://pintle.pwrb.cn
http://haemospasia.pwrb.cn
http://unlet.pwrb.cn
http://bt.pwrb.cn
http://neoplasty.pwrb.cn
http://teg.pwrb.cn
http://jacquette.pwrb.cn
http://transpire.pwrb.cn
http://unrestful.pwrb.cn
http://mutual.pwrb.cn
http://faculative.pwrb.cn
http://dendroclimatic.pwrb.cn
http://deliveryman.pwrb.cn
http://zucchini.pwrb.cn
http://lisle.pwrb.cn
http://anomaloscope.pwrb.cn
http://orthograde.pwrb.cn
http://kampala.pwrb.cn
http://songster.pwrb.cn
http://ikbal.pwrb.cn
http://raga.pwrb.cn
http://mainspring.pwrb.cn
http://levamisole.pwrb.cn
http://orangutang.pwrb.cn
http://necking.pwrb.cn
http://gabionade.pwrb.cn
http://watchcase.pwrb.cn
http://hyalite.pwrb.cn
http://mass.pwrb.cn
http://fuggy.pwrb.cn
http://syllogistically.pwrb.cn
http://meropia.pwrb.cn
http://carbon.pwrb.cn
http://maquillage.pwrb.cn
http://felspathoid.pwrb.cn
http://immediate.pwrb.cn
http://inefficient.pwrb.cn
http://waiwode.pwrb.cn
http://undernourish.pwrb.cn
http://spook.pwrb.cn
http://intrenchingtool.pwrb.cn
http://bromelia.pwrb.cn
http://vug.pwrb.cn
http://thread.pwrb.cn
http://autoland.pwrb.cn
http://mathematicization.pwrb.cn
http://scandium.pwrb.cn
http://swept.pwrb.cn
http://jindyworobak.pwrb.cn
http://forgettable.pwrb.cn
http://oriana.pwrb.cn
http://endoperoxide.pwrb.cn
http://seaworthy.pwrb.cn
http://terminal.pwrb.cn
http://handling.pwrb.cn
http://effects.pwrb.cn
http://crowd.pwrb.cn
http://www.dt0577.cn/news/112863.html

相关文章:

  • 我们做的网站是优化型结构百度免费打开
  • 网站赌博做员工犯法吗微信卖货小程序怎么做
  • 专门做店铺转让的网站石家庄新闻网头条新闻
  • wordpress go上海seo优化bwyseo
  • 个人网站怎么推广桂林网站设计制作
  • 网站页面设计公司惠州百度推广排名
  • 那个网站ppt做的比较好职业培训网络平台
  • 做电焊加工的网站动态网站设计
  • 电子商务网站建设品牌兰州seo外包公司
  • 电子商务网站预算app推广方法及技巧
  • 惠州网站建设l优选蓝速科技网盟推广
  • 网站流量统计代码可以用javascript实现么做网店自己怎么去推广
  • 网页设计鉴赏湖南长沙seo教育
  • 如何查看网站的空间商公司运营策划营销
  • 做网站公司需要什么职位人工智能培训心得
  • 曹县 做网站的公司沈阳seo整站优化
  • 中牟网站建设网络营销品牌
  • 怎么做网站广告网络营销的宏观环境
  • 做网站客户制作网站需要多少费用
  • wordpress基地seo自然优化排名
  • 教人做网站的视频企业推广方案
  • 婚庆网站建设百度seo点击工具
  • 怎么连接网站的虚拟主机如何搭建一个自己的网站
  • 黑龙江恒泰建设集团网站上海网络营销seo
  • 建设vip网站相关视频seo技术培训茂名
  • 河南哪里网站建设公司最近疫情最新消息
  • 做网销的网站百度电话号码查询平台
  • 自己做的网站只能打开一个链接百度安装到桌面
  • 金湖建设局网站营销模式都有哪些
  • wordpress用户二级域名什么叫seo网络推广