Consul3-使用consul作为配置中心


           在前面的文章中学习了consul在windows下的安装配置,然后consul作为spring boot的服务发现和注册中心,详细的参考:

https://blog.csdn.net/j903829182/article/details/80960802

https://blog.csdn.net/j903829182/article/details/80960917

            在这里将学习consul作为springboot的配置中心,有spring cloud config的功能。这里还是以前面consul文章里面的代码为基础进行学习,不在进行重复的代码。

       consul作为配置中心,需要引入配置的jar包,pom.xml的内如修改如下:


   
   
  
  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <groupId>com.jack </groupId>
  6. <artifactId>consul_study1 </artifactId>
  7. <version>0.0.1-SNAPSHOT </version>
  8. <packaging>jar </packaging>
  9. <name>consul_study1 </name>
  10. <description>Demo project for Spring Boot </description>
  11. <!--<parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/> <!– lookup parent from repository –>
  16. </parent>-->
  17. <parent>
  18. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-parent -->
  19. <groupId>org.springframework.cloud </groupId>
  20. <artifactId>spring-cloud-starter-parent </artifactId>
  21. <version>Finchley.RELEASE </version>
  22. <relativePath/>
  23. </parent>
  24. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
  25. <properties>
  26. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  27. <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
  28. <java.version>1.8 </java.version>
  29. </properties>
  30. <dependencies>
  31. <dependency>
  32. <groupId>org.springframework.boot </groupId>
  33. <artifactId>spring-boot-starter-web </artifactId>
  34. </dependency>
  35. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
  36. <dependency>
  37. <groupId>org.springframework.cloud </groupId>
  38. <artifactId>spring-cloud-starter-consul-discovery </artifactId>
  39. <!--<version>2.0.0.RELEASE</version>-->
  40. </dependency>
  41. <!--feign依赖 配置-->
  42. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
  43. <dependency>
  44. <groupId>org.springframework.cloud </groupId>
  45. <artifactId>spring-cloud-starter-feign </artifactId>
  46. <version>1.4.5.RELEASE </version>
  47. </dependency>
  48. <dependency>
  49. <groupId>com.jack </groupId>
  50. <artifactId>consul-api </artifactId>
  51. <version>1.0.0 </version>
  52. </dependency>
  53. <!--consul中健康检查需要用到actuator,不添加会check failing-->
  54. <!--<dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-actuator</artifactId>
  57. </dependency>-->
  58. <dependency>
  59. <groupId>org.springframework.cloud </groupId>
  60. <artifactId>spring-cloud-starter-consul-config </artifactId>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework.boot </groupId>
  64. <artifactId>spring-boot-starter-test </artifactId>
  65. <scope>test </scope>
  66. </dependency>
  67. </dependencies>
  68. <build>
  69. <plugins>
  70. <plugin>
  71. <groupId>org.springframework.boot </groupId>
  72. <artifactId>spring-boot-maven-plugin </artifactId>
  73. </plugin>
  74. </plugins>
  75. </build>
  76. </project>


  上面主要是引入了:


   
   
  
  
  1. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
  2. <dependency>
  3. <groupId>org.springframework.cloud </groupId>
  4. <artifactId>spring-cloud-starter-consul-discovery </artifactId>
  5. <!--<version>2.0.0.RELEASE</version>-->
  6. </dependency>



1,修改配置文件  

 1)添加  bootstrap.yml配置文件


   
   
  
  
  1. spring:
  2. cloud:
  3. consul:
  4. host: localhost
  5. #host: 00.0.100.200
  6. port: 8500
  7. #enabled将此值设置为“false”禁用Consul配置
  8. config:
  9. enabled: true #默认是true --
  10. format: YAML # 表示consul上面文件的格式 有四种 YAML PROPERTIES KEY-VALUE FILES
  11. #data-key: configuration #表示consul上面的KEY值(或者说文件的名字) 默认是data
  12. data- key: data #表示consul上面的KEY值(或者说文件的名字) 默认是data
  13. #prefix设置配置值的基本文件夹
  14. #defaultContext设置所有应用程序使用的文件夹名称
  15. #profileSeparator设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值

2)添加application-dev.yml配置文件,配置如下:


   
   
  
  
  1. spring:
  2. cloud:
  3. consul:
  4. host: localhost
  5. port: 8500
  6. discovery:
  7. #healthCheckPath: ${management.contextPath}/health
  8. healthCheckPath: /health
  9. healthCheckInterval: 15s
  10. instance- id: consul1
  11. enabled: true
  12. enabled: true
  13. application:
  14. name: consul1
  15. server:
  16. port: 8081

