SpringCloud-29-Spring Cloud Config手动刷新配置

在人生的道路上,不管是潇洒走一回,或者是千山独行,皆须是自己想走的路,虽然,有的人并不是很快就能找到自己的方向和道路,不过,只要坚持到底,我相信,就一定可以找到自己的路,只要找到路,就不必怕路途遥远了。

导读:本篇文章讲解 SpringCloud-29-Spring Cloud Config手动刷新配置,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

11.6 手动刷新配置

  • 将本地仓库的application.yml文件修改下信息,在提交到仓库上去,也可以在gitee上进行编辑
    在这里插入图片描述
    在这里插入图片描述

  • 然后在不重启配置中心microservice-cloud-config-server-8016服务前提下,浏览器访问http://localhost:8016/master/application-dev.yml,结果如下:
    在这里插入图片描述

  • 可以看出配置中心已经成功地获取到了修改后的配置。

  • 同样在不重启客户端microservice-cloud-config-client-8017服务前提下,浏览器访问http://localhost:8017/getInfo,尝试通过 Spring Cloud Config 客户端获取修改后的配置信息。结果如下:
    在这里插入图片描述

  • 这次重启客户端microservice-cloud-config-client-8017服务前提下,浏览器访问http://localhost:8017/getInfo。结果如下:
    在这里插入图片描述

  • 由此可以看出

配置更新后,Config Server可以直接从 Git 仓库中获取最新的配置。

必须重启 Spring Cloud Config 客户端(Client),重新连接Config Server配置中心,否则无法通过 Spring Cloud Config 服务端获取最新的配置信息。

  • 要解决不重启 Config 客户端无法获取最新配置的问题,接下来,我们就对 microservice-cloud-config-client-8017进行修改,修改如下。

  • 我们需要让Config 客户端监控到配置文件的变化那就需要spring-boot-starter-actuator依赖的支持,这就是导入此依赖的目的

  • 在配置文件 bootstrap.yml 中添加以下配置,对外暴露 Spring Boot actuator 的监控节点。

# spring cloud 使用 Spring Boot actuator 监控完善信息
# Spring Boot 2.50对 actuator 监控屏蔽了大多数的节点,只暴露了 heath 节点,本段配置(*)就是为了开启所有的节点
management:
  endpoints:
    web:
      exposure:
        include:  "*" #应包含的端点 ID 或 '' 全部,* 在yaml 文件属于关键字,所以需要加双引号
        #include的值也可以改成*,但建议还是最小暴露原则,用啥开启啥health,info,hystrix:stream
  • Spring Boot Actuator 模块提供了生产级别的功能,比如健康检查,审计,指标收集,HTTP 跟踪等,帮助我们监控和管理Spring Boot 应用。
  • 能采集应用内部信息暴露给外部的模块,上述的功能都可以通过HTTP 和 JMX 访问,所以Actuator 也可以和一些外部的应用监控系统整合,提供仪表板,图形,分析和警报等界面,监视和管理应用程序。如果使用不当或者一些不经意的疏忽,可能造成信息泄露等严重的安全隐患。
  • Actuator 内置的端点Endpoints类型,它用来监视应用程序及交互,也允许我们自己扩展自己的 Endpoints
Http方法 Endpoint 介绍
get /autoconfig 记录哪些自动配置的使用情况
get /beans 查看应用程序上下文里的Bean列表,以及它们的关系
get /metrics 查看应用程序所有度量信息,如内存用量和HTTP请求计数
get /metrics/{name} 查看应用程序具体度量信息
get /dump 打印线程活动的快照
get /mappings 查看所有url映射,以及它们和控制器(包含Actuator端点)的映射关系(最好不打开,对外暴露了接口地址,易受攻击)
get /health 查看程序的健康指标,这些值由HealthIndicator的实现类提供
get /info 查看应用程序的自定义信息,这些信息由info打头的属性提供。
get /trace 查看基本追踪信息,通过访问/trace路径,可获取用户认证字段信息,如token,cookie 等重要权限验证信息(关闭)
get /heapdump 访问/heapdump 路径,返回 gzip 压缩 hprof 堆转储文件。在 Android studio 打开,会泄露站点内存信息,很多时候会包含后台用户的账号密码
get /configprops 查看属性配置,包括默认值,如何注入Bean。容易暴露ip地址,eureka地址,endpoints哪些可访问等(关闭,容易泄露其他注册信息)
get /env 查看所有环境变量,像操作系统信息,虚拟机信息,应用信息,用户信息,三方服务信息(关闭)
get /env 查看具体环境变量
post /shutdown 关闭应用,(关闭,否则可远程关闭应用)
get /loggers 查看所有包的日志级别,默认为info,可动态修改

断点关闭格式endpoints.env.enabled=true

新版本可配置端点,在集成了spring-boot-starter-actuator的项目中访问http://localhost:8017/actuator可以显示全部的可配置端点

