You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Spring Integration与Java 11调用REST API无日志输出问题排查

问题描述

使用Spring Integration + Java 11在Docker环境中实现Web服务,尝试对指定端点发起GET请求,但启动应用上下文后日志无任何请求相关输出,也无报错。尝试用Service Activator后依旧无异常日志,求排查问题。

应用上下文配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-http="http://www.springframework.org/schema/integration/http"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/integration
                           http://www.springframework.org/schema/integration/spring-integration-5.1.xsd
                           http://www.springframework.org/schema/integration/http
                           http://www.springframework.org/schema/integration/http/spring-integration-http-5.1.xsd">

<int:channel id="requestChannel"/>

<int:channel id="responseChannel"/>

<int-http:outbound-gateway request-channel="requestChannel"
                           reply-channel="responseChannel"
                           url="https://devopsprojectefrei.azurewebsites.net/api/students"
                           http-method="GET"
                           expected-response-type="java.lang.String"/>

<int:logging-channel-adapter id="responseChannel" level="INFO"/>
</beans>

启动日志

[main] DEBUG org.springframework.integration.config.GlobalChannelInterceptorProcessor - No global channel interceptors.
[main] DEBUG org.springframework.context.support.DefaultLifecycleProcessor - Starting beans in phase -2147483648
[main] INFO org.springframework.integration.endpoint.EventDrivenConsumer - Adding {http:outbound-gateway} as a subscriber to the 'requestChannel' channel
[main] INFO org.springframework.integration.channel.DirectChannel - Channel 'org.springframework.context.support.ClassPathXmlApplicationContext@50caa560.requestChannel' has 1 subscriber(s).
[main] INFO org.springframework.integration.endpoint.EventDrivenConsumer - started org.springframework.integration.config.ConsumerEndpointFactoryBean#0
[main] DEBUG org.springframework.context.support.DefaultLifecycleProcessor - Successfully started bean 'org.springframework.integration.config.ConsumerEndpointFactoryBean#0'
[main] INFO org.springframework.integration.endpoint.EventDrivenConsumer - Adding {logging-channel-adapter:responseChannel.adapter} as a subscriber to the 'responseChannel' channel
[main] INFO org.springframework.integration.channel.DirectChannel - Channel 'org.springframework.context.support.ClassPathXmlApplicationContext@50caa560.responseChannel' has 1 subscriber(s).
[main] INFO org.springframework.integration.endpoint.EventDrivenConsumer - started responseChannel.adapter
[main] DEBUG org.springframework.context.support.DefaultLifecycleProcessor - Successfully started bean 'responseChannel.adapter'
[main] INFO org.springframework.integration.endpoint.EventDrivenConsumer - Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
[main] INFO org.springframework.integration.channel.PublishSubscribeChannel - Channel 'org.springframework.context.support.ClassPathXmlApplicationContext@50caa560.errorChannel' has 1 subscriber(s).
[main] INFO org.springframework.integration.endpoint.EventDrivenConsumer - started _org.springframework.integration.errorLogger
[main] DEBUG org.springframework.context.support.DefaultLifecycleProcessor - Successfully started bean '_org.springframework.integration.errorLogger'
[main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.liveBeansView.mbeanDomain' in PropertySource 'systemProperties' with value of type String
问题排查与解决

从配置和日志来看,核心问题是没有消息被发送到requestChannel,HTTP出站网关根本没被触发,自然不会产生请求相关日志。

1. 配置缺陷:缺少消息触发源

当前配置仅定义了通道、HTTP出站网关和日志适配器,但没有任何组件向requestChannel发送消息。Spring Integration是消息驱动架构,必须有消息进入通道,下游的网关才会启动工作流程。

2. 解决办法

根据业务场景选择以下触发方式:

  • 定时触发:如果需要定期调用目标接口,添加定时入站适配器:

    <int:inbound-channel-adapter channel="requestChannel" expression="'dummy'">
        <int:poller fixed-rate="5000"/> <!-- 每5秒触发一次请求 -->
    </int:inbound-channel-adapter>
    

    这里的expression="'dummy'"是因为GET请求无需请求体,发送任意字符串即可触发网关执行。

  • 外部HTTP触发:如果需要通过外部请求触发调用,添加HTTP入站网关作为入口:

    <int-http:inbound-gateway request-channel="requestChannel"
                              reply-channel="responseChannel"
                              path="/trigger-student-api"
                              request-method="GET"/>
    

    外部访问/trigger-student-api接口时,消息会被转发到requestChannel,触发下游的HTTP出站网关调用目标接口。

3. 额外检查项

  • 验证Docker网络连通性:在容器内用curl测试能否访问目标接口https://devopsprojectefrei.azurewebsites.net/api/students,排除网络不通的问题。
  • 调整日志级别:将org.springframework.integration.http的日志级别设为DEBUG,可查看更详细的请求/响应细节。

内容的提问来源于stack exchange,提问作者Frik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 06:45:08