如何解决“达到 http.client.requests 的最大 URI 标记数”警告?

Ror*_*oro 11 java resttemplate spring-boot

我在我的应用程序上收到此警告。我正在同时阅读大约 30 位读者的 rfidtags。每次有标签进来时,我都会点击数据库查看它是否在那里。我有一个我正在使用的休息 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;
    }
}

}
Run Code Online (Sandbox Code Playgroud)

小智 15

因此,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);
Run Code Online (Sandbox Code Playgroud)

这让 Spring 收集指标http://url/{rfidtag}而不是为http://url/rfidtag1、http://url/rfidtag2、http://url/rfidtag3等设置 URI 标记。

这应该会减少您的应用程序正在创建的 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
Run Code Online (Sandbox Code Playgroud)

增加用于指标的 URI 标签的最大数量。

如果你只是不想要Reached the maximum number of URI tags for http.client.requests警告,你可以把

@SpringBootApplication(exclude = HttpClientMetricsAutoConfiguration.class)
Run Code Online (Sandbox Code Playgroud)

在您运行应用程序的任何类的顶部,它将消除警告消息,但可能无法解决潜在问题。

  • 就我而言,使用 URI 变量并没有解决我的问题,因为实际上 /receiving/ 请求才是问题所在。通过(也)排除 WebMvcMetricsAutoConfiguration.class,我终于摆脱了警告! (2认同)