美文网首页
05.SpringCloud中用config配置参数

05.SpringCloud中用config配置参数

作者: LewisZhu | 来源:发表于2019-12-25 22:41 被阅读0次

总论:config就是把项目中一些用到的配置参数放到Git上进行访问

01.创建一个spring initialize的工程

创建的时候之间勾上cloud config中的config server,会自动导入config-server的依赖。

这个可以不用放到eureka server中;

02.配置application.yml

//基本配置
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          url:https...   //工程的git地址
          username:xxxx      //登录git的账号
          password:xxxxxxxxx  //登录git的密码
          search-paths:config-file   //config文件的目录
server:
  port: 7900

03.添加注解

@EnableConfigServer

如果现在访问 localhost:7900/项目中properties的前缀名/dev/branch名字(不写 就默认是master)
例如路径地址: .../config-demo/config-file/gateway-zuul-dev.properties

就可以访问到配置文件的内容了

04.客户端(config client)访问这个config的内容,像provider,consumer都可以认为是这个config-server的客户端

书接上节,gateway-zuul中需要访问这个config文件里面的内容;
1>.在gateway-zuul中添加依赖

    <!--Spring Cloud Config 客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

<!--Spring Boot Actuator,感应服务端变化,用于刷新,当config中的内容变更时 就不用重新启动server-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
//加了这个依赖之后   需要在过滤器上面添加注解@RefreshScope

2>.在gateway-zuul中配置bootstrap.yml 这个加载的优先级要高于application.yml

spring:
  application:
    name:gateway-zuul  #这个名字得和Git上面的项目中gateway-zuul-dev.properties的名字对应
# config server 的地址的配置(localhost:7900/项目中properties的前缀名/dev(pro代表生产环境,test代表测试环境)/branch名字(不写 就默认是master) )
cloud:
  config:
    url: http://localhost:7900    #这就是config server的地址
    profile: dev  #指定的环境
    lab:xxx   #这个就是分支名称
management:
  security:
    enabled: false     #SpringBoot 1.5.X 以上默认开通了安全认证,如果不关闭会要求权限,这样就可以刷新了


//刷新通过执行 localhost:7600/refresh 可进行一次刷新

3>.在gateway-zuul中使用config中的参数

通过下面的方式取得参数。

@Value("$(token)") 
private boolean token;

推荐这篇文章写得 比较好:走起

相关文章

网友评论

      本文标题:05.SpringCloud中用config配置参数

      本文链接:https://www.haomeiwen.com/subject/uldhoctx.html