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

在使用 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. Part_four:redis主从复制

    redis主从复制 1.redis主从同步 Redis集群中的数据库复制是通过主从同步来实现的 主节点(Master)把数据分发从节点(slave) 主从同步的好处在于高可用,Redis节点有冗余设计 ...

  2. Android数据存储原理分析

    Android上常见的数据存储方式为: SharedPreferences是 Android 中比较常用的存储方法,本篇将从源码角度带大家分析一下Android中常用的轻量级数据存储工具SharedP ...

  3. 【亲测可行,图片宽度高度自适应】c# Graphics MeasureString精确测量字体宽度

    , , ) { int count = number.Length; //需要配置的字段 //Font f = new Font("Microsoft Sans Serif", f ...

  4. sql基本常识[未完待续]

    ---------------------------------------------------------------------------------------------------- ...

  5. js 删除 数组中某个元素(转载)

    来源:https://www.jb51.net/article/134312.htm js删除数组中某一项或几项的几种方法 https://www.jb51.net/article/154737.ht ...

  6. 迷你商城后台管理系统————stage2核心代码实现

    应用程序主函数接口 @SpringBootApplication(scanBasePackages = {"org.linlinjava.litemall.db", "o ...

  7. XSL-FO知识点【一】

    XSL-FO 用于格式化供输出的 XML 数据. 什么是 XSL-FO? XSL-FO 是用于格式化 XML 数据的语言 XSL-FO 指可扩展样式表语言格式化对象(Extensible Styles ...

  8. python - django (路由)

    # """ # Django路由分配系统简介: Django project目录中的urls.py文件中, 以Python [ ( ) ]的数据类型记录了可以访问到该站点 ...

  9. 07 Node.js安装及环境配置

    二.安装Node.js步骤 1.下载对应你系统的Node.js版本:https://nodejs.org/en/download/2.选安装目录进行安装3.环境配置4.测试 开始安装 1.下载完成后, ...

  10. Spark运行架构及作业提交流程

    1.yarn-cluster模式: (1)client客户端提交spark Application应用程序到yarn集群. (2)ResourceManager收到了请求后,在集群中选择一个NodeM ...