Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31970
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 149
  • 用 户 组: 普通用户
  • 注册时间: 2019-11-29 16:34
文章分类
文章存档

2020年(14)

我的朋友

分类: 其他平台

2020-02-17 18:58:48

.Spring Cloud Config 分布式配置

a.Config服务器

①新建springboot项目,依赖选择Config Server

②pom文件关键依赖
    了解springcloud架构可以加求求:三五三六二四七二五九



点击(此处)折叠或打开

  1. org.springframework.boot
  2. spring-boot-starter-parent
  3. 2.1.3.RELEASE
  4. org.springframework.cloud
  5. spring-cloud-config-server
  6. ......
  7. ......

③application.yml文件

点击(此处)折叠或打开

  1. spring:
  2. application:
  3. name: config-server
  4. profiles:
  5. #配置文件在本地
  6. active: native
  7. #配置文件的目录
  8. cloud:
  9. config:
  10. server:
  11. native:
  12. search-locations: classpath:/config
  13. server:
  14. port: 8101

④启动类添加注解@EnableConfigServer

点击(此处)折叠或打开

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

⑤在resources下新建config/commom-dev.properties,用于测试

点击(此处)折叠或打开

  1. test.name=vettel
  2. test.password=111111

⑥启动后访问 /common/dev 可查看配置文件信息,访问路径有如下几种

点击(此处)折叠或打开

  1.  /{application}/{profile}[/{label}]
  2.       /{application}-{profile}.yml
  3.       /{label}/{application}-{profile}.yml
  4.       /{application}-{profile}.properties
  5.       /{label}/{application}-{profile}.properties

注:对于resources下的config/commom-dev.properties,{application}为文件名"commom",{profile}为环境名"dev",{label}为路径名"config"。
 
 b.Config客户端
 
  ①新建springboot项目,依赖选择 Config Client 、Web


②pom文件关键依赖

点击(此处)折叠或打开

  1. org.springframework.boot
  2. spring-boot-starter-parent
  3. 2.1.3.RELEASE
  4. org.springframework.boot
  5. spring-boot-starter-web
  6. org.springframework.cloud
  7. spring-cloud-starter-config
  8. ......
  9. ......
③bootstrap.yml文件

点击(此处)折叠或打开

  1. spring:
  2. application:
  3. name: config-client
  4. cloud:
  5. config:
  6. uri:
  7. profile: dev
  8. name: common
  9. server:
  10. port: 8102

注意:此处需要将配置写入bootstrap.yml(会优先于application.yml加载)中,因为config的配置是优先于application.yml加载的,否则会报错。了解springcloud架构可以加求求:三五三六二四七二五九

点击(此处)折叠或打开

  1. @SpringBootApplication
  2. @RestController
  3. public class ConfigClientApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigClientApplication.class, args);
  6. }
  7. @Value("${test.name}")
  8. String name;
  9. @Value("${test.password}")
  10. String password;
  11. @RequestMapping(value="/getConfig")
  12. public String getConfig(){
  13. return "name[" + name + "], password[" + password + "]";
  14. }
  15. }

④具体使用






阅读(1229) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~