摘录一段网上盛传的,如下:

在使用 spring cloud config 时,如果在 properties 文件里面有中文的话,会出现乱码。

乱码的原因是:spring 默认使用org.springframework.boot.env.PropertiesPropertySourceLoader 来加载配置,底层是通过调用 Properties 的 load 方法,而load方法输入流的编码是 ISO 8859-1

解决方法:实现org.springframework.boot.env.PropertySourceLoader 接口,重写 load 方法

public class MyPropertiesHandler implements PropertySourceLoader {

private static final Logger logger = LoggerFactory.getLogger(MyPropertiesHandler.class);

@Override
public String[] getFileExtensions() {
return new String[]{"properties", "xml"};
}

@Override
public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
if (profile == null) {
Properties properties = getProperties(resource);
if (!properties.isEmpty()) {
PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(name, properties);
return propertiesPropertySource;
}
}
return null;
}

private Properties getProperties(Resource resource) {
Properties properties = new Properties();
InputStream inputStream = null;
try {
inputStream = resource.getInputStream();
properties.load(new InputStreamReader(inputStream, "utf-8"));
inputStream.close();
} catch (IOException e) {
logger.error("load inputstream failure...", e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error("close IO failure ....", e);
}
}
}
return properties;
}
}

在 resources下新建 META-INF 文件夹,新建一个 spring.factories 文件

org.springframework.boot.env.PropertySourceLoader=com.example.configserver.MyPropertiesHandler
1
重启服务,可以看到中文正常显示了

--------------------------------------------------------华丽的分割线-----------------------------------------------------------

尝试各种方法都没有解决,深受其害,查了近一天, 有的说springcloud1.* 和2.*有不同的写法,等等,不一一细说,讲一下最终解决方案。

springcloud Finchley.RELEASE 对应的 springboot-2.0.4

主要是springboot框架中env包下OriginTrackedPropertiesLoader类,159行,有一个ISO8859-1的编码。

1.我们要做的是将spring框架中的OriginTrackedPropertiesLoader类复制到项目中,目录和源码中保持一致,将ISO8859-1编码修改为utf-8;

2.application.yaml中 增加如下配置:

server.tomcat.uri-encoding=utf-8

spring.http.encoding.charset=utf-8

spring.http.encoding.enabled=true

spring.http.encoding.force=true

management.endpoint.health.show-details : always

management.endpoints.web.exposure.include: ${prometheus,refresh}

大功告成,但愿能拯救水深火热的广大猿友!

自动刷新:

1. 在项目中增加 相关actuator jar包

gradle方式:  compile ‘org.springframework.boot:spring-boot-starter-actuator’

maven方式: 吧啦吧啦

2.  在使用到配置参数的类上 增加@RefreshScope

3. 修改完配置文件里的参数后,需要调用 服务层的 刷新接口:  http://localhost:port/actuator/refresh

springcloud config自动刷新中文乱码问题的更多相关文章

  1. SpringCloud Config手动刷新及自动刷新

    1.Config手动刷新a.使用@RefreshScope注解 import org.springframework.beans.factory.annotation.Value; import or ...

  2. Spring Cloud Config 自动刷新所有节点

    全局刷新 详细参考:<Sprin Cloud 与 Docker 微服务架构实战>p160-9.9.2节 1.使用Spring Cloud Config 客户端时,可以使用 /refresh ...

  3. Spring Cloud Config 自动刷新所有节点 架构改造

    详细参考:<Sprin Cloud 与 Docker 微服务架构实战>p162-9.9.4节 要做的改动是: 1.在spring cloud config server 服务端加入 spr ...

  4. SpringCloud-微服务配置统一管理SpringCloud Config(七)

    前言:对于应用,配制文件通常是放在项目中管理的,它可能有spring.mybatis.log等等各种各样的配置文件和属性文件,另外你还可能有开发环境.测试环境.生产环境等,这样的话就得一式三份,若是传 ...

  5. Eclipse的properties文件中文乱码解决方法

    转自:http://jingyan.baidu.com/article/ed2a5d1f3381d709f6be17f8.html 打开Myeclipse,找到window这一栏,点击Preferen ...

  6. 实现SpringCloud Config 客户端自动刷新

    文章来源:https://blog.csdn.net/qq_27385301/article/details/82716218 一.简介 在使用SpringCloud Config客户端时,如果Con ...

  7. SpringCloud Config(配置中心)实现配置自动刷新(十六)

    一.实现原理 1.ConfigServer(配置中心服务端)从远端git拉取配置文件并在本地git一份,ConfigClient(微服务)从ConfigServer端获取自己对应 配置文件: 2.当远 ...

  8. SpringCloud学习之Bus消息总线实现配置自动刷新(九)

    前面两篇文章我们聊了Spring Cloud Config配置中心,当我们在更新github上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新 ...

  9. config配置中心之自动刷新

    自动刷新(自动刷新是基于springcloudbus来实现的,springcloud bus是基于rabbitMQ或者Kafka来实现的) Spring Cloud Bus 将分布式的节点用轻量的消息 ...

随机推荐

  1. adb server is out of date. killing... ADB server didn't ACK * failed to start daemon *……

    问题 使用 adb 命令的时候报错如下: adb server is out of date. killing... ADB server didn't ACK * failed to start d ...

  2. js中this指向的三种情况

    js中this指向的几种情况一.全局作用域或者普通函数自执行中this指向全局对象window,普通函数的自执行会进行预编译,然后预编译this的指向是window //全局作用域 console.l ...

  3. MySQL Binlog--PURGE MASTER LOGS失败

    问题背景: 在我们磁盘空间维护策略中,BINLOG的默认保留期限为7天,但当磁盘空间不足时,会根据磁盘空间使用率自动清理超过一定数量的BINLOG. 问题原因: 某服务器上报磁盘空间不足,登录服务器发 ...

  4. Unable to guess the mime type as no guessers are available 2 9

    做了一个上传图片的功能,在本地上传通过,服务器报 bug Unable to guess the mime type as no guessers are available(Did you enab ...

  5. SQL Server 字段提取拼音首字母

    目前工作中遇到一个情况,需要将SQL Server中的一个字段提取拼音的首字母,字段由汉字.英文.数字以及“-”构成,百度了一堆,找到如下方法,记录一下,以备后用! 首先建立一个函数 --生成拼音首码 ...

  6. centos6/7添加系统服务

    服务脚本必须存放在/etc/ini.d/目录下:       chkconfig --add mongodb     chkconfig --list mongodb     等级0表示:表示关机   ...

  7. kubernetes 故障排除、处理、预防

    kubernetes 故障排除.处理.预防 故障排除顺序和思路 第一步: 我们可以通过查看节点是否正常,一是保证 K8S API Server 是正常的,二是可以查看节点集群网络中是否存在节点异常.如 ...

  8. 隐藏按钮button

    <td> <input id="del" type="button" value="删除" onclick="u ...

  9. InvenSense 美国公司

    InvenSense为智能型运动处理方案的先驱.全球业界的领导厂商,驱动了运动感测人机接口在消费性电子产品上的应用.公司提供的集成电路(IC)整合了运动传感器-陀螺仪以及相对应的软件,有别于其他厂商, ...

  10. python算法与数据结构-希尔排序算法(35)

    一.希尔排序的介绍 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法. 希尔排序是把记录按下标的一定增量分组,对每 ...