当前位置:   article > 正文

九、springcloud配置中心_org.springframework.cloud

org.springframework.cloud

config Server服务端:

新建引入pom

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-config-server</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.mybatis.spring.boot</groupId>
  20. <artifactId>mybatis-spring-boot-starter</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.alibaba</groupId>
  24. <artifactId>druid-spring-boot-starter</artifactId>
  25. <version>1.1.10</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>mysql</groupId>
  29. <artifactId>mysql-connector-java</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-jdbc</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-devtools</artifactId>
  38. <scope>runtime</scope>
  39. <optional>true</optional>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.projectlombok</groupId>
  43. <artifactId>lombok</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-test</artifactId>
  48. <scope>test</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>com.smy.springcloud</groupId>
  52. <artifactId>common-api</artifactId>
  53. <version>1.0-SNAPSHOT</version>
  54. </dependency>
  55. </dependencies>

yml配置:

  1. server:
  2. port: 3344
  3. spring:
  4. application:
  5. name: CLOUD-CONFIG
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://gitee.com/rentings/config-server.git
  11. search-paths:
  12. - config
  13. label: master
  14. datasource:
  15. type: com.alibaba.druid.pool.DruidDataSource
  16. driver-class-name: org.gjt.mm.mysql.Driver
  17. url: jdbc:mysql://127.0.0.1:3306/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
  18. username: root
  19. password: 123456
  20. eureka:
  21. client:
  22. register-with-eureka: true
  23. fetch-registry: true
  24. service-url:
  25. defaultZone: http://peer1:7001/eureka,http://peer2:7002/eureka
  26. instance:
  27. instance-id: payment8001
  28. prefer-ip-address: true
  29. lease-renewal-interval-in-seconds: 1
  30. lease-expiration-duration-in-seconds: 2
  31. mybatis:
  32. mapper-locations: classpath:mapper/*.xml
  33. type-aliases-package: com.smy.entity
  34. logging:
  35. level:
  36. com.smy.dao: debug

主类:

  1. @SpringBootApplication
  2. @EnableConfigServer
  3. public class ConfigMain3344 {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigMain3344.class,args);
  6. }
  7. }

启动访问配置文件:http://127.0.0.1:3344/master/config-dev.yml

config Client客户端

pom配置:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-config</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-devtools</artifactId>
  21. <scope>runtime</scope>
  22. <optional>true</optional>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>com.smy.springcloud</groupId>
  35. <artifactId>common-api</artifactId>
  36. <version>1.0-SNAPSHOT</version>
  37. </dependency>
  38. </dependencies>

bootstrap.yml配置:

  1. server:
  2. port: 3355
  3. spring:
  4. application:
  5. name: CLOUD-CONFIG-CLIENT
  6. cloud:
  7. config:
  8. label: master
  9. name: config
  10. profile: dev
  11. uri: http://127.0.0.1:3344
  12. eureka:
  13. client:
  14. register-with-eureka: true
  15. fetch-registry: true
  16. service-url:
  17. defaultZone: http://peer1:7001/eureka,http://peer2:7002/eureka
  18. instance:
  19. instance-id: configclient3355
  20. prefer-ip-address: true
  21. lease-renewal-interval-in-seconds: 1
  22. lease-expiration-duration-in-seconds: 2

主类:

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class ConfigClientMain3355 {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigClientMain3355.class,args);
  6. }
  7. }

controller:此处@Value("${info1.zs}")对应配置中加载的服务端的配置文件内容必须一致否则在启动时候会报错。

  1. @RequestMapping("configclient")
  2. @RestController
  3. public class ConfigController {
  4. @Value("${info1.zs}")
  5. private String str;
  6. @GetMapping("/getConfigInfo")
  7. public String getConfigInfo(){
  8. return str;
  9. }
  10. }

访问:http://127.0.0.1:3355/configclient/getConfigInfo  得到8001

注意:bootstrap会优先application文件加载一般对外的配置写在bootstrap中对内的写在application中,如果把服务端的config改成applicatio的内容则会自动读取并加载到项目中。

手动刷新:

bootstrap.yml配置:

include配置成*,要加双引号哦,然后启动先获取一下配置http://localhost:3355/configclient/getConfigInfo嗯没错,然后修改gitee里面的配置,然后刷新3355地址http://localhost:3355/actuator/refresh用postman发post请求,我开始用cmd发-X POST那种的死活无法成功一致提示404,但是访问3355的controller是可以的郁闷,刷新完之后再请求http://localhost:3355/configclient/getConfigInfo发现配置已经修改了。
  1. server:
  2. port: 3355
  3. spring:
  4. application:
  5. name: CLOUD-CONFIG-CLIENT
  6. cloud:
  7. config:
  8. label: master
  9. name: config
  10. profile: dev
  11. uri: http://127.0.0.1:3344
  12. eureka:
  13. client:
  14. register-with-eureka: true
  15. fetch-registry: true
  16. service-url:
  17. defaultZone: http://peer1:7001/eureka,http://peer2:7002/eureka
  18. instance:
  19. instance-id: configclient3355
  20. prefer-ip-address: true
  21. lease-renewal-interval-in-seconds: 1
  22. lease-expiration-duration-in-seconds: 2
  23. #暴露监控端口
  24. management:
  25. endpoints:
  26. web:
  27. exposure:
  28. include: "*"
@RefreshScope开启刷新。
  1. @RequestMapping("configclient")
  2. @RestController
  3. @RefreshScope
  4. public class ConfigController {
  5. @Value("${config.info}")
  6. private String str;
  7. @GetMapping("/getConfigInfo")
  8. public String getConfigInfo(){
  9. return str;
  10. }
  11. }

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/479528
推荐阅读
相关标签
  

闽ICP备14008679号