Spring Cloud学习笔记(六)——Spring Cloud Config统一配置管理

简介

Spring Cloud Config可以对微服务配置进行统一的外部管理,并且默认采用Git来管理配置信息。相对于传统的每个微服务对应一份自个儿的配置文件来说,通过Spring Cloud Config统一管理所有微服务配置具有如下优点:1.集中管理微服务配置,当微服务数量众多的时候,使用这种方式会更为方便;2.通过Git管理微服务配置,方便追踪配置修改记录;3.可以在应用运行期间修改配置,微服务能够自动更新配置。

搭建Config-Server

在搭建Config-Server之前,我们需要创建好一个存储配置文件的Git仓库。
在这里插入图片描述
四个配置文件中分别都包含了一个message属性。

创建好配置文件仓库后,我们新建一个Spring Boot项目,然后引入如下依赖:

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>

在入口类中加入@EnableConfigServer注解,开启Spring Cloud Config服务端功能。
接着配置application.yml:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/haliaddel/spring-cloud-config.git
          username: xxx
          password: xxx
server:
  port: 8092

配置具体含义如下:

  • spring.cloud.config.server.git.uri:配置了Git仓库的地址,这里用的是码云,当然你也可以使用别的Git代码托管平台;
  • spring.cloud.config.server.git.username:仓库用户名,即Git托管平台的用户名;
  • spring.cloud.config.server.git.password:仓库密码,即Git托管平台的密码。

启动应用,我们便可以使用下面这些格式来访问配置信息了:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

搭建Config-Client

新建SpringBoot项目,pom文件引入依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

编写一个Controller,用于测试获取Config-Server中配置文件的message属性值:

@RestController
public class TestController {

    @Value("${message}")
    private String message;

    @GetMapping("message")
    public String getMessage() {
        return this.message;
    }
}

要想让上面的代码顺利的获取到message属性值,我们还需要在配置文件中进行一些额外配置:

spring:
  application:
    name: qiang
  cloud:
    config:
      profile: dev
      label: master
      #整合Spring Security时,需要提供身份信息
      username: qiang
      password: 123456
      #整合服务发现
      discovery:
        enabled: true
        service-id: config-server
#在Greenwich版本中,需要如下配置暴露/refresh端口,否则无效
management:
  endpoint:
    refresh:
      enabled: true
  endpoints:
    web:
      exposure:
        include: refresh, health
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8085/eureka, http://localhost:8087/eureka

值得注意的是,这里配置文件并不是叫application.yml,而是必须命名为bootstrap.yml。

上面配置含义如下:

spring.application.name: 对应配置文件规则中的{application};

spring.cloud.config.profile: 对应配置文件规则中的{profile}部分;

spring.cloud.config.label: 对应配置文件规则中的{label}部分;

spring.cloud.config.uri: 对应Config-Server的地址。未引入eureka时,直接指定config服务的地址

Config-Server额外配置

  1. 占位符的使用
    在Config-Server中,除了固定配置一个Git仓库地址外,我们也可以使用占位符来灵活的指定Git仓库地址。
    将上面Config-Server的Git仓库地址改为:

    spring:
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/mrbird/{application}
    

    这里使用占位符{application}来代替上面的spring-cloud-config,通过这种配置,我们可以让不同的Config-Client去不同的Git仓库获取配置。比如,当Config-Client的项目名称为febs的时候,对应Git仓库地址为:https://gitee.com/mrbird/febs,当名称为test时,对应Git仓库地址为:https://gitee.com/mrbird/test。这样我们就可以为不同的项目配置不同的Git仓库。

  2. 子目录支持
    除了使用占位符为每个项目创建单独的Git仓库来存储配置信息外,我们也可以只创建一个Git仓库来存储配置,只不过是将不同的项目配置放置到不同的目录下,只需要像下面这样配置即可:

    spring:
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/mrbird/spring-cloud-config/
              username: xxxx
              password: xxxx
              search-paths: '{application}'
    

    这里search-paths的占位符必须加上单引号’’,否则没办法正确读取配置

  3. clone-on-start
    默认情况下Config-Server在启动的时候并不会马上就去Git参考clone配置文件,只有当Config-Clinet从Config-Server获取相关配置信息的时候,其才会去进行clone操作。我们可以将clone-on-start属性设置为true,其Config-Server在启动的时候就进行clone操作:

    spring
      cloud:
        config:
          server:
            git:
              clone-on-start: true
    

    这样做的好处在于,当Git连接信息有误时,可以马上发现。

  4. 整合Spring Security
    Config-Server中包含了Git连接信息,为了使其更加安全,我们可以通过Spring Security来做用户名密码认证。
    在Config-Server中加入Spring Security依赖.
    然后在Config-Server的配置文件application.yml中加入用户名和密码:

    security:
      user:
        name: qiang
        password: 123456
    

    与此同时,我们也需要在Config-Client中配置Config-Server的用户名和密码,否则在获取配置的时候将报401错误:

    spring:
      cloud:
        config:
          username: qiang
          password: 123456
    
  5. 加密
    参考链接

