我需要计算每个端点上的视图。这个想法是为所有端点创建一个通用的请求计数映射,该请求应基于动态输入的端点返回视图计数。
假设某人想查看http://localhost:8080/user/101
上的观看次数。
path = /admin/count & RequestParam = url (Here
/user/101)
dynamic Request based on RequestParam
http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:/user/101
Get and Return the Response
的动态请求(JSON Object)
并获得COUNT
的值我坚持将
dynamic request
发送给 http://localhost:8080/actuator/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/actuator/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>
答案 0 :(得分:1)
所以您想用actuator/metrics
封装/admin/count
有许多方法和库可以用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
}
编辑1:
您快到了。只需将String解析为JSONObject。尝试这个
String strJson = result.toString().replace("\\\"","'");
JSONObject jo = new JSONObject(strJson.substring(1,json.length()-1));
return jo;
编辑2:
我想您已经安装了Spring Security。
当您在内部调用API时,Spring被视为需要身份验证的外部调用。
作为一种解决方法,您可以从安全上下文中排除/actuator
API。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/actuator*").permitAll()
...
}
或XML
<security:http auto-config="true" use-expressions="true" >
<security:intercept-url pattern="/actuator*" access="permitAll"/>
...
</security:http>
希望Spring安全性将忽略此URL,并且您将不会获得登录表单。
答案 1 :(得分:0)
这个想法是,您将从用户那里获取endPoint进行显示,以显示将使用@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)
public int count(@RequestParam(name="endPoint") final String endPoint) throws IOException, JSONException
{
final String URLtoMap = "http://localhost:8080/actuator/metrics/http.server.requests?tag=uri:" + endPoint + "";
return sendRequestToURL(URLtoMap);
}
现在基于URLtoMap
使用HttpURLConnection
发送请求,并使用BufferedReader
获取输出。当我使用Spring Security时,我被重定向到登录页面。为了解决该问题,我在SecurityConfig文件中添加了antMatchers,如下所示。如果您面对JSONException: Value of type java.lang.String cannot be converted to JSONObject
,请参考this
public int sendRequestToURL(@PathVariable("URLtoMap") String URLtoMap) throws IOException, JSONException
{
int count = 0;
StringBuilder result = new StringBuilder();
URL url = new URL(URLtoMap);
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();
try {
JSONObject jsonObject =new JSONObject(result.toString().replace("\"", ""));
JSONObject jsonCountObject = new JSONObject(jsonObject.getJSONArray("measurements").get(0).toString());
count =(int) jsonCountObject.get("value");
}
catch (JSONException e) {
e.printStackTrace();
}
return count;
}
SecurityConfig
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.csrf().disable()
.authorizeRequests().antMatchers("/login").permitAll()
.antMatchers(HttpMethod.GET,"/actuator/**").permitAll()
.antMatchers(HttpMethod.POST,"/actuator/**").permitAll()
}
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<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;