油门在Apache骆驼/春天

发布于 2025-02-03 09:12:48 字数 1829 浏览 2 评论 0 原文

如何在骆驼中的所有路线中添加节气门,

@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,如何在所有主要路线中插入油门。 我知道我可以在.from(“ Direct:TransferRoute”)和.from(“ Direct:AccountRoute”)中插入。 我可以在骆驼中这样做,还是使用弹簧更安全?

How can I add throttle in all routes in 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));
}
}

That can receive more than a rest resource_path, how Can I insert throttle in all my main route.
I know I can insert after the start of each route in .from("direct:transferRoute") and .from("direct:accountsRoute"), but I want to insert in all my resources generically.
Can I do this in Camel, or maybe using spring is more safe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不可一世的女人 2025-02-10 09:12:48

据我所知,我认为不可能在全球范围内做
恐怕您必须以每次路线进行操作。

作为(非常愚蠢的)解决方法,您仍然可以想象这样的事情。

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")
  }
}

As far as I know, I do not think it is possible to do it globally.
I'm afraid you have to do it on a per-route basis.

As a (pretty stupid) workaround, you can yet imagine something like this.

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")
  }
}
千鲤 2025-02-10 09:12:48

也许您可以使用interceptor

interceptFrom().throttle(...);

Maybe you can use interceptor https://camel.apache.org/components/3.17.x/eips/intercept.html#_using_intercept

interceptFrom().throttle(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文