<!-- Spring Boot 监控模块,完善监控信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
{
    "_links": {
        "self": {
            "href": "http://localhost:8017/actuator",
            "templated": false
        },
        "archaius": {
            "href": "http://localhost:8017/actuator/archaius",
            "templated": false
        },
        "beans": {
            "href": "http://localhost:8017/actuator/beans",
            "templated": false
        },
        "caches-cache": {
            "href": "http://localhost:8017/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "http://localhost:8017/actuator/caches",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8017/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8017/actuator/health/{*path}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:8017/actuator/info",
            "templated": false
        },
        "conditions": {
            "href": "http://localhost:8017/actuator/conditions",
            "templated": false
        },
        "configprops": {
            "href": "http://localhost:8017/actuator/configprops",
            "templated": false
        },
        "configprops-prefix": {
            "href": "http://localhost:8017/actuator/configprops/{prefix}",
            "templated": true
        },
        "env": {
            "href": "http://localhost:8017/actuator/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://localhost:8017/actuator/env/{toMatch}",
            "templated": true
        },
        "integrationgraph": {
            "href": "http://localhost:8017/actuator/integrationgraph",
            "templated": false
        },
        "loggers": {
            "href": "http://localhost:8017/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://localhost:8017/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://localhost:8017/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://localhost:8017/actuator/threaddump",
            "templated": false
        },
        "metrics-requiredMetricName": {
            "href": "http://localhost:8017/actuator/metrics/{requiredMetricName}",
            "templated": true
        },
        "metrics": {
            "href": "http://localhost:8017/actuator/metrics",
            "templated": false
        },
        "scheduledtasks": {
            "href": "http://localhost:8017/actuator/scheduledtasks",
            "templated": false
        },
        "mappings": {
            "href": "http://localhost:8017/actuator/mappings",
            "templated": false
        },
        "refresh": {
            "href": "http://localhost:8017/actuator/refresh",
            "templated": false
        },
        "busenv-destinations": {
            "href": "http://localhost:8017/actuator/busenv/{*destinations}",
            "templated": true
        },
        "busenv": {
            "href": "http://localhost:8017/actuator/busenv",
            "templated": false
        },
        "busrefresh": {
            "href": "http://localhost:8017/actuator/busrefresh",
            "templated": false
        },
        "busrefresh-destinations": {
            "href": "http://localhost:8017/actuator/busrefresh/{*destinations}",
            "templated": true
        },
        "features": {
            "href": "http://localhost:8017/actuator/features",
            "templated": false
        },
        "serviceregistry": {
            "href": "http://localhost:8017/actuator/serviceregistry",
            "templated": false
        },
        "functions": {
            "href": "http://localhost:8017/actuator/functions",
            "templated": false
        },
        "bindings-name": {
            "href": "http://localhost:8017/actuator/bindings/{name}",
            "templated": true
        },
        "bindings": {
            "href": "http://localhost:8017/actuator/bindings",
            "templated": false
        },
        "channels": {
            "href": "http://localhost:8017/actuator/channels",
            "templated": false
        }
    }
}
  • 在 ConfigClientController 类上使用 @RefreshScope 注解开启配置刷新,代码如下
package com.example.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 读取配置中心指定配置文件的内容,并展示到页面
 * 为了让手动(也是一种动态方式,不重启服务)的获取最新的git 配置,在添加 actuator 监控加载 RefreshScope,
 */
@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${server.port}")
    private String port;

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${config.info}")
    private String info;

    @Value("${config.version}")
    private String version;

    @GetMapping("/getInfo")
    public String getInfo(){
        return "applicationName is:"+applicationName+" <br/>port is:"+port+"<br/>info is:"+info+"<br/>version is"+version;
    }
}
  • 重启 microservice-cloud-config-client-8017,再次修改本地仓库的application.yml文件信息,在提交到仓库上去
  • 修改前
    在这里插入图片描述
  • 修改后
    在这里插入图片描述
  • 使用浏览器再次访问“http://localhost:8017/getInfo”,结果如下图。
    在这里插入图片描述
  • 发现还是无法直接获取到最新配置,这需要,打开命令行窗口,手动的使用以下命令发送一个 POST 请求刷新 Client 8017客户端,通知客户端配置文件已经修改,需要重新拉去配置。
curl -X POST "http://localhost:8017/actuator/refresh"

在这里插入图片描述

  • 使用浏览器再次访问“http://localhost:8017/getInfo”,结果如下图。
    在这里插入图片描述

问题记录:在使用TortoiseGit工具(小乌龟)向远程仓库推送文件的时候会报错 no supported authentication methods avaiable错误
在这里插入图片描述
在这里插入图片描述

原因:TortoiseGit的网络设置里面,SSH客户端使用了自己的TortoiseGitPlink.exe工具,不是git的,而TortoiseGit和Git冲突。

  • TortoiseGit安装后,右键-TortoiseGit-设置-网络
    在这里插入图片描述
  • 将SSH client指向E:\software\Git**\usr\bin\ssh.exe**
    在这里插入图片描述
  • 设置后点击应用,然后提交文件到仓库,结果如下:
    在这里插入图片描述

下一篇:SpringCloud-31-Spring Cloud Config+Bus 实现配置的动态刷新

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/123794.html

(0)

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!