程序员的公众号:源1024,获取更多资料,无加密无套路!
最近整理了一份大厂面试资料《史上最全大厂面试题》,Springboot、微服务、算法、数据结构、Zookeeper、Mybatis、Dubbo、linux、Kafka、Elasticsearch、数据库等等
获取方式: 关注公众号并回复 666 领取,更多内容持续奉上
搭建prometheus环境监控Java应用服务,发现报错:
expected a valid start token, got "\"" ("INVALID") while parsing:
请求http://localhost:8080/actuator/prometheus:格式错乱
正常格式应该如下:
经排查发现是因为项目中配置了fastjson转换导致
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty);
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastConverter.setFastJsonConfig(fastJsonConfig);
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.ALL);
fastConverter.setSupportedMediaTypes(supportedMediaTypes);
converters.add(fastConverter);
}
}
解决方案:
- 注释上面转换代码(简单粗暴,如果影响别的地方,建议采用第二种方案)
- 配置自定义接口进行反序列化成原来格式
@RestController
@RequestMapping("prometheus")
public class PrometheusController {
@Value("${server.servlet.context-path}")
private String contextPath;
@Value("${server.port}")
private Integer port;
@Value("${prometheus.url}")
private Integer url;
@GetMapping(path = "/metrics", produces = MediaType.TEXT_PLAIN_VALUE)
public void metrics(HttpServletResponse response) {
RestTemplate restTemplate = new RestTemplate();
StringBuilder prometheusUrl = new StringBuilder(url);
prometheusUrl.append(port);
prometheusUrl.append(contextPath);
prometheusUrl.append("/actuator/prometheus");
ResponseEntity<String> responseEntity = restTemplate.getForEntity(prometheusUrl.toString(), String.class);
String body = responseEntity.getBody();
String s = JSON.parseObject(body, String.class);
try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
bos.write(s.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
prometheus.yml配置
- job_name: "demo-boot"
metrics_path: "actuator/prometheus"
static_configs:
- targets: ["localhost:8080"]
改成:
- job_name: "demo-boot"
metrics_path: "prometheus/metrics"
static_configs:
- targets: ["localhost:8080"]
重启prometheus:
系列文章索引
MyBatis的插件能在哪些地方进行拦截?
了解MyBatis的缓存机制吗
面试官:谈谈对volatile的理解
Spring中用到了哪些设计模式
面试官:说一下SQL的执行过程
线程池的工作原理