Spring boot spring云数据流rabbitmq消费者动态缩放

Spring boot spring云数据流rabbitmq消费者动态缩放,spring-boot,spring-cloud-stream,spring-rabbit,Spring Boot,Spring Cloud Stream,Spring Rabbit,在RabbitMQ版本中使用spring云流。我想实现消费者自动缩放的解释。通过以下配置,我有2个并发使用者。但是SimpleMessageListenerContainer无法动态添加消费者。我已经检查了SMLC实例是否同时使用并发和最大并发属性初始化。有没有什么东西,我没有得到动态缩放 注意:我在rabbitmq的input1 exchange中发布了200条消息。 pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0

在RabbitMQ版本中使用spring云流。我想实现消费者自动缩放的解释。通过以下配置,我有2个并发使用者。但是SimpleMessageListenerContainer无法动态添加消费者。我已经检查了SMLC实例是否同时使用并发和最大并发属性初始化。有没有什么东西,我没有得到动态缩放

注意:我在rabbitmq的input1 exchange中发布了200条消息。

pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>consumerex</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>graphql</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
spring:
  cloud:
    stream:
      bindings:
        input1:
          destination: input1
          binder: local_rabbit
          group: logMessageConsumers
          consumer:
            concurrency: 2
        input2:
          destination: input2
          binder: local_rabbit
          group: logMessageConsumers
      binders:
        local_rabbit:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
                virtual-host: /
      rabbit:
        bindings:
          input1:
            consumer:
              maxConcurrency: 10
server:
  port: 0
management:
  health:
    binders:
       enabled: true
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

@SpringBootApplication
@EnableBinding(MyProcessor.class)
public class App 
{
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @StreamListener("input1")
    public void enrichLogMessage(String log) {
        //code to keep current consumer busy. So auto scalling can happen.
        long time = System.currentTimeMillis() + 120000;
        while(time> System.currentTimeMillis()) {

        }
        System.out.println(Thread.currentThread().getName() + "    input1       "  + log);

    }

    @StreamListener("input2")
    public void input2(String log) {
        System.out.println(Thread.currentThread().getName() + "     input2         "  + log);
    }
}
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;
public interface MyProcessor {
    @Input("input1")
    SubscribableChannel input1();

    @Input("input2")
    SubscribableChannel input2();

}
App.java文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>consumerex</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>graphql</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
spring:
  cloud:
    stream:
      bindings:
        input1:
          destination: input1
          binder: local_rabbit
          group: logMessageConsumers
          consumer:
            concurrency: 2
        input2:
          destination: input2
          binder: local_rabbit
          group: logMessageConsumers
      binders:
        local_rabbit:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
                virtual-host: /
      rabbit:
        bindings:
          input1:
            consumer:
              maxConcurrency: 10
server:
  port: 0
management:
  health:
    binders:
       enabled: true
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

@SpringBootApplication
@EnableBinding(MyProcessor.class)
public class App 
{
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @StreamListener("input1")
    public void enrichLogMessage(String log) {
        //code to keep current consumer busy. So auto scalling can happen.
        long time = System.currentTimeMillis() + 120000;
        while(time> System.currentTimeMillis()) {

        }
        System.out.println(Thread.currentThread().getName() + "    input1       "  + log);

    }

    @StreamListener("input2")
    public void input2(String log) {
        System.out.println(Thread.currentThread().getName() + "     input2         "  + log);
    }
}
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;
public interface MyProcessor {
    @Input("input1")
    SubscribableChannel input1();

    @Input("input2")
    SubscribableChannel input2();

}
MyProcessor.java文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>consumerex</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>graphql</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
spring:
  cloud:
    stream:
      bindings:
        input1:
          destination: input1
          binder: local_rabbit
          group: logMessageConsumers
          consumer:
            concurrency: 2
        input2:
          destination: input2
          binder: local_rabbit
          group: logMessageConsumers
      binders:
        local_rabbit:
          type: rabbit
          environment:
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
                virtual-host: /
      rabbit:
        bindings:
          input1:
            consumer:
              maxConcurrency: 10
server:
  port: 0
management:
  health:
    binders:
       enabled: true
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

@SpringBootApplication
@EnableBinding(MyProcessor.class)
public class App 
{
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @StreamListener("input1")
    public void enrichLogMessage(String log) {
        //code to keep current consumer busy. So auto scalling can happen.
        long time = System.currentTimeMillis() + 120000;
        while(time> System.currentTimeMillis()) {

        }
        System.out.println(Thread.currentThread().getName() + "    input1       "  + log);

    }

    @StreamListener("input2")
    public void input2(String log) {
        System.out.println(Thread.currentThread().getName() + "     input2         "  + log);
    }
}
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;
public interface MyProcessor {
    @Input("input1")
    SubscribableChannel input1();

    @Input("input2")
    SubscribableChannel input2();

}

增加并发性的算法在侦听器线程上运行,因此这样睡眠线程将阻止它工作

以下应用程序显示它按设计工作

@springboot应用程序
@EnableBinding(Sink.class)
公共类SO59723356应用程序{
私有静态最终记录器log=LoggerFactory.getLogger(So59723356Application.class);
公共静态void main(字符串[]args){
SpringApplication.run(So59723356Application.class,args);
}
@StreamListener(Sink.INPUT)
public void listen(字符串输入)抛出InterruptedException{
日志信息(in);
睡眠(1_000);
}
@豆子
公共应用程序运行程序(RabbitTemplate模板){
返回args->IntStream.range(0,50).forEach(i->template.convertAndSend(“输入”、“输入”、“foo”));
}
}


请参见新线程开始于
14:21:03.917

RabbitMessageChannelBinder不支持动态缩放。每个分区必须至少有一个使用者。使用者的instanceIndex用于指示使用了哪个分区。Cloud Foundry等平台只能有一个实例具有instanceIndex。事实上,支持实例内的动态缩放;看看我的答案。