首页 > 其他分享 >springcloud的configserver配置中心默认中文乱码的解决方法

springcloud的configserver配置中心默认中文乱码的解决方法

时间:2023-06-01 13:37:05浏览次数:31  
标签:return springcloud org private 乱码 IOException reader import configserver

springcloud的configserver配置中心默认中文乱码的解决方法

先表明我的springcloud版本(2021.0.6)和对应springboot版本(2.6.14)


Release Train Version: 2021.0.6



Supported Boot Version: 2.6.14


https://docs.spring.io/spring-cloud/docs/2021.0.6/reference/html/

springcloud的configserver配置中心默认中文乱码的解决方法_spring

 



问题描述:

当从远端拉取的properties配置文件中含有中文,呈现乱码



产生原因:

spring 中默认使用PropertiesPropertySourceLoader进行properties文件解析,在解析过程会利用OriginTrackedPropertiesLoader创建CharacterReader, 而问题就出现在这个CharacterReader身上,内部指定了ISO_8859_1编码,导致解析中文乱码!



解决方法:

对于spring cloud config组件由两部分组成,分别是configServer和configClient,configClient向configServer发起配置请求,configServer从github上拉取配置并在本地缓存,而后对配置文件进行解析,最后将配置发送个configClient, 所以,问题主要解决点在configServer方!。

 

注意:下述操作在configServer方!!!

创建两个自定义对象,分别是CustomizedOriginTrackedPropertiesLoader和CustomizedPropertiesPropertySourceLoader分别对应源码的OriginTrackedPropertiesLoader和PropertiesPropertySourceLoader,可以对着源码直接ctrl+cv,而后改一下名字,即可,两者放在同一包,OriginTrackedPropertiesLoader访问权限默认是包私有,自定义的可以改!

注意:为了让自定义的CustomizedPropertiesPropertySourceLoader优先级高于系统自带的PropertiesPropertySourceLoader,需要添加@Order注解,系统默认优先级最低(2147483647),我们直接Ordered.LOWEST_PRECEDENCE - 1

 

我这里是在configserver应用创建了一个encode的子包

springcloud的configserver配置中心默认中文乱码的解决方法_spring_02

 



CustomizedPropertiesPropertySourceLoader.java

package com.imddysc.configserver.encode;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
//import org.springframework.boot.env.OriginTrackedPropertiesLoader;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Order(Ordered.LOWEST_PRECEDENCE - 1)
public class CustomizedPropertiesPropertySourceLoader implements PropertySourceLoader {
    private static final Logger logger = LoggerFactory.getLogger(CustomizedPropertiesPropertySourceLoader.class);


    private static final String XML_FILE_EXTENSION = ".xml";

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

    @Override
    public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
        List<Map<String, ?>> properties = loadProperties(resource);
        if (properties.isEmpty()) {
            return Collections.emptyList();
        }
        List<PropertySource<?>> propertySources = new ArrayList<>(properties.size());
        for (int i = 0; i < properties.size(); i++) {
            String documentNumber = (properties.size() != 1) ? " (document #" + i + ")" : "";
            propertySources.add(new OriginTrackedMapPropertySource(name + documentNumber,
                    Collections.unmodifiableMap(properties.get(i)), true));
        }
        return propertySources;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private List<Map<String, ?>> loadProperties(Resource resource) throws IOException {
        String filename = resource.getFilename();
        List<Map<String, ?>> result = new ArrayList<>();
        if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
            result.add((Map) PropertiesLoaderUtils.loadProperties(resource));
        }
        else {
            List<CustomizedOriginTrackedPropertiesLoader.Document> documents = new CustomizedOriginTrackedPropertiesLoader(resource).load();
            documents.forEach((document) -> result.add(document.asMap()));
        }
        return result;
    }

}

 



CustomizedOriginTrackedPropertiesLoader.java

package com.imddysc.configserver.encode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.boot.env.OriginTrackedPropertiesLoader;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.boot.origin.TextResourceOrigin;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BooleanSupplier;

public class CustomizedOriginTrackedPropertiesLoader {
    private static final Logger logger = LoggerFactory.getLogger(CustomizedOriginTrackedPropertiesLoader.class);

    private final Resource resource;

    /**
     * Create a new {@link OriginTrackedPropertiesLoader} instance.
     * @param resource the resource of the {@code .properties} data
     */
    CustomizedOriginTrackedPropertiesLoader(Resource resource) {
        Assert.notNull(resource, "Resource must not be null");
        this.resource = resource;
    }

