我在我的应用程序上收到此警告。我同时阅读了大约30名读者的rfidtags。每次标签都会出现时,我都会击中数据库,以查看其是否在其中。我有一个我正在使用的REST API。因此,我使用REST模板击中REST API。关于如何解决这个问题的想法?谢谢!
这是我的一些代码:
private void vehicleRequests(List<Maybevehicle> vehicles){
//process list of unique tags to see if they are in the database and linked to a vehicle
List<Maybevehicle> foundMaybeVehs= new ArrayList<>();
List<Maybevehicle> notFound=new ArrayList<>();
if(!vehicles.isEmpty()){
for (Maybevehicle v: vehicles){
Future<Maybevehicle> r=aService.batchVehTags(v);
try{
Maybevehicle m=r.get(2000, TimeUnit.SECONDS);
if(r.isDone()){
if (!(m.getB().getVin().equals("notindb"))){
foundMaybeVehs.add(m);
}
}
}
}catch(InterruptedException | ExecutionException | TimeoutException e){
}
}
if(!foundMaybeVehs.isEmpty()){
addLocation(foundMaybeVehs);
}
}else{
log.info("no vehicles to check.");
}
}
@Override
public Future<Maybevehicle> batchVehTags(Maybevehicle v) {
Future<Maybevehicle> future=null;
try{
SimpleTaskMaybeveh task=new SimpleTaskMaybeveh(v, appRestTemplate);
future=dbService.submit(task);
}catch(Exception e){
e.printStackTrace();
}
return future;
}
}
public class SimpleTaskMaybeveh implements Callable<Maybevehicle>{
private RestTemplate appRestTemplate;
private Maybevehicle veh;
public SimpleTaskMaybeveh(Maybevehicle veh, RestTemplate appRestTemplate){
this.veh=veh;
this.appRestTemplate=appRestTemplate;
}
@Override
public Maybevehicle call(){
String url="http://url/"+veh.getB().getRfidtag();
String authString= "";
byte[] encodedAuth= Base64.encodeBase64(authString.getBytes(Charset.forName("US-ASCII")));
String authHeader="Basic "+new String(encodedAuth);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.set("Authorization", authHeader);
HttpEntity<Bmwvehiclemain> requestEntity=new HttpEntity <Bmwvehiclemain>(headers);
ResponseEntity<Bmwvehiclemain> results=appRestTemplate.exchange(url, HttpMethod.GET, requestEntity, Bmwvehiclemain.class);
try{
Bmwvehiclemain n=results.getBody();
Maybevehicle d=new Maybevehicle(n,veh.getNewtaglocation());
return d;
}catch(Exception e){
Maybevehicle notveh=new Maybevehicle("notindb");
return notveh;
}
}
}
因此,Spring应用程序在所有入站和出站API调用上收集指标。可能会查询这些指标以查看一个称为某个URL的次。
在您的public Maybevehicle call()
方法中,您正在通过附加到字符串来构建URL。这将使每个URL具有独特的RFIDTAG在其自己的URI标签存储桶中。
为了避免此问题,您可以使用尿路图:
String url = “http://url/{rfidtag}”;
Map<String, ?> uriVariables = new HashMap<>();
uriVariables.put(“rfidtag”, veh.getB().getRfidtag();
…
ResponseEntity<Bmwvehiclemain> results = appRestTemplate
.exchange(url, HttpMethod.GET, requestEntity, Bmwvehiclemain.class, uriVariables);
这使Spring可以在http://url/{rfidtag}
上收集指标,而不是为http://url/rfidtag1
,http://url/rfidtag2
,http://url/rfidtag3
等具有URI标签。
这将减少您的应用程序创建的URI标签数量。
URI标签的最大数量的默认值为100。如果您希望在此方面更多的指标,则可以通过将其属性值设置为其他内容来自定义。例如,我的春季应用程序配置为我的projectname-ws/src/main/resources/config/application.yml
文件。在此文件中,我可以放置
management:
metrics:
web:
client:
max-uri-tags: 200
server:
max-uri-tags: 200
增加用于指标的URI标签的最大数量。
如果您只是不想要Reached the maximum number of URI tags for http.client.requests
警告,则可以放置
@SpringBootApplication(exclude = HttpClientMetricsAutoConfiguration.class)
在您运行应用程序的任何类的顶部都可以摆脱警告消息,但可能无法解决基本问题。
确实,此警告是关于将变量数据串联到URL而不是使用变量的HTTP请求。而且,正如保罗所解释的那样,您应该用参数URL替换URL串联。另一种方式,使用"流利样式"来使用Web-Client实现此目的是:
webClient
.method(HttpMethod.GET)
.uri("/.../...s/{id}/overview", id)
.retrieve()
但是,请注意,忽略此类警告有其影响...
例如,如果您使用Prometheus从应用程序中刮擦指标,则使用此类串联的URL结果,其中具有大量指标,例如http_client_requests_seconds_count{clientName="...",environment="",method="GET",outcome="SUCCESS",region="",service_name="",status="200",uri="/api/.../1234-5678-90/...",}
(因为Spring无法确定"两个此类请求"仅在" ID"中"实际上是一样的)。
因此,除了向您的Prometheus垃圾邮件外,此类指标大多是没有用的。
就我而言,我的应用程序正在使用一个库,该库反过来又使用了Web-Client,并且正在串联URL。我正在寻找一种识别这些电话的方法,以便我可以要求它们修复。最终,我发现我可以使用actuator/prometheus
端点来查看所有网络客户指标,这确实是由于这个问题的巨大。