Spring Cloud Stream Core学习笔记(二)

文章目录

  • 参数调优
    • concurrency,maxConcurrency,prefetch
    • 消息重试
  • show you my code
  • 参考文章

参数调优

concurrency,maxConcurrency,prefetch

concurrency代表客户端的个数,prefetch代表消费端预取的消息个数,maxConcurrency弹性的客户端个数。

怎么理解?
concurrency初始化客户端个数
prefetch消费者每次都会批量取的消息个数
maxConcurrency当消息量很大的时候,会自动的扩展到该数量的客户端

如何配置?
Spring Cloud Stream Core学习笔记(二)_第1张图片
Spring Cloud Stream Core学习笔记(二)_第2张图片
Spring Cloud Stream Core学习笔记(二)_第3张图片

消息重试

有几种情况需要重试,比如短暂失败(网络问题),长期失败(业务报错)
短暂的是可以解决的就是进行重试,长期的重试多少遍都是报错,会进入死信队列里面。

重试

Spring Cloud Stream Core学习笔记(二)_第4张图片
idea执行结果

2020-01-08 22:21:27 一般监听收到:大家好
2020-01-08 22:21:30 一般监听收到:大家好
2020-01-08 22:21:36 一般监听收到:大家好
2020-01-08 22:21:46 一般监听收到:大家好
2020-01-08 22:21:46.235  WARN 7612 --- [GeFOpfXbE7bSA-3] o.s.a.r.r.RejectAndDontRequeueRecoverer  : Retries exhausted for message (Body:'大家好' MessageProperties [headers={contentType=text/plain}, timestamp=null, messageId=null, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=null, contentType=text/plain, contentEncoding=null, contentLength=0, deliveryMode=null, receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=mqTestDefault, receivedRoutingKey=mqTestDefault, receivedDelay=null, deliveryTag=1, messageCount=0, consumerTag=amq.ctag-arceamECZ8w1qRs7xfNsCA, consumerQueue=mqTestDefault.anonymous.xbAA6oOuSGeFOpfXbE7bSA])

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:876) [spring-rabbit-1.7.1.RELEASE.jar:na]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:786) [spring-rabbit-1.7.1.RELEASE.jar:na]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:706) [spring-rabbit-1.7.1.RELEASE.jar:na]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:96) [spring-rabbit-1.7.1.RELEASE.jar:na]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:187) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(RetryOperationsInterceptor.java:91) ~[spring-retry-1.2.0.RELEASE.jar:na]
	at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:286) [spring-retry-1.2.0.RELEASE.jar:na]
	at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:179) [spring-retry-1.2.0.RELEASE.jar:na]
	at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:115) [spring-retry-1.2.0.RELEASE.jar:na]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at com.sun.proxy.$Proxy106.invokeListener(Unknown Source) [na:na]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1274) [spring-rabbit-1.7.1.RELEASE.jar:na]
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:687) [spring-rabbit-1.7.1.RELEASE.jar:na]
	at org.springframework.amqp.rabbit.listener.SimpleMessageList

注意前面这个时间差,3秒,6秒,10秒之后重试。

代码

@StreamListener(Processor.INPUT)
    public void input(Message message) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(System.currentTimeMillis())+" "+"一般监听收到:" + message.getPayload());
        throw new RuntimeException();
    }

show you my code

  • Spring-Cloud-Stream-Core-demo

参考文章

  • Spring Cloud Stream
  • spring-boot-cloudJava

你可能感兴趣的:(spring,cloud)