接着上一篇把把本地仓库yml文件推送到github之后,下面我们就是实战了,各个微服务如何读取到远程仓库的的yml文件配置
一,新建一个Module模块microservicecloud-config-3344 它即为Cloud配置中心模块
二,pom文件添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>microservicecloud</artifactId><groupId>com.atguigu.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>microservicecloud-config-3344</artifactId><dependencies><!-- springCloud Config --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback --><!--<dependency><groupId>org.eclipse.jgit</groupId><artifactId>org.eclipse.jgit</artifactId><version>4.10.0.201712302008-r</version></dependency>--><!-- 图形化监控 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 熔断 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- 热部署插件 --><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies></project>
三,编写启动类,添加@EnableConfigServer注解
package com.atguigu.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp {public static void main(String[] args) {SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);}
}
四,编写application.yml配置文件
server: port: 3344 spring:application:name: microservicecloud-configcloud:config:server:git:uri: git@github.com:470812087/microservicecloud-config.git #GitHub上面的git仓库名字
uri: git@github.com:470812087/microservicecloud-config.git #GitHub上面的git仓库名字从github上复制
启动配置中心微服务 访问:http://config-3344.com:3344/application-dev.yml
http://config-3344.com:3344/application-test.yml
访问不存在的配置(不符合规则):
访问不存在的配置(符合规则):
配置读取规则