当前位置: 首页 > 知识库问答 >
问题:

Spring Boot:计算页面视图-执行器

左康安
2023-03-14

我要求计算每个endpoint上的视图。其思想是为所有endpoint创建一个公共的请求计数映射,该映射应该基于动态输入的endpoint返回视图计数。

假设有人想检查http://localhost:8080/user/101上的视图计数。

    null

我一直在讨论如何将动态请求发送到http://localhost:8080/actulator/metrics/http.server.requests?tag=uri:/user/101,并返回它的响应并获得计数值

@RequestMapping(path="/admin/count",method=RequestMethod.POST)
public JSONObject count(@RequestParam(name="url") final String url)//@PathVariable(name="url") final String url
{   
    String finalURL = "http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:" + url + "";
    return sendRequestToURL(finalURL);  
}
@RequestMapping(path="/{finalURL}",method=RequestMethod.GET)
public JSONObject sendRequestToURL(@PathVariable("finalURL") String url)
{
    //How to return the response Here
}

这是直接激发URL时得到的结果

获取:http://localhost:8080/acturet/metrics/http.server.requests?tag=uri:/user/101

  {
    "name": "http.server.requests",
    "description": null,
    "baseUnit": "seconds",
    "measurements": [
        {
            "statistic": "COUNT",
            "value": 1
        },
        {
            "statistic": "TOTAL_TIME",
            "value": 0.3229436
        },
        {
            "statistic": "MAX",
            "value": 0.3229436
        }
    ],
    "availableTags": [
        {
            "tag": "exception",
            "values": [
                "None"
            ]
        },
        {
            "tag": "method",
            "values": [
                "GET"
            ]
        },
        {
            "tag": "outcome",
            "values": [
                "SUCCESS"
            ]
        },
        {
            "tag": "status",
            "values": [
                "200"
            ]
        }
    ]
}

环境:

    `spring boot 2.1.2.RELEASE`
    <java.version>1.8</java.version>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

共有1个答案

公西俊民
2023-03-14

因此您希望用/admin/count封装acture/metrics

Java中有多种调用Rest API的方法和库

我会加上一个最简单的

public JSONObject sendRequestToURL(@PathVariable("finalURL") String urlToRead)
{
      StringBuilder result = new StringBuilder();
      URL url = new URL(urlToRead);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = rd.readLine()) != null) {
         result.append(line);
      }
      rd.close();
      return new JSONObject(result.toString());  // org.json
}
String strJson = result.toString().replace("\\\"","'");
JSONObject jo = new JSONObject(strJson.substring(1,json.length()-1));
return jo;

编辑2:

我猜你的Spring保安已经到位了。

并且当您在内部调用API时,Spring将其视为需要身份验证的外部调用。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
     .antMatchers("/actuator*").permitAll()

     ...
}
<security:http  auto-config="true"  use-expressions="true"   >
    <security:intercept-url pattern="/actuator*" access="permitAll"/>

    ...
</security:http>

希望Spring security会忽略这个URL,您也不会得到登录表单。

 类似资料:
  • 本文向大家介绍asp.net计算每个页面执行时间的方法,包括了asp.net计算每个页面执行时间的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了asp.net计算每个页面执行时间的方法。分享给大家供大家参考。具体分析如下: 这里的asp.net代码可实现计算每个页面的执行时间,无需要修改页面的相关代码,这段代码会给所有的页面统一加上执行时间显示 希望本文所述对大家的asp.net程序

  • 更新:一面过了,求保佑🙏 ————————————————— 面试官是个6k引的老师,很犀利 首先双方自我介绍, 1.拷打论文 2.我说的啰哩啰嗦的,打开论文讲,讲贡献创新,三句话足够 3.拷打完问我怎么设计一个3d文生图pipeline,看得少乱扯 4.对图生图的了解(ip- adapter controlnet 5.ddpm与ddim区别(denoising区别, 6.为什么ddim比ddp

  • 我用SpringBoot创建了一个简单的演示应用程序,其中包括执行器。带有@Scheduled注释的任务显示在执行器中,但以编程方式启动的任务不会显示。有没有办法让他们也出现? 我已经注释了@Enable调度。 我的组件如下所示: 执行器的结果仅显示带注释的任务:

  • 我想先除,然后乘,但不知道怎么做这两个想法? 它在工作,但它只是分裂的时刻。

  • 问题内容: 我想测试某个代码段执行的SQL查询越少越好。 似乎有其自己的方法,它将做到这一点。但是由于我没有修补ActiveRecord,所以对我来说没什么用。 RSpec或ActiveRecord是否提供任何官方的公开方式来计算在代码块中执行的SQL查询的数量? 问题答案: 我认为您通过提及回答了您自己的问题,但是这里有: 我建议您看一下后面的代码,并使用它来构建自己的方法,该方法可用于计算查询

  • 我正在使用Spring boot来渲染页面。我也使用了叶子。但是我在从浏览器调用它时得到了下面提供的异常。 我已经编写了视图解析器类,当我使用为此。 和我在应用程序中提到的一样。yml也是。应用yml是 这是我的Controller类,负责渲染index.jspMainController.class