    /**
     * Load {@code .properties} data and return a list of documents.
     * @return the loaded properties
     * @throws IOException on read error
     */
    List<CustomizedOriginTrackedPropertiesLoader.Document> load() throws IOException {
        return load(true);
    }

    /**
     * Load {@code .properties} data and return a map of {@code String} ->
     * {@link OriginTrackedValue}.
     * @param expandLists if list {@code name[]=a,b,c} shortcuts should be expanded
     * @return the loaded properties
     * @throws IOException on read error
     */
    List<CustomizedOriginTrackedPropertiesLoader.Document> load(boolean expandLists) throws IOException {
        List<CustomizedOriginTrackedPropertiesLoader.Document> documents = new ArrayList<>();
        CustomizedOriginTrackedPropertiesLoader.Document document = new CustomizedOriginTrackedPropertiesLoader.Document();
        StringBuilder buffer = new StringBuilder();
        try (CustomizedOriginTrackedPropertiesLoader.CharacterReader reader = new CustomizedOriginTrackedPropertiesLoader.CharacterReader(this.resource)) {
            while (reader.read()) {
                if (reader.isCommentPrefixCharacter()) {
                    char commentPrefixCharacter = reader.getCharacter();
                    if (isNewDocument(reader)) {
                        if (!document.isEmpty()) {
                            documents.add(document);
                        }
                        document = new CustomizedOriginTrackedPropertiesLoader.Document();
                    }
                    else {
                        if (document.isEmpty() && !documents.isEmpty()) {
                            document = documents.remove(documents.size() - 1);
                        }
                        reader.setLastLineCommentPrefixCharacter(commentPrefixCharacter);
                        reader.skipComment();
                    }
                }
                else {
                    reader.setLastLineCommentPrefixCharacter(-1);
                    loadKeyAndValue(expandLists, document, reader, buffer);
                }
            }

        }
        if (!document.isEmpty() && !documents.contains(document)) {
            documents.add(document);
        }
        return documents;
    }

    private void loadKeyAndValue(boolean expandLists, CustomizedOriginTrackedPropertiesLoader.Document document, CustomizedOriginTrackedPropertiesLoader.CharacterReader reader, StringBuilder buffer)
            throws IOException {
        String key = loadKey(buffer, reader).trim();
        if (expandLists && key.endsWith("[]")) {
            key = key.substring(0, key.length() - 2);
            int index = 0;
            do {
                OriginTrackedValue value = loadValue(buffer, reader, true);
                document.put(key + "[" + (index++) + "]", value);
                if (!reader.isEndOfLine()) {
                    reader.read();
                }
            }
            while (!reader.isEndOfLine());
        }
        else {
            OriginTrackedValue value = loadValue(buffer, reader, false);
            document.put(key, value);
        }
    }

    private String loadKey(StringBuilder buffer, CustomizedOriginTrackedPropertiesLoader.CharacterReader reader) throws IOException {
        buffer.setLength(0);
        boolean previousWhitespace = false;
        while (!reader.isEndOfLine()) {
            if (reader.isPropertyDelimiter()) {
                reader.read();
                return buffer.toString();
            }
            if (!reader.isWhiteSpace() && previousWhitespace) {
                return buffer.toString();
            }
            previousWhitespace = reader.isWhiteSpace();
            buffer.append(reader.getCharacter());
            reader.read();
        }
        return buffer.toString();
    }

    private OriginTrackedValue loadValue(StringBuilder buffer, CustomizedOriginTrackedPropertiesLoader.CharacterReader reader, boolean splitLists)
            throws IOException {
        buffer.setLength(0);
        while (reader.isWhiteSpace() && !reader.isEndOfLine()) {
            reader.read();
        }
        TextResourceOrigin.Location location = reader.getLocation();
        while (!reader.isEndOfLine() && !(splitLists && reader.isListDelimiter())) {
            buffer.append(reader.getCharacter());
            reader.read();
        }
        Origin origin = new TextResourceOrigin(this.resource, location);
        return OriginTrackedValue.of(buffer.toString(), origin);
    }

    private boolean isNewDocument(CustomizedOriginTrackedPropertiesLoader.CharacterReader reader) throws IOException {
        if (reader.isSameLastLineCommentPrefix()) {
            return false;
        }
        boolean result = reader.getLocation().getColumn() == 0;
        result = result && readAndExpect(reader, reader::isHyphenCharacter);
        result = result && readAndExpect(reader, reader::isHyphenCharacter);
        result = result && readAndExpect(reader, reader::isHyphenCharacter);
        if (!reader.isEndOfLine()) {
            reader.read();
            reader.skipWhitespace();
        }
        return result && reader.isEndOfLine();
    }