3)修改application.yml配置文件


   
   
  
  
  1. spring:
  2. profiles:
  3. active: dev

   以上就是一些配置文件的信息了,都是在resource目录下。


2,java代码

1)添加一个配置类


   
   
  
  
  1. package com.jack.consul_study1.config;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. /**
  4. * create by jack 2018/7/15
  5. */
  6. @ConfigurationProperties(prefix = "student")
  7. public class StudentConfig {
  8. private String name;
  9. private int age;
  10. private String sex;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. public String getSex() {
  24. return sex;
  25. }
  26. public void setSex(String sex) {
  27. this.sex = sex;
  28. }
  29. @Override
  30. public String toString() {
  31. return "StudentConfig{" +
  32. "name='" + name + '\'' +
  33. ", age=" + age +
  34. ", sex='" + sex + '\'' +
  35. '}';
  36. }
  37. }

2)修改测试的控制器,添加测试代码


   
   
  
  
  1. package com .jack .consul_study1 .controller;
  2. import com .jack .consul_study1 .api .Chinese;
  3. import com .jack .consul_study1 .config .StudentConfig;
  4. import org .springframework .beans .factory .annotation .Autowired;
  5. import org .springframework .beans .factory .annotation .Value;
  6. import org .springframework .web .bind .annotation .RequestMapping;
  7. import org .springframework .web .bind .annotation .RestController;
  8. /**
  9. * create by jack 2018/7/8
  10. */
  11. @ RestController
  12. @RequestMapping( "/test")
  13. public class TestController {
  14. @ Autowired
  15. private Chinese chinese;
  16. @ Value("${ myName}")
  17. private String myName;
  18. @ Autowired
  19. private StudentConfig studentConfig;
  20. @ RequestMapping("/ hello")
  21. public String testHello(String name){
  22. System .out .println(" my name is : "+ myName);
  23. return chinese .sayHello( name);
  24. }
  25. @ RequestMapping("/ myname")
  26. public String testHello(){
  27. System .out .println(" my name is : "+ myName);
  28. return myName;
  29. }
  30. @ RequestMapping("/ config")
  31. public String testConfig(){
  32. System .out .println( studentConfig .toString());
  33. return studentConfig .toString();
  34. }
  35. }

3)主类添加注解@EnableConfigurationProperties


   
   
  
  
  1. package com.jack.consul_study1;
  2. import com.jack.consul_study1.config.StudentConfig;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  6. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  7. import org.springframework.cloud.openfeign.EnableFeignClients;
  8. @ SpringBootApplication
  9. @ EnableDiscoveryClient
  10. @ EnableFeignClients
  11. @ EnableConfigurationProperties({ StudentConfig. class})
  12. public class ConsulStudy1Application {
  13. public static void main( String[] args) {
  14. SpringApplication.run( ConsulStudy1Application. class, args);
  15. }
  16. }

   注意:属性配置类的class需要添加到springboot的属性配置注解里面,eg:

@EnableConfigurationProperties({StudentConfig.class})
  
  
 
 
      不添加的话,不能通过@Autowired注解,注入属性配置类,那么就需要在属性配置类上使用spring的bean注解,标记时一个bean

     到这里,代码已经完成了,启动consul服务器,下面在consul里面进行配置了


3,consul配置

      consul作为配置中心的参考文档:https://springcloud.cc/spring-cloud-dalston.html#spring-cloud-consul-config

 1)创建配置,如下图

      

         输入key和value

key为:config/consul1,dev/data

value:

myName: jack
student: 
  name: jack
  age: 18
  sex: 男


注意value用的是yml格式的配置,冒号后面有一个空格


4,运行程序测试

1)测试通过@Value注入


   
   
  
  
  1. @Value("${myName}")
  2. private String myName;
  测试url:http://localhost:8081/test/myname

结果如下:






2)测试通过@ConfigurationProperties进行属性配置

测试url:http://localhost:8081/test/config





    总结:

       到这里consul的简单使用就完成了,consul有两个功能,一个是consul作为注册中心,另一个是consul作为配置中心。在本文中consul作为配置中心,有一个点需要注意,通过@Value注入的属性,修改consul的配置后,属性不能立即生效,需要服务重启。而通过@ConfigurationProperties注入的属性,修改consul的配置后,属性会立即生效,所以建议如果需要动态生效的配置,最好使使用@ConfigurationProperties进行属性的注入。

     

     源代码地址:源码url

欢迎加群:331227121,一起学习交流


 

原文地址:https://blog.csdn.net/j903829182/article/details/81050507


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM