一、前言
上一篇已经完整的描述了spring boot 与cxf整合工程,包括服务端的发布以及客户端的访问。但是整个日志输出只有一个简单的一句:
o.a.c.w.s.f.ReflectionServiceFactoryBean : Creating Service {http://ws.cxf.ouyushan.org/}UserServiceService from class org.ouyushan.cxf.ws.UserService
表示通过CXF已创建指定服务。但是webservice服务中设计的请求信息一星半点都没有,当服务出现问题时,如何通过获取日志来分析问题呢?
其实在CXF整合完成后,其它附加的高级功能基本上都是通过拦截器来实现的。日志捕获和输出也是基于此实现的。
本文将带领大家快速实现CXF日志输出功能。
项目源码: https://github.com/ouyushan/spring-webservice-samples
spring-cxf参考源码: https://github.com/code-not-found/cxf-jaxws
spring-ws参考源码: https://github.com/code-not-found/spring-ws
二、开发环境
IDE::IDEA
JDK:1.8
maven:3.6.2
spring boot:2.4.0
cxf:3.4.1
三、实现步骤
具体代码实现步骤和上篇一样。本章在之前基础上讲解新增内容。
3.1、实现服务端和客户端
新建服务端和客户端spring-cxf-client-logging、spring-cxf-server-logging,并完成服务部署和客户端访问接口,具体步骤见上一篇。
3.2、添加日志输出依赖
spring boot与cxf整合后输出业务日志只需引入cxf-rt-features-logging,并实现日志拦截器即可。
服务端、客服端pom文件中添加cxf日志依赖包
1<dependency> 2 <groupId>org.apache.cxf</groupId> 3 <artifactId>cxf-rt-features-logging</artifactId> 4 <version>3.4.1</version> 5</dependency>
3.3、实现日志输出拦截器
在服务端和客户端配置类中,添加请求接入和响应输出日志拦截器
1@Bean 2public LoggingInInterceptor loggingInInterceptor() { 3 return new LoggingInInterceptor(); 4} 5 6@Bean 7public LoggingOutInterceptor loggingOutInterceptor() { 8 return new LoggingOutInterceptor(); 9}
服务端在Endpoint中配置新增的拦截器
1// log the request and response messages 2endpoint.getInInterceptors() 3 .add(loggingInInterceptor()); 4endpoint.getOutInterceptors() 5 .add(loggingOutInterceptor());
客户端在UserService中配置新增的拦截器
1// log the request and response messages 2jaxWsProxyFactoryBean.getInInterceptors() 3 .add(loggingInInterceptor()); 4jaxWsProxyFactoryBean.getOutInterceptors() 5 .add(loggingOutInterceptor());
3.4、功能验证
分别启动服务端和客户端,访问客户端请求接口:
http://localhost:7070/user/get?userId=1
客户端控制台输出如下:
12020-11-26 20:22:00.884 INFO 8768 --- [nio-7070-exec-1] o.a.cxf.services.UserService.REQ_OUT : REQ_OUT 2 Address: http://localhost:8080/ws/user 3 HttpMethod: POST 4 Content-Type: text/xml 5 ExchangeId: 3140c6ed-a420-4dee-b970-4f9452b17d17 6 ServiceName: UserServiceService 7 PortName: UserServicePort 8 PortTypeName: UserService 9 Headers: {SOAPAction="", Accept=*/*} 10 Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getUserName xmlns:ns2="http://ws.cxf.ouyushan.org/"><userId>1</userId></ns2:getUserName></soap:Body></soap:Envelope> 11 122020-11-26 20:22:01.272 INFO 8768 --- [nio-7070-exec-1] o.a.cxf.services.UserService.RESP_IN : RESP_IN 13 Address: http://localhost:8080/ws/user 14 Content-Type: text/xml;charset=UTF-8 15 ResponseCode: 200 16 ExchangeId: 3140c6ed-a420-4dee-b970-4f9452b17d17 17 ServiceName: UserServiceService 18 PortName: UserServicePort 19 PortTypeName: UserService 20 Headers: {Keep-Alive=timeout=60, connection=keep-alive, content-type=text/xml;charset=UTF-8, Content-Length=220, Date=Thu, 26 Nov 2020 12:22:01 GMT} 21 Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getUserNameResponse xmlns:ns2="http://ws.cxf.ouyushan.org/"><return>tom</return></ns2:getUserNameResponse></soap:Body></soap:Envelope>
服务端控制台输出如下:
12020-11-26 20:22:01.057 INFO 10604 --- [nio-8080-exec-4] o.a.cxf.services.UserService.REQ_IN : REQ_IN 2 Address: http://localhost:8080/ws/user 3 HttpMethod: POST 4 Content-Type: text/xml; charset=UTF-8 5 ExchangeId: 3a1a8ecf-f857-4472-9f56-165b54144dae 6 ServiceName: UserService 7 PortName: UserServiceImplPort 8 PortTypeName: UserService 9 Headers: {SOAPAction="", Accept=*/*, host=localhost:8080, connection=keep-alive, content-type=text/xml; charset=UTF-8, cache-control=no-cache, Content-Length=202, pragma=no-cache, user-agent=Apache-CXF/3.4.1} 10 Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getUserName xmlns:ns2="http://ws.cxf.ouyushan.org/"><userId>1</userId></ns2:getUserName></soap:Body></soap:Envelope> 11 122020-11-26 20:22:01.108 INFO 10604 --- [nio-8080-exec-4] o.a.cxf.services.UserService.RESP_OUT : RESP_OUT 13 Address: http://localhost:8080/ws/user 14 Content-Type: text/xml 15 ResponseCode: 200 16 ExchangeId: 3a1a8ecf-f857-4472-9f56-165b54144dae 17 ServiceName: UserService 18 PortName: UserServiceImplPort 19 PortTypeName: UserService 20 Headers: {} 21 Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getUserNameResponse xmlns:ns2="http://ws.cxf.ouyushan.org/"><return>tom</return></ns2:getUserNameResponse></soap:Body></soap:Envelope>
从日志可以看出完整的请求链路信息
四、总结
通过本篇教材,您应该已掌握Spring Boot整合CXF实现服务端、客户端日志的配置与输出。整个过程可分为两步:
- 定义日志输入、输出拦截器;
- 在服务端EndPoint中设置日志拦截器/在客户端代理工厂JaxWsProxyFactoryBean中设置日志拦截器。
获取服务日志的问题已解决了,服务就这样能上线吗?尤其是在保险、银行等公司对接时。结果肯定是否定的,针对敏感信息的服务会话,至少得对会话内容进行加解密甚至签名和验签吧。
具体怎么实现会话的加解密、签名及验签将在下一篇教材中进行讲解。