    private boolean readAndExpect(CustomizedOriginTrackedPropertiesLoader.CharacterReader reader, BooleanSupplier check) throws IOException {
        reader.read();
        return check.getAsBoolean();
    }

    /**
     * Reads characters from the source resource, taking care of skipping comments,
     * handling multi-line values and tracking {@code '\'} escapes.
     */
    private static class CharacterReader implements Closeable {

        private static final String[] ESCAPES = { "trnf", "\t\r\n\f" };

        private final LineNumberReader reader;

        private int columnNumber = -1;

        private boolean escaped;

        private int character;

        private int lastLineCommentPrefixCharacter;

        CharacterReader(Resource resource) throws IOException {
            logger.info("-----显示这行就代表加载了UTF来解析对应的Resource代指的properties文件----------------------------------------------------");
            this.reader = new LineNumberReader(
                    new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
        }

        @Override
        public void close() throws IOException {
            this.reader.close();
        }

        boolean read() throws IOException {
            this.escaped = false;
            this.character = this.reader.read();
            this.columnNumber++;
            if (this.columnNumber == 0) {
                skipWhitespace();
            }
            if (this.character == '\\') {
                this.escaped = true;
                readEscaped();
            }
            else if (this.character == '\n') {
                this.columnNumber = -1;
            }
            return !isEndOfFile();
        }

        private void skipWhitespace() throws IOException {
            while (isWhiteSpace()) {
                this.character = this.reader.read();
                this.columnNumber++;
            }
        }

        private void setLastLineCommentPrefixCharacter(int lastLineCommentPrefixCharacter) {
            this.lastLineCommentPrefixCharacter = lastLineCommentPrefixCharacter;
        }

        private void skipComment() throws IOException {
            while (this.character != '\n' && this.character != -1) {
                this.character = this.reader.read();
            }
            this.columnNumber = -1;
        }

        private void readEscaped() throws IOException {
            this.character = this.reader.read();
            int escapeIndex = ESCAPES[0].indexOf(this.character);
            if (escapeIndex != -1) {
                this.character = ESCAPES[1].charAt(escapeIndex);
            }
            else if (this.character == '\n') {
                this.columnNumber = -1;
                read();
            }
            else if (this.character == 'u') {
                readUnicode();
            }
        }

        private void readUnicode() throws IOException {
            this.character = 0;
            for (int i = 0; i < 4; i++) {
                int digit = this.reader.read();
                if (digit >= '0' && digit <= '9') {
                    this.character = (this.character << 4) + digit - '0';
                }
                else if (digit >= 'a' && digit <= 'f') {
                    this.character = (this.character << 4) + digit - 'a' + 10;
                }
                else if (digit >= 'A' && digit <= 'F') {
                    this.character = (this.character << 4) + digit - 'A' + 10;
                }
                else {
                    throw new IllegalStateException("Malformed \\uxxxx encoding.");
                }
            }
        }

        boolean isWhiteSpace() {
            return !this.escaped && (this.character == ' ' || this.character == '\t' || this.character == '\f');
        }

        boolean isEndOfFile() {
            return this.character == -1;
        }

        boolean isEndOfLine() {
            return this.character == -1 || (!this.escaped && this.character == '\n');
        }

        boolean isListDelimiter() {
            return !this.escaped && this.character == ',';
        }

        boolean isPropertyDelimiter() {
            return !this.escaped && (this.character == '=' || this.character == ':');
        }

        char getCharacter() {
            return (char) this.character;
        }

        TextResourceOrigin.Location getLocation() {
            return new TextResourceOrigin.Location(this.reader.getLineNumber(), this.columnNumber);
        }

        boolean isSameLastLineCommentPrefix() {
            return this.lastLineCommentPrefixCharacter == this.character;
        }

        boolean isCommentPrefixCharacter() {
            return this.character == '#' || this.character == '!';
        }

        boolean isHyphenCharacter() {
            return this.character == '-';
        }

    }

    /**
     * A single document within the properties file.
     */
    static class Document {

        private final Map<String, OriginTrackedValue> values = new LinkedHashMap<>();

        void put(String key, OriginTrackedValue value) {
            if (!key.isEmpty()) {
                this.values.put(key, value);
            }
        }

        boolean isEmpty() {
            return this.values.isEmpty();
        }

