配置带有服务激活器的断路器

时间:2015-12-23 13:42:33

标签: java spring-integration apache-kafka

配置详情:

  <int:publish-subscribe-channel id="toKafka"/>

  <int:publish-subscribe-channel id="sendMessageToKafkaChannel"/>

  <int:service-activator input-channel="toKafka" output-channel="sendMessageToKafkaChannel" order="1" ref="conditionalProducerService" method="producerCircuitBreaker">
       <int:request-handler-advice-chain> 
              <ref bean="circuitBreakerAdvice" />
       </int:request-handler-advice-chain>
  </int:service-activator>

  <int-kafka:outbound-channel-adapter id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext"
                                                                       auto-startup="true" channel="toKafka" message-key="kafka_messageKey"/>

  <bean id="circuitBreakerAdvice" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
    <property name="threshold" value="2"/>                      
    <property name="halfOpenAfter" value="15000" />                     
  </bean>
  public Message<?> producerCircuitBreaker(Message<?> payload) {
      throw  new RuntimeException("foo Pro");
  }
  for(int i=0;i<4;i++){
          toKafka.send(MessageBuilder
                              .withPayload(messageVO.getMessageContentVO())                                  
                              .setHeader(KafkaHeaders.TOPIC, topic)
                              .setHeader(KafkaHeaders.PARTITION_ID,Integer.parseInt(messageVO.getPartition())).
                              build());

                 APPLOGGER.info("sending message");
   }

期望让过程失败2次,异常然后&#34;断路器打开&#34;异常,但它只是在控制台中抛出以下异常后停止。

此外,我们如何在此处配置错误通道。

https://gist.github.com/anonymous/67aae50e548c78470cd0

更新了配置:

<int:service-activator input-channel="toKafka"  ref="gw">
    <int:request-handler-advice-chain> <ref bean="circuitBreakerAdvice"/>
                      </int:request-handler-advice-chain> 
   </int:service-activator>

   <int:channel id="failedChannel1" />


   <int:gateway id="gw" default-request-channel="toKafka" default-reply-timeout="0" error-channel="failedChannel1"  />

     <int:chain input-channel="failedChannel1">
        <int:transformer expression="'failed:'+payload.failedMessage.payload+ ' with a' +payload.cause.message" />
        <int-stream:stderr-channel-adapter append-newline="true"/>
            </int:chain>  

低于例外。
失败:TestVo [data = sample message]]无法处理消息。
https://gist.github.com/anonymous/921be7691c41d125dc84
然而,它正在使用相同的消息。(消息内容有意更改)

还尝试为生产者上下文设置无效值:例如。 broker-list / value-class-type作为无效的类类型,如下所示。

低于错误但期望让CB进入画面并且消息应该流向错误频道。

在value-class-type的情况下:CB没有被调用,但是消息流向错误通道但是有1条消息发布了。

失败:TestVo [data = {tes message}}],找不到能够从类型xx.xxx.vo.TestVo转换为类型java.lang.String

的转换器

这些在控制台中多次出现。

在broker-list的情况下:它只是在控制台中抛出异常。

https://gist.github.com/anonymous/6ece517fb5e82ac73492

预期:在所有情况下,CB都会被调用并且消息流到错误通道。

  <int-kafka:outbound-channel-adapter id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext"
                                                                       auto-startup="true" channel="toKafka" message-key="kafka_messageKey"/>  

  <int-kafka:producer-context id="producerContext" producer-properties="producerProperties">
                                                            <int-kafka:producer-configurations>
                                                                           <int-kafka:producer-configuration
                                                                                            broker-list="1.2.3:9092" topic="headers['topic']" key-class-type="java.lang.String"
                                                                                            value-class-type="java.lang.String"
                                                                                            value-encoder="kafkaEncoder" key-encoder="kafkaKeyEncoder"
                                                                                            compression-type="none" />
                                                            </int-kafka:producer-configurations>
                                            </int-kafka:producer-context>

1 个答案:

答案 0 :(得分:3)

使用该代码,try {...}附近需要send()

前两次尝试将抓住您的RuntimeException;接下来会遇到断路器异常。

使用带有错误频道的Messaging Gateway,而不是直接发送到频道。

修改

此代码......

<int:service-activator input-channel="toKafka"  ref="gw">
    <int:request-handler-advice-chain> <ref bean="circuitBreakerAdvice"/>
                  </int:request-handler-advice-chain> 
</int:service-activator>

<int:gateway id="gw" default-request-channel="toKafka" default-reply-timeout="0" error-channel="failedChannel1"  />

当您向toKafka发送消息时,将调用网关,该消息将在循环中将消息发送到toKafka

会导致堆栈溢出。