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

在使用 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. ArcEngine二次开发中运行出现There is no Spatial Analyst license currently available or enabled.

    只需要在许可控件上勾选空间分析功能即可.

  2. 【idea】scala&sbt+idea安装配置与测试

    一.IDEA安装 下载Community版的IDEA,Ultimate是免费试用版(相当于用到后面要给钱的) ideaIC-2019.2.3.tar.gz 解压IDEA: tar -zxvf idea ...

  3. ORACLE 清理SYSAUX表空间

    在数据库检查中发现SYSAUX表空间占用过大,SYSAUX是ORACLE10G开始提供的功能,用于数据库为SYSTEM表空间减负. 用以下语句查出相应的表空间值 select a.tablespace ...

  4. RabbitMQ java 原生代码

    rabbitMQ 的交换器有四种类型:direct.fanout.topic.headers 以下是具体的代码: direct:路由键只能全部匹配,才能进入到指定队列中.其他使用 direct生产者 ...

  5. error 106: Can't Access ASP.NET\ClientFiles\

    Error 1606 Can’t access ASP.NET\ClientFiles\ when installing Crystal Reports Support Pack 10     Sea ...

  6. [daily] 使用thunderbird通过davmail代理访问Microsoft Exchange Service(OWA)

    前言 我需要接入某企业的邮件服务器, 该服务器没有开通pop3, 没有smtp, 没有imap, 只有exchange. 也就是说必须要使用outlook才能访问. 但是我没有outlook. 方案一 ...

  7. 华为云和开源Istio运维管理对比样例应用部署

    前言 在公有云方面,华为云已经率先将 Istio 作为产品投入到公有云中进行商业应用中,保持和开源istio高度兼容,做了商业化的运维管理界面,同时进行了性能优化.这里我们做一次验证测试. Booki ...

  8. 十分钟了解pandas

    十分钟掌握Pandas(上)--来自官网API 一.numpy和pandas numpy是矩阵计算库,pandas是数据分析库,关于百度百科,有对pandas的介绍. pandas 是基于NumPy ...

  9. netty websocket

    1 全局保存websocket的通道  NettyConfig.java public class NettyConfig { public static ChannelGroup group = n ...

  10. springboot学习笔记(一)

    springboot案例(一) Application.java package com.xdr.spring; import org.springframework.boot.SpringAppli ...