使用spring websocket实时读取k8s容器日志输出

1.首先是k8s的api地址

get请求 日志实时输出。一直读流。和前端保持连接即可。 container:容器名称 pretty=true:格式化输出 follow=true:实时输出,长连接,默认为false

具体参见官方api:


遇到的坑: 1.web.xml要设置项目3.0以上

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

2.要扫描包

3.加上namespace

xmlns:websocket="http://www.springframework.org/schema/websocket"  
        http://www.springframework.org/schema/websocket   
        http://www.springframework.org/schema/websocket/spring-websocket.xsd">

4.spring版本要是4.0以上的

5.websocket方式连接的话,一定不要加上.withSocketJS()

registry.addHandler(SocketHandler(),uri)
                .setAllowedOrigins("*")
                .addInterceptors(new LogHandShakeInteceptor());
经验分享 程序员 微信小程序 职场和发展