猿问

弹簧靴:计数页面浏览量 - 执行器

我需要计算每个端点上的视图。我们的想法是为所有端点创建一个通用的请求计数映射,该映射应基于动态输入的终结点返回视图计数。

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

  1. 请求映射path = /admin/count & RequestParam = url (Here /user/101)

  2. 然后创建dynamic Request based on RequestParam http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101

  3. Get and Return the Response的动态请求并获取的值(JSON Object)COUNT

我坚持如何发送一个到 http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101 并返回它的响应并获取计数值dynamic request

@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/actuator/metrics/http.server.requests?tag=uri:/user/101


守着一只汪
浏览 85回答 2
2回答

宝慕林4294392

这个想法是,您将从用户获取端点以显示视图计数,这将使用@RequestParam完成。 根据您的要求Based on the request endPoint create the URLtoMap(i.e methods, status, outcome, exception etc, e.g. http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101&tag=method:GET).@RequestMapping(path="/admin/count",method=RequestMethod.POST)&nbsp; &nbsp; public int count(@RequestParam(name="endPoint") final String endPoint) throws IOException, JSONException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; final String URLtoMap = "http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:" + endPoint + "";&nbsp; &nbsp; &nbsp; &nbsp; return sendRequestToURL(URLtoMap);&nbsp; &nbsp; }现在基于发送请求使用和获取输出使用。当我使用Spring Security时,我被重定向到登录页面。为了解决这个问题,我在SecurityConfig文件中添加了antMatchers,如下所示。如果您面对那么请参考这个URLtoMapHttpURLConnectionBufferedReaderJSONException: Value of type java.lang.String cannot be converted to JSONObjectpublic int sendRequestToURL(@PathVariable("URLtoMap") String URLtoMap) throws IOException, JSONException{&nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; StringBuilder result = new StringBuilder();&nbsp; &nbsp; &nbsp; URL url = new URL(URLtoMap);&nbsp; &nbsp; &nbsp; HttpURLConnection conn = (HttpURLConnection) url.openConnection();&nbsp; &nbsp; &nbsp; conn.setRequestMethod("GET");&nbsp; &nbsp; &nbsp; BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));&nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; while ((line = rd.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result.append(line);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; rd.close();&nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject =new JSONObject(result.toString().replace("\"", ""));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonCountObject = new JSONObject(jsonObject.getJSONArray("measurements").get(0).toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count =(int) jsonCountObject.get("value");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return count;}安全配置@Override&nbsp; &nbsp; &nbsp; &nbsp; protected void configure(HttpSecurity http) throws Exception{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.csrf().disable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.authorizeRequests().antMatchers("/login").permitAll()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.antMatchers(HttpMethod.GET,"/actuator/**").permitAll()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.antMatchers(HttpMethod.POST,"/actuator/**").permitAll()&nbsp;}啪.xml<dependency>&nbsp; &nbsp; <groupId>com.fasterxml.jackson.core</groupId>&nbsp; &nbsp; <artifactId>jackson-core</artifactId></dependency><dependency>&nbsp; &nbsp; <groupId>com.fasterxml.jackson.core</groupId>&nbsp; &nbsp; <artifactId>jackson-databind</artifactId></dependency><dependency>&nbsp; <groupId>org.json</groupId>&nbsp; <artifactId>json</artifactId>&nbsp; <version>20090211</version></dependency>导入正确的软件包import org.json.JSONException;import org.json.JSONObject;import java.net.URL;import java.net.HttpURLConnection;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;

杨魅力

所以你想要封装actuator/metrics/admin/count在Java中调用Rest API的方法和库有很多我将添加最简单的一个类似的东西public JSONObject sendRequestToURL(@PathVariable("finalURL") String urlToRead){&nbsp; &nbsp; &nbsp; StringBuilder result = new StringBuilder();&nbsp; &nbsp; &nbsp; URL url = new URL(urlToRead);&nbsp; &nbsp; &nbsp; HttpURLConnection conn = (HttpURLConnection) url.openConnection();&nbsp; &nbsp; &nbsp; conn.setRequestMethod("GET");&nbsp; &nbsp; &nbsp; BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));&nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; while ((line = rd.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result.append(line);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; rd.close();&nbsp; &nbsp; &nbsp; return new JSONObject(result.toString());&nbsp; // org.json}编辑 1:你快到了。只需要将字符串解析为 JSONObject。试试这个也许String strJson = result.toString().replace("\\\"","'");JSONObject jo = new JSONObject(strJson.substring(1,json.length()-1));return jo;编辑 2:我猜你有Spring Security。当您在内部调用API时,Spring将其视为需要身份验证的外部调用。作为一种解决方法,您可以从安全上下文中排除 API。/actuator@Overrideprotected void configure(HttpSecurity http) throws Exception {&nbsp; &nbsp; http.csrf().disable().authorizeRequests()&nbsp; &nbsp; &nbsp;.antMatchers("/actuator*").permitAll()&nbsp; &nbsp; &nbsp;...}或 XML 格式<security:http&nbsp; auto-config="true"&nbsp; use-expressions="true"&nbsp; &nbsp;>&nbsp; &nbsp; <security:intercept-url pattern="/actuator*" access="permitAll"/>&nbsp; &nbsp; ...</security:http>希望Spring安全将忽略此URL,您将不会获得登录表单。
随时随地看视频慕课网APP

相关分类

Java
我要回答