使用 Spring Cloud Stream kafka 动态更改实例索引 [英] Dynamically changing the instanceindex with spring cloud stream kafka
问题描述
类似于:更改 spring-cloud-stream 实例索引/运行时计数
我在微服务器架构中对批处理的启动做了一个 poc,我正在将 Spring 批处理与 Spring Cloud Stream Kafka 一起使用.我正在寻找一种方法来动态创建消费者(处理器)应用程序的多个实例.我看到可以用
定义多个实例spring.cloud.stream.instanceCount=nspring.cloud.stream.instanceIndex=[0, ..., n-1]
但是我还没有找到动态改变 instanceIndex 值的方法.是否可以使用 Spring Cloud Stream kafka 动态修改此值.
感谢您的帮助.
我找到了一个解决方案如何自动动态更改实例索引.我设置了 minPortNum & 的值属性文件中的 maxPortNum 和 instanceCount,我根据 port-minPortNum 的值改变 instanceIndex,然后根据需要运行尽可能多的使用者.
<预><代码>@成分类 ServerPortCustomize 实现 WebServerFactoryCustomizer {@value("${port.number.min}")私人整数 minPortNum;@value("${port.number.max}")私人整数 maxPortNum;@value("${spring.cloud.stream.instanceCount}")私有整数实例计数;@覆盖公共无效定制(最终 ConfigurableWebServerFactory 工厂){最终 int 端口 = SocketUtils.findAvailableTcpPort(minPortNum, maxPortNum);factory.setPort(端口);System.getProperties().put(server.port", port);System.getProperties().put("spring.cloud.stream.instanceIndex", port-minPortNum);}}对于这个例子,我已经设置了spring.cloud.stream.instanceCount=3,所以我运行三个消费者,实例索引为值 0, 1 &2
Similar to: Changing spring-cloud-stream instance index/count at runtime
I do a poc on the launch of batch in a microserver architecture and I am using Spring batch with Spring Cloud Stream Kafka. I am looking for a way to dynamically create multiple instances of consumer (processor) application. I saw that it is possible to define a number of instances with
spring.cloud.stream.instanceCount=n
spring.cloud.stream.instanceIndex=[0, ..., n-1]
However I haven't found a way to dynamically change the value of instanceIndex. Is it possible to modify this value dynamically with Spring Cloud Stream kafka.
Thanks for any help.
I have find a solution how change dynamically the instanceindex automatically. I set the value of minPortNum & maxPortNum and instanceCount in the properties file and I vary instanceIndex depending on the value of port-minPortNum, and then I run as much consumer as I want.
@component
class ServerPortCustomize implements WebServerFactoryCustomizer {
@value("${port.number.min}")
private Integer minPortNum;
@value("${port.number.max}")
private Integer maxPortNum;
@value("${spring.cloud.stream.instanceCount}")
private Integer instanceCount;
@Override
public void customize(final ConfigurableWebServerFactory factory) {
final int port = SocketUtils.findAvailableTcpPort(minPortNum, maxPortNum);
factory.setPort(port);
System.getProperties().put("server.port", port);
System.getProperties().put("spring.cloud.stream.instanceIndex", port-minPortNum);
}
}
for this example I have set spring.cloud.stream.instanceCount=3, so I run three consumer with instance index as value 0, 1 & 2
这篇关于使用 Spring Cloud Stream kafka 动态更改实例索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!