Apache Camel/Spring中的节流阀

lyfkaqu1  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(144)

如何在camel中的所有路线中添加油门

@Component
public class MyRestRoute extends RouteBuilder {

@Value("${spring.application.name}")
private String appName;

@Value("${spring.application.description}")
private String description;

@Value("${spring.application.version}")
private String appVersion;

@Override
public void configure() throws Exception {

    restConfiguration().apiContextRouteId("swagger").contextPath(System.getenv("CONTEXT_PATH"))
            .apiContextPath("/swagger").component("servlet")
            .apiProperty("api.title", appName)
            .apiProperty("api.description", description)
            .apiProperty("api.version", appVersion)
            .apiProperty("host", "localhost")
            .apiProperty("port", "8080")
            .apiProperty("schemes", "http");

    rest("/transfers/{transfer_id}")
            .post().type(Request.class).id("id-limits").description("transfer").bindingMode(RestBindingMode.auto)
            .skipBindingOnErrorCode(true)
            .param().name("transfer_id").type(RestParamType.path).description("transferId").endParam()
            .produces(MediaType.APPLICATION_JSON_VALUE)

            .to("direct:transferRoute);

    rest("/accounts")
            .get().id("id-limits").description("Get Accounts").bindingMode(RestBindingMode.auto)
            .skipBindingOnErrorCode(true)

            .param().name("account_id").type(RestParamType.query).description("account_id").endParam()
            .param().name("document").type(RestParamType.query).description("document").endParam()

            .produces(MediaType.APPLICATION_JSON_VALUE)
            .to("direct:accountsRoute));
}
}

可以接收多个rest resource_path,如何在所有主路径中插入throttle?我知道我可以在.from(“direct:transferRoute”)和.from(“direct:accountsRoute”)中的每个路径的开始后插入,但我想一般地插入所有资源。我可以在Camel中这样做吗,或者使用spring更安全?

daolsyd0

daolsyd01#

据我所知,我认为全球范围内不可能这样做,恐怕你们必须按每条航线来做。
作为一个(相当愚蠢的)变通办法,你还可以想象这样的事情。

public abstract class CustomRouteBuilder extends RouteBuilder {
  public RouteDefinition fromWithTrottle(String uri) {
    return this.fromWithTrottle(uri, 3, 10000); // default throttling
  }
  public RouteDefinition fromWithTrottle(String uri, int count, long period) {
    return super.from(uri)
       .throttle(count)
       .timePeriodMillis(period);
}

public class MyRestRoute extends CustomRouteBuilder {
  @Override
  public void configure() throws Exception {
     fromWithTrottle("direct:transferRoute")
       .to("mock:transfer");

     fromWithTrottle("direct:accountsRoute", 2, 12_000)
       .to("mock:accounts")
  }
}
mxg2im7a

mxg2im7a2#

也许你可以用拦截器https://camel.apache.org/components/3.17.x/eips/intercept.html#_using_intercept

interceptFrom().throttle(...);

相关问题