微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringCloud---Config配置中心

SpringClud Config概述 

  Spring Cloud Config一个解决分布式系统的配置管理方案,它包含了 server 和 client 两个部分。

  server 用来获取远程的配置信息(认为 Git 仓库),并且以接口的形式提供出去;

  client 根据 server 提供的接口读取配置文件,以便于初始化自己的应用。

  如果配置中心出现了问题,将导致灾难性的后果,因此在生产环境下配置中心都会做集群,来保证高可用。

  此处配置高可用实际就是把多个配置中心(指定同一个 Git 远程仓库)注册注册中心

  官方文档:https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html

  帮助文档:https://spring.io/guides/gs/centralized-configuration/

Config Server

  首先我们基于之前的代码,在springCloud工程下面新建一个Config Server,是一个springboot项目,并且在Eureka上面注册服务(还不会服务注册与发现的,请戳:SpringCloud——Eureka 服务注册与发现),本例使用的是GitHub

 maven中引入相关依赖

        <!-- config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

登录GitHub,新建一个public仓库:config-server,并且添加测试项目对应的配置文件:myspringboot-dev.properties,并设置几个值

 

 配置文件

server.port=1112
spring.application.name=config-server
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
#健康检查(需要spring-boot-starter-actuator依赖)
eureka.client.healthcheck.enabled=true
# 续约更新时间间隔(认30秒)
eureka.instance.lease-renewal-interval-in-seconds=10
# 续约到期时间(认90秒)
eureka.instance.lease-expiration-duration-in-seconds=10

#连接GitHub
spring.cloud.config.server.git.uri=https://[email protected]:junhuai1994/SpringCloud.git//config-server/
spring.cloud.config.server.git.search-paths=config-server
spring.cloud.config.label=master
spring.cloud.config.server.git.username=******
spring.cloud.config.server.git.password=******

启动类加入注解@Enableconfigserver

@Enableconfigserver
@EnableEurekaClient
@SpringBootApplication
public class configserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(configserverApplication.class, args);
    }
}

启动项目,访问http://localhost:1112/myspringboot-dev.properties/,发现有中文乱码

 

解决中文乱码,参考:https://blog.csdn.net/sinat_38843093/article/details/79960777

  新建自定义解析器MyPropertiesHandler,继承PropertiesPropertySourceLoader,重写方法

/**
 * 解决中文乱码问题
 * 参考:https://blog.csdn.net/sinat_38843093/article/details/79960777
 */
public class MyPropertiesHandler extends PropertiesPropertySourceLoader {

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

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

    private Properties getProperties(Resource resource) throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = resource.getInputStream();
        properties.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        inputStream.close();
        return properties;
    }
}

resources文件夹下面新建meta-inf文件夹,在里面创建spring.factories文件,指定使用我们自定义的解析器

org.springframework.boot.env.PropertySourceLoader=cn.huanzi.qch.config.configserver.MyPropertiesHandler

解决http响应中文乱码问题

  配置文件添加

#解决http响应数据中文乱码问题
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

最终效果

 

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

相关推荐