github创建仓库
- github 新建仓库 spring-cloud-config
- 仓库内新建文件夹config,文件夹内新建文件 config-dev.yml
- config-dev.yml 文件内容
server: port: 3355
搭建配置中心
- 新建微服务模块 cloud-config-center3344
- pom引入依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
- 修改创建 application.yml 文件
server: port: 3344 spring: application: name: cloud-config-center cloud: config: server: git: #uri: git@github.com:EiletXie/config-repo.git #Github上的git仓库名字 # github仓库的地址,把地址栏复制粘贴即可 uri: https://github.com/OT-mt/spring-cloud-config #搜索目录.这个目录指的是github上的目录 search-paths: /config #因为github为外网访问较慢,所以设置超时时间较长 timeout: 15 # 分支,对应github仓库的分支,可查看本文章第一张图片上的Branch label: master eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka
- 建立启动类
@SpringBootApplication @EnableEurekaClient @EnableConfigServer public class Config_Main3344 { public static void main(String[] args) { SpringApplication.run(Config_Main3344.class,args); } }
- 测试
访问 http://localhost:3344/config-dev.yml,即可查看到访问到github
中的文件。
客户端配置
-
创建项目 cloud-config-client3355
-
pom依赖
注意与服务端的依赖不同<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 注意与服务端的依赖不同 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> ```
-
yml文件
此时 yml 文件名应该设为 bootstrap.yml,因为bootstrap的优先级大于application防止被 配置中心的配置所覆盖server: port: 3355 spring: application: name: cloud-client cloud: config: # 分支与配置中心含义一样 label: master # name与profile在下面讲解 name: config profile: dev #对应配置中心的端口 uri: http://localhost:3344 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka
-
SpringCloud Config HTTP服务具有以下格式的资源:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
这代表可以用什么样的http路径来访问github中的资源。
上述五种格式中:- application 代表 config.name
- label 代表 config.label
- profile 代表 config.profile、
由于本文中采用的是yml,所以这代表着可以用以下三个 路径访问github中的文件
- http://localhost:3344/config/dev/master
- http://localhost:3344/config-dev.yml
- http://localhost:3344/master/config-dev.yml