更新时间:2023-01-26 10:53:36
我找到了一个解决方案如何自动动态更改实例索引.我设置了 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