Config-Client额外配置

  1. 刷新配置
    在Config-Server和Config-Client都启动后,如果这时候Git仓库存储的配置信息改变了,在不重启Config-Client的情况下,配置信息是不会跟着更新的。那么如何在Git仓库存储的配置得到改变的时候也刷新Config-Client中获取到的配置值呢?很简单,我们只需要对Config-Client进行简单的改造:

    在Config-Client中添加spring-boot-starter-actuator依赖:

    		<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    

    该依赖包含了/refresh端点,可以用来刷新配置。

    然后在获取配置的Controller上加入@RefreshScope注解:

    @RestController
    @RefreshScope
    public class TestController {
    
        @Value("${message}")
        private String message;
    
        @GetMapping("message")
        public String getMessage() {
            return this.message;
        }
    }
    

    值得注意的是,我们需要在Config-Client的配置文件中加入如下配置来关闭认证,否则我们无权访问/refresh端点。
    SpringCloud暴露了一个接口 /refresh 来给我们去刷新配置,但是SpringCloud 2.0.0以后,有了改变.

    1. 我们需要在bootstrap.yml里面加上需要暴露出来的地址

      	management:
      	  endpoints:
      	    web:
      	      exposure:
      	        include: refresh,health
      
    2. 现在的地址也不是/refresh了,而是/actuator/refresh

集群配置

如果Config-Server只是单机部署的话,当其遇到问题宕机后,所有从Config-Server获取配置信息的微服务都无法正常工作了,这是灾难性的。所以我们很有必要对Config-Server进行集群处理。

集群的方式有很多,这里介绍通过将Config-Server注册到Eureka的方式来搭建集群。

我们使用在Spring Cloud Eureka服务治理一节中的Eureka-Service来构建Eureka-Server服务注册中心,使用以下命令启动Eureka-Server集群:

java -jar Eureka-Service-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar Eureka-Service-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

Eureka集群启动成功。 接下来开始对Config-Server进行改造,将其注册到Eureka服务注册中心。

引入eureka依赖,在入口开启服务发现@EnableDiscoveryClient,在配置中指定eureka的注册地址。改造好后,启动Config-Server。

接着开始改造Config-Client,在其pom中同样引入Eureka依赖,然后在bootstrap.yml中指定获取Config-Server服务的地址:

spring:
  application:
    name: qiang
  cloud:
    config:
      profile: dev
      label: master
      username: qiang
      password: 123456
      discovery:
        enabled: true
        service-id: config-server

其中eureka.client.service-url.defaultZone指定了Eureka服务注册中心的地址;spring.cloud.config.discovery.enabled指定为true开启获取服务的功能,spring.cloud.config.discovery.service-id表明需要获取服务的名称为config-server。

最后在Config-Client的启动类中引入@EnableDiscoveryClient注解,启动应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本火锅店点餐系统采用Java语言和Vue技术,框架采用SSM,搭配Mysql数据库,运行在Idea里,采用小程序模式。本火锅店点餐系统提供管理员、用户两种角色的服务。总的功能包括菜品的查询、菜品的购买、餐桌预定和订单管理。本系统可以帮助管理员更新菜品信息和管理订单信息,帮助用户实现在线的点餐方式,并可以实现餐桌预定。本系统采用成熟技术开发可以完成点餐管理的相关工作。 本系统的功能围绕用户、管理员两种权限设计。根据不同权限的不同需求设计出更符合用户要求的功能。本系统中管理员主要负责审核管理用户,发布分享新的菜品,审核用户的订餐信息和餐桌预定信息等,用户可以对需要的菜品进行购买、预定餐桌等。用户可以管理个人资料、查询菜品、在线点餐和预定餐桌、管理订单等,用户的个人资料是由管理员添加用户资料时产生,用户的订单内容由用户在购买菜品时产生,用户预定信息由用户在预定餐桌操作时产生。 本系统的功能设计为管理员、用户两部分。管理员为菜品管理、菜品分类管理、用户管理、订单管理等,用户的功能为查询菜品,在线点餐、预定餐桌、管理个人信息等。 管理员负责用户信息的删除和管理,用户的姓名和手机号都可以由管理员在此功能里看到。管理员可以对菜品的信息进行管理、审核。本功能可以实现菜品的定时更新和审核管理。本功能包括查询餐桌,也可以发布新的餐桌信息。管理员可以查询已预定的餐桌,并进行审核。管理员可以管理公告和系统的轮播图,可以安排活动。管理员可以对个人的资料进行修改和管理,管理员还可以在本功能里修改密码。管理员可以查询用户的订单,并完成菜品的安排。 当用户登录进系统后可以修改自己的资料,可以使自己信息的保持正确性。还可以修改密码。用户可以浏览所有的菜品,可以查看详细的菜品内容,也可以进行菜品的点餐。在本功能里用户可以进行点餐。用户可以浏览没有预定出去的餐桌,选择合适的餐桌可以进行预定。用户可以管理购物车里的菜品。用户可以管理自己的订单,在订单管理界面里也可以进行查询操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值