spring - 使用千分尺将 POST /** 请求 URL 解析为完整请求 URL
问题描述
使用微服务架构,我编写了一个通用的 POST 请求处理程序,它被所有微服务使用。spring 中的 post 映射如下所示:
@RestController
@RequestMapping(value = "/v1/", consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
public class V1Controller {
@PostMapping(path = "/**")
public @ResponseBody Json post () {}
}
现在,当我使用千分尺使用此端点的指标时,我只将/v1/作为指标中的端点,而我正在从调用服务发送完整的 URL,例如/v1/demo/foo 。我尝试了很多组合,但它不起作用。我还添加了我列出的 WebMvcTagsProvider 以请求和解决 POST api 调用。
@Bean
@SuppressWarnings("unchecked")
public WebMvcTagsProvider webMvcTagsProvider(ObjectMapper objectMapper) {
return new DefaultWebMvcTagsProvider() {
public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
if ("POST".equals(request.getMethod())) {
Tag uriTag = Tag.of("uri", String.valueOf(request.getRequestURI()));
return Tags.of(WebMvcTags.method(request), uriTag, WebMvcTags.exception(exception), WebMvcTags.status(response));
}
return Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, response), WebMvcTags.exception(exception), WebMvcTags.status(response));
}
};
}
它仍然解析为指标中的/v1/ URL。我尝试了很多谷歌搜索,但没有找到任何解决方案。提前致谢。
解决方案
基于 Spring Boot RequestMapping 的构建在注释上匹配并将它们添加为标签。
这是为了避免标签爆炸。想象一下,@RequestMapping
对于类似的路径user/{userId}
,您希望将所有这些调用组合在一起(user/1、user/2、user/3)。
您需要Timer
在您的 post 方法中创建您自己的设置 URL 标签等。
如果您决定重用与内置 Spring Boot 指标相同的指标名称,您也需要禁用该指标名称,因此您不会重复计算这些请求。
推荐阅读
- r - 如何将多个字母连接成一个唯一的字符串?
- jenkins - 使用相应的参数触发多个下游作业
- java - Spark SQL:将JSON格式的毫秒时间戳转换为日期格式
- java - 当 VM 选项设置为一个目录时 JavaFX 程序不运行,但设置为具有相同文件的另一个目录时运行良好
- hive - 如何将分区添加到在 Amazon EMR 中运行的 Presto 中的分区表?
- amazon-web-services - AWS Cloudformation 模板:哪个部分?
- r - 在 R 中加入跨国表
- angular - 我如何在 Angular 6 中预览文件(文档和图像)?
- database - 迁移自引用外键问题
- c - 生成原始比特流作为输出到另一个软件中的管道