• SpringCloud:配置中心(spring cloud config)


    spring cloud config简介

    为什么要使用配置中心

    简单来说,就是为了方便所有服务的配置统一管理,实时更新。

    在分布式的微服务架构中,服务数量会越来越多,而每个服务实例都会有一个或几个配置文件(yml,properties,json…)。而这些文件,分布在系统的各个角落,管理起来特别麻烦,因此出现了一些可以集中管理配置的组件。这里的spring cloud config就是其中之一。

    为什么要使用spring cloud config

    类似这种分布式配置中心组件,分以下两种角色:
    1. config server
    2. config client

    而spring cloud config的优点则是和Spring的集成更好,接口规范一致,同时已有的Spring项目迁移也比较方便。同时和SpringBoot的标准统一,可以让项目的版本依赖变得容易管理,减少可能出现的依赖冲突。

    spring cloud config使用

    服务端ConfigServer

    新建一个简单的SpringBoot的项目cloud-config。

    添加依赖

    在项目的pom.xml文件中添加如下依赖:

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

    添加配置文件

    在项目的resources目录下,新建application.yml(也可以使用application.properties),在其中添加如下内容:

    spring:
      application:
        name: config-server
      # 配置中心服务配置
      cloud:
        config:
          server:
            git:
              # git的uri
              uri: https://gitee.com/lieh_666/SpringCloudConfig/
              # 搜索的文件夹
              search-paths: clear-cloud
              # 若git为公共项目的话,账号密码可以为空
              username: xxxxxxxx
              password: xxxxxxxx
          #分支
          label: master
    
    server:
      port: 8888
    • spring.cloud.config.server.git.uri:配置git仓库地址
    • spring.cloud.config.server.git.searchPaths:配置仓库路径
    • spring.cloud.config.label:配置仓库的分支
    • spring.cloud.config.server.git.username:访问git仓库的用户名
    • spring.cloud.config.server.git.password:访问git仓库的用户密码

    开启服务配置

    新建项目启动类ConfigServer,通过@EnableConfigServer开启配置中心服务。

    /**
     * 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
     * 在Spring Cloud中,有分布式配置中心组件spring cloud config
     *      它支持配置服务放在配置服务的内存中(即本地)
     *      也支持放在远程Git仓库中
     * 在spring cloud config 组件中,分两个角色
     *      一是config server
     *      二是config client
     * 这里是ConfigServer,
     * 只需要开启@SpringBootApplication和@EnableConfigServer
     *
     */
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServer {
        public static void main(String[] args) {
            SpringApplication.run(ConfigServer.class, args);
        }
    }

    http请求地址和资源文件映射如下(其中application为服务名):
    - /{application}/{profile}[/{label}]
    - /{application}-{profile}.yml
    - /{label}/{application}-{profile}.yml
    - /{application}-{profile}.properties
    - /{label}/{application}-{profile}.properties

    客户端ConfigClient

    新建一个SpringBoot的项目config-client

    添加依赖

    在其pom文件中添加如下依赖:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    添加服务端的配置

    在项目的resources目录下,新建application.yml,在其中添加如下内容:

    server:
      port: 8889
    spring:
      application:
        name: service-hello
      #配置中心的配置信息
      cloud:
        config:
          label: master
          profile: dev
          uri: http://localhost:8888/

    配置启动类,并在Controller中获取Git文件中的属性

    新建启动类HelloApplication,并使用@RestController注册为Controller接口。使用@Value获取属性信息:

    /**
     * 一个简单的服务
     *
     */
    @EnableEurekaClient
    @SpringBootApplication
    @RestController
    public class HelloApplication {
        public static void main(String[] args) {
            SpringApplication.run(HelloApplication.class, args);
        }
    
        @Value("${msg}")
        String msg;
        @RequestMapping(value = "/hello")
        public String hi(){
            return msg;
        }
    }

    因为配置服务名为service-helloprofiledev,因此在git的对应目录(这里为https://gitee.com/lieh_666/SpringCloudConfig/clear-cloud)下,新建一个service-hello-dev的文件,并在文件中添加msg属性如下:

    msg = hello,this is git config

    启动服务端和客户端

    启动ConfigServer
    启动ConfigClient
    访问http://localhost:8889/hello

    运行结果如下:
    运行结果

  • 相关阅读:
    hdu2037 今年暑假不AC[贪心][区间调度问题]
    Features Track[STL map]
    Characters with Hash[签到题]
    hdu2094产生冠军[STL set]
    2018ECNA Difference[时空复杂度]
    hdu1276士兵队列训练问题[简单STL list]
    20190815网络与信息安全领域专项赛线上赛misc WriteUp
    hdu1873 看病要排队[优先队列]
    Pygame 贪吃蛇
    Windows安装Python3 curses模块
  • 原文地址:https://www.cnblogs.com/enhe/p/12141697.html
Copyright © 2020-2023  润新知