SpringCloud微服务之Config知识点有哪些

发布时间:2021-05-20 09:38:44 作者:小新
来源:亿速云 阅读:210

这篇文章主要介绍了SpringCloud微服务之Config知识点有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

一、什么是Spring Cloud Config?

二、搭建GIT环境

创建仓库

SpringCloud微服务之Config知识点有哪些

创建文件

# 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)"
# 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);
    }
}
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

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

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

代表环境名称,对应配置文件中的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

SpringCloud微服务之Config知识点有哪些

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

感谢你能够认真阅读完这篇文章,希望小编分享的“SpringCloud微服务之Config知识点有哪些”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. SpringCloud之配置中心Config
  2. 微服务 springcloud

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springcloud config

上一篇:SpringCloud微服务基础知识点

下一篇:如何使用Python元类特性实现ORM框架

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》