我在我的应用程序上收到此警告。我正在同时读取大约 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。这会将每个带有唯一 rfidtag 的 URL 放入其自己的 URI 标记存储桶中。
为了避免这个问题,您可以使用 uriVariables 映射:
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
、 的 URI 标记http://url/rfidtag2
、http://url/rfidtag3
等等。
这应该会减少您的应用程序创建的 URI 标记的数量。
URI 标签最大数量的默认值为 100。如果您想要更多的指标,您可以通过将其属性值设置为其他值来自定义它。例如,我的 Spring 应用程序是使用我的 projectname-ws/src/main/resources/config/application.yml
文件配置的。在这个文件中我可以放置类似的内容
management:
metrics:
web:
client:
max-uri-tags: 200
server:
max-uri-tags: 200
增加用于指标的 URI 标记的最大数量。
如果您根本不希望出现达到了 http.client.requests 的 URI 标记的最大数量
警告,您可以将
@SpringBootApplication(exclude = HttpClientMetricsAutoConfiguration.class)
在您运行应用程序的任何类的顶部,它将消除警告消息,但可能无法解决根本问题。
关于java - 如何解决 "Reached the maximum number of URI tags for http.client.requests"警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54930449/