Java 配置带出站适配器的断路器以处理连接超时问题 公共消息生成器断路器(消息有效负载){ 抛出新的RuntimeException(“foo Pro”);}

Java 配置带出站适配器的断路器以处理连接超时问题 公共消息生成器断路器(消息有效负载){ 抛出新的RuntimeException(“foo Pro”);},java,spring-integration,kafka-producer-api,Java,Spring Integration,Kafka Producer Api,通过上述配置,我们正在尝试: <int:service-activator input-channel="toKafka" ref="conditionalProducerService" method="producerCircuitBreaker"> <int:request-handler-advice-chain> <ref bean="circuitBreakerAdvice1" /> </int:request-h

通过上述配置,我们正在尝试:

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}
1.希望将失败消息传播到错误通道=“failedChannel2”,但没有发生。因为我无法在控制台中看到转换后的输出

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}
2.CircuitBreaker正在为ServiceActivator工作(此处的应用程序相关异常如上所述),但我们如何为出站适配器的失败案例配置CB。示例:当连接超时或服务器突然停机/网络连接问题/在将消息从SI通道发送到外部(kafka)服务器之前出现某些环境问题。我们是否可以为CB配置出站适配器以应对这种情况

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}
根据SI文件关于断路器的建议,见下文

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}
“通常,此建议可能用于外部服务,在这些服务中可能需要一段时间才能失败(例如 作为尝试建立网络连接的超时)”

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}
请就如何实现这一目标提出建议。非常感谢

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}
更新配置:

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}

公共无效转换失败(消息){
APPLOGGER.log(“变压器消息测试”+消息);
公共类ProducerMessageHandler扩展了KafkaProducerMessageHandler{
公共产品消息处理程序(KafkaProducerContext KafkaProducerContext){
超级(卡夫卡创作的内容);
//TODO自动生成的构造函数存根
}
@凌驾
public void handleMessageInternal(最终消息)引发异常{
//super.handleMessageInternal(消息);
抛出新的运行时异常(“testfoo”);
}
日志:

<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}
01-05@23:44:18598调试org.springframework.integration.config.ServiceActivatorFactoryBean$1-org.springframework.integration.config.ServiceActivatorFactoryBean$1@6a0ef4b6收到的消息:GenericMessage[payload=hello,headers={timestamp=1452017658598,id=e0591162-3b93-9bb6-0699-89b15b20e904}] 调试:-com.XXX.ProducerMessageHandler#0收到的消息:GenericMessage[payload=hello,headers={timestamp=1452017658598,id=e0591162-3b93-9bb6-0699-89b15b20e904}] 获取异常:org.springframework.messaging.MessageHandlingException:消息处理程序[com.XXX.ProducerMessageHandler#0]中发生错误;嵌套异常为java.lang.RuntimeException:test foo 01-05@23:44:18606调试org.springframework.integration.channel.PublishSubscribeChannel-在频道“toKafka”上显示,消息:GenericMessage[payload=hello,headers={timestamp=1452017658605,id=61597941-b2f8-314d-141d-8f2c058dda4d}] 01-05@23:44:18606调试org.springframework.integration.config.ServiceActivatorFactoryBean$1-org.springframework.integration.config.ServiceActivatorFactoryBean$1@6a0ef4b6收到的消息:GenericMessage[payload=hello,headers={timestamp=1452017658605,id=61597941-b2f8-314d-141d-8f2c058dda4d}] 调试:-com.XXX.ProducerMessageHandler#0收到的消息:GenericMessage[payload=hello,headers={timestamp=1452017658605,id=61597941-b2f8-314d-141d-8f2c058dda4d}] 获取异常:org.springframework.messaging.MessageHandlingException:消息处理程序[com.XXX.ProducerMessageHandler#0]中发生错误;嵌套异常为java.lang.RuntimeException:test foo 01-05@23:44:18606调试org.springframework.integration.channel.PublishSubscribeChannel-在频道“toKafka”上呈现,消息:GenericMessage[payload=hello,headers={timestamp=1452017658606,id=119afbf1-6104-feb1-eb44-f646aa932277}] 01-05@23:44:18606调试org.springframework.integration.config.ServiceActivatorFactoryBean$1-org.springframework.integration.config.ServiceActivatorFactoryBean$1@6a0ef4b6收到的消息:GenericMessage[payload=hello,headers={timestamp=1452017658606,id=119afbf1-6104-feb1-eb44-f646aa932277}] 获取异常:org.springframework.messaging.MessageHandlingException:消息处理程序[org.springframework.integration.config.ServiceActivatorFactoryBean]中发生错误$1@6a0ef4b6];嵌套异常为org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice$CircuitBreakerOpenException:org.springframework.integration.config.ServiceActivatorFactoryBean的断路器打开$1@6a0ef4b6 01-05@23:44:18606调试org.springframework.integration.channel.PublishSubscribeChannel-在频道“toKafka”上显示,消息:GenericMessage[payload=hello,headers={timestamp=1452017658606,id=8dafe2e0-8efe-c827-e745-1387e6045e7d}] 01-05@23:44:18606调试org.springframework.integration.config.ServiceActivatorFactoryBean$1-org.springframework.integration.config.ServiceActivatorFactoryBean$1@6a0ef4b6收到的消息:GenericMessage[payload=hello,headers={timestamp=1452017658606,id=8dafe2e0-8efe-c827-e745-1387e6045e7d}]
获取异常:org.springframework.messaging.MessageHandlingException:消息处理程序[org.springframework.integration.config.ServiceActivatorFactoryBean]中发生错误$1@6a0ef4b6];嵌套异常为org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice$CircuitBreakerOpenException:org.springframework.integration.config.ServiceActivatorFactoryBean的断路器打开$1@6a0ef4b6

该建议仅适用于分配给它的端点,而不适用于下游流;不幸的是
<int:service-activator input-channel="toKafka"  ref="conditionalProducerService" method="producerCircuitBreaker">

  <int:request-handler-advice-chain>
       <ref bean="circuitBreakerAdvice1" />
   </int:request-handler-advice-chain>
            </int:service-activator>

  <int:channel id="failedChannel2" />
  <int-kafka:outbound-channel-adapter
                            id="kafkaOutboundChannelAdapter" kafka-producer-context-ref="producerContext" auto-startup="false" channel="toKafka" message-key="kafka_messageKey">
                            <int:poller fixed-delay="1000" error-channel="failedChannel2" />
            </int-kafka:outbound-channel-adapter>


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

            <bean id="circuitBreakerAdvice1" class="org.springframework.integration.handler.advice.RequestHandlerCircuitBreakerAdvice">
                            <property name="threshold" value="2" />                                         
                            <property name="halfOpenAfter" value="12000" />                     
            </bean>

  public Message<?> producerCircuitBreaker(Message<?> payload) {
          throw  new RuntimeException("foo Pro");}