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

来源:互联网 发布:儿童节礼物 知乎 编辑:程序博客网 时间:2024/06/04 18:05

1.首先是k8s的api地址

http://10.1.1.1:8080/api/v1/namespaces/default/pods/pod-ming/log?container=service-6744&pretty=true&follow=true
get请求 日志实时输出。一直读流。和前端保持连接即可。
container:容器名称
pretty=true:格式化输出
follow=true:实时输出,长连接,默认为false

具体参见官方api:
http://kubernetes.io/kubernetes/third_party/swagger-ui/#!/api%2Fv1/readNamespacedPodLog


遇到的坑:
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.要扫描包

    <context:component-scan base-package="com.wlqq.cloud.core.websocket" />

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());
0 0