SpringCloud Config SpringCloud微服务之Config知识总结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringCloud Config SpringCloud微服务之Config知识总结

ProChick   2021-05-19 我要评论
想了解SpringCloud微服务之Config知识总结的相关内容吗,ProChick在本文为您仔细讲解SpringCloud Config的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringCloud微服务,SpringCloud,Config,下面大家一起来学习吧。

一、什么是Spring Cloud Config?

  • Spring Cloud Config 可以为微服务架构中的应用提供集中化的外部配置支持,它分为服务端和客户端两个部分。
  • Spring Cloud Config 服务端被称为分布式配置中心,它是个独立的应用,可以从配置仓库获取配置信息并提供给客户端使用。
  • Spring Cloud Config 客户端可以通过配置中心来获取配置信息,在启动时加载配置。
  • Spring Cloud Config 的配置中心默认采用Git来存储配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客户端来方便地管理和访问配置信息。

二、搭建GIT环境

创建仓库

在这里插入图片描述

创建文件

  • master分支
# config-dev.yml
config:
  info: "config info for dev(master)"
# config-test.yml
config:
  info: "config info for test(master)"
# config-prod.yml
config:
  info: "config info for prod(master)"
  • dev分支
# config-dev.yml
config:
  info: "config info for dev(dev)"
# config-test.yml
config:
  info: "config info for test(dev)"
# config-prod.yml
config:
  info: "config info for prod(dev)"

三、服务端示例

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

添加配置

  • 启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class SpringcloudConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudConfigServerApplication.class, args);
    }
}
  • application.yml配置文件
server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        # 配置存储配置信息的Git仓库
        git:
          uri: https://gitee.com/prochick/spring-cloud-config.git
          # Git用户名
          username: xxx
          # Git密码
          password: xxx
          # 指定是否开启启动时直接从git获取配置
          clone-on-start: true

eureka:
  instance:
    prefer-ip-address: true
    instance-id: config-server-8888
  client:
    fetch-registry: false
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8010/eureka/

访问说明

# 获取配置信息
/{label}/{application}-{profile}
# 获取配置文件信息
/{label}/{application}-{profile}.yml
  • application

代表应用名称,默认为配置文件中的spring.application.name,如果配置了spring.cloud.config.name,则为该名称

  • label

代表分支名称,对应配置文件中的spring.cloud.config.label

  • profile

代表环境名称,对应配置文件中的spring.cloud.config.profile

测试使用

# 访问http://localhost:8888/master/config-dev来获取master分支上dev环境的配置信息
# 访问http://localhost:8888/master/config-dev.yml来获取master分支上dev环境的配置文件信息
# 访问http://localhost:8888/master/config-test.yml来获取master分支上test环境的配置文件信息
# 访问http://localhost:8888/dev/config-dev.yml来获取dev分支上dev环境的配置文件信息

四、客户端示例

添加依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

添加配置

  • 配置文件

bootstrap.yml

server:
  port: 9999
spring:
  application:
    name: config-client

  cloud:
    config:
      # 配置中心地址
      uri: http://localhost:8888
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 配置后缀名称
      profile: dev

eureka:
  instance:
    prefer-ip-address: true
    instance-id: config-client-9999
    client:
      fetch-registry: false
      register-with-eureka: true
      service-url:
        defaultZone: http://localhost:8010/eureka/
 
# 暴露刷新监控 
management:
  endpoints:
    web:
      exposure:
        include: 'refresh'

控制器类

@RestController
@RefreshScope
public class ConfigController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {

        return configInfo;
    }
}

测试使用

# 访问http://localhost:9999/configInfo 可以获取到dev分支下dev环境的配置

五、安全认证示例

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

添加配置

  • 服务端配置
server:
  port: 8888
spring:
  application:
    name: config-server
  # 配置存储配置信息的Git仓库
  cloud:
    config:
      server:
        git:
          # 访问地址
          uri: https://gitee.com/prochick/spring-cloud-config.git
          # Git用户名
          username: xxx
          # Git密码
          password: xxx
          # 指定是否开启启动时直接从git获取配置
          clone-on-start: true
  # 配置用户名和密码
  security: 
    user:
      name: xxx
      password: xxx
  • 客户端配置
server:
  port: 9999
spring:
  application:
    name: config-client

  cloud:
    config:
      # 配置中心地址
      uri: http://localhost:8888
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 配置后缀名称
      profile: dev
      # 配置中心用户名
      username: xxx
      # 配置中心密码
      password: xxx

六、集群搭建示例

添加配置

bootstrap.yml

server:
  port: 9999
spring:
  application:
    name: config-client

  cloud:
    config:
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 配置后缀名称
      profile: dev
      # 集群绑定
      discovery:
        enabled: true
        service-id: config-server

eureka:
  instance:
    prefer-ip-address: true
    instance-id: config-client-9999
    client:
      fetch-registry: true
      register-with-eureka: true
      service-url:
        defaultZone: http://localhost:8010/eureka/

测试访问

# 访问eureka-server

在这里插入图片描述

# 访问http://localhost:9999/configInfo 可以获取到dev分支下dev环境的配置

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们