【问题标题】:How to change context path of spring-boot actuator?如何更改 spring-boot 执行器的上下文路径?
【发布时间】:2022-10-07 22:35:38
【问题描述】:

我为我的spring-boot 应用程序创建了一个docker 容器,并启用了actuator 端点。假设容器在端口 8080 上运行,它可以通过localhost:8080/actuator 访问,它公开端点如下:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        ...
}

问题:我在 apache2 代理后面,它重定向如下:

ProxyPass https://myserver.com/api http://localhost:8080
ProxyPassReverse https://myserver.com/api http://localhost:8080

现在如果我转到https://myserver.com/api/actuator,我可以看到端点,但这里缺少重写的上下文路径:http://myserver/actuator/health

问题:如何强制 Spring 使用额外的上下文路径 /api 构建管理端点路径?

我想要的执行器端点输出是:

http://myserver/api/actuator/health

{
    "_links": {
        "self": {
            "href": "http://myserver/api/actuator",
            "templated": false
        },
        "health": {
            "href": "http://myserver/api/actuator/health",
            "templated": false
        },
        ... should be applied to all endpoints
}

那可能吗?

【问题讨论】:

    标签: spring spring-boot spring-actuator


    【解决方案1】:

    其实这也不是什么可以直接在spring中解决的,只是在前面的代理中加上X-Forwarded-Prefix

    阿帕奇2配置:

    <VirtualHost *:443>
        <Location "/api">
            ProxyPass http://localhost:8080
            ProxyPassReverse http://localhost:8080
            
            RequestHeader set X-Forwarded-Prefix "/api"
        </Location>
    </VirtualHost>
    

    应用程序属性: server.forward-headers-strategy=framework

    【讨论】:

      【解决方案2】:

      您可以更改文件路径应用程序属性

      应用程序属性

      management.endpoints.web.base-path=/v1
      management.endpoints.web.path-mapping.health=test

      【讨论】:

        猜你喜欢
        • 2018-08-17
        • 1970-01-01
        • 1970-01-01
        • 2019-08-22
        • 1970-01-01
        • 2016-03-05
        • 1970-01-01
        • 1970-01-01
        • 2018-11-25
        相关资源
        最近更新 更多