        Map<String, OriginTrackedValue> asMap() {
            return this.values;
        }

    }

}

 



在resources目录下创建META-INF文件夹,然后创建spring.factories文件

在spring.factories文件里添加如下内容:

org.springframework.boot.env.PropertySourceLoader=com.imddysc.configserver.encode.CustomizedPropertiesPropertySourceLoader

 



附:

springcloud的configserver配置中心默认中文乱码的解决方法_configserver_03


 



运行结果:

springcloud的configserver配置中心默认中文乱码的解决方法_java_04

 

 

 

标签:return,springcloud,org,private,乱码,IOException,reader,import,configserver
From: https://blog.51cto.com/lenglingx/6393368

相关文章

  • unity随机生成乱码图片并保存本地,各项参数均可调整
    @TOC<hrstyle="border:solid;width:100px;height:1px;"color=#000000size=1">前言最近有个小需求,要生成随机的乱码图片,用于ar的识别,于是我写了这个小demo,有需要的小伙伴可以拿去用,我也是借此留个备份。<hrstyle="border:solid;width:100px;height:1px;"color=#000000si......
  • springboot-解决项目编译后resources下文件生成乱码问题
    SpringBoot项目下resources文件项目编译之后resources下文件会生成乱码,是说明maven打包的时候出现问题缺少一个插件<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId>......
  • 终于解决了cplex12.8中文报错乱码的问题
     最终通过把系统语言改成英文解决,报错变成英语,就能看懂了。上面那条以前试过,没成功,把下面那条一起改了,终于成功了,其他软件确实出现了一点问题,但可以忍受,大不了再改回去,找到办法就好。实在是不想重新安装这个软件了。  尝试过其他方法:配置外部OPL,在控制器里看报错失败:我......
  • JavaWeb 解决乱码问题
    自写过滤器解决文件结构代码配置EncondingFilerpackagefilter;importjavax.servlet.*;importjava.io.IOException;publicclassEncondingFilterimplementsFilter{@Overridepublicvoidinit(FilterConfigfilterConfig)throwsServletException{......
  • 解决Jenkins控制台日志中文乱码
    前提:Jenkins部署在Windows服务器的tomcat容器里,执行python时控制台日志中文乱码step1:设置Jenkins环境变量:系统管理→系统设置→全局属性→环境变量JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8PYTHONIOENCODING=UTF8step2:设置jenkins所在服务器环境变量:右键我的电脑→属性→高级系......
  • pycharm debug中文乱码解决办法
    setting-->editor-->fileencoding编码改成utf-8 控制面板-->时钟和区域-->日期和时间-->更改日期个时间-->更改日历设置-->管理-->更改系统区域设置-->Beta版:使用UnicodeUTF-8提供全球语言支持(U)勾线上 设置完重启电脑OK ......
  • SpringCloudAlibaba整合分布式事务Seata
    目录1整合分布式事务Seata1.1环境搭建1.1.1Nacos搭建1.1.2Seata搭建1.2项目搭建1.2.1项目示意1.2.2pom.xml1.2.2.1alibaba-demo模块1.2.2.2call模块1.2.2.3order模块1.2.2.4common模块1.2.3配置文件1.2.3.1order模块1.2.3.2call模块1.2.4OpenFeign调用1.2.5order......
  • centos7.6 终端显示乱码解决只要一步
    解释一下乱码原因,服务器编码和终端工具不一致。但只要支持utf-8,不管终端工具和服务器怎么编码都可以正确显示。所以先看终端工具是不是utf-8,我的终端工具显示如下,是UTF-8 再检查服务器编码,使用命令locale看服务器编码,或者用echo$LANG明显没有UTF-8字样。那就修改服务......
  • ajax乱码问题和异步同步问题
    1. 测试内容: 201.1 发送ajax get请求    发送数据到服务器,服务器获取的数据是否乱码?    - 服务器响应给前端的中文,会不会乱码?1.2 发送ajax post请求    - 发送数据到服务器,服务器获取的数据是否乱码?    - 服务器响应给前端的中文,会不会乱码?1.3 包括还要......
  • 解决mysqldump 导出中文乱码的问题
    导数据库mysqldump-uroot-p111111-P3306-h127.0.0.1test>/data/test.sql导出后的数据库打开是乱码,如下:开始以为打开的方式不对,就用记事本打开后,用utf-8的编码格式另保存下结果打开后,仍然是乱码。这时候,猜测是不是数据库的字符集的编码有问题,然后进入数据库,输入命......