刚接触sPRing cloud 的 ;做了一个svn做配置文件仓库demo;
创建一个spring boot工程作为 spring cloud config server ;下面是pom.xml文件;
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.honkib.springcloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>1.0-SNAPSHOT</version> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.0.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--表示为web工程--> <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.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--config server need read svn--> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.8.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>configserver程序结构:
然后创建一个application.java%20类
package%20com.hobkib.cloud.demo;import%20org.springframework.boot.SpringApplication;import%20org.springframework.boot.autoconfigure.EnableAutoConfiguration;import%20org.springframework.boot.autoconfigure.SpringBootApplication;import%20org.springframework.cloud.config.server.EnableConfigServer;import%20org.springframework.context.annotation.Configuration;import%20org.springframework.web.bind.annotation.RestController;/**%20*%20Created%20by%20liaokailin%20on%2016/4/28.%20*/@Configuration@EnableAutoConfiguration@EnableConfigServerpublic%20class%20Application%20{%20%20%20%20public%20static%20void%20main(String[]%20args)%20{%20%20%20%20%20%20%20%20SpringApplication.run(Application.class,%20args);%20%20%20%20}%20%20%20%20}其中@EnableConfigServer%20为作为配置文件服务的注解;appliction.yml配置文件:
server:%20%20port:%208888spring:%20%20cloud:%20%20%20%20config:%20%20%20%20%20%20enabled:%20true%20%20%20%20%20%20server:%20%20%20%20%20%20%20%20svn:%20%20%20%20%20%20%20%20%20%20uri:%20http://192.168.0.66/svn/cloudconfig/%20%20%20%20%20%20%20%20%20%20username:%20chenhua%20%20%20%20%20%20%20%20%20%20passWord:%20123456%20%20%20%20%20%20%20%20#git:%20%20%20%20%20%20%20%20#%20%20uri:%20https://github.com/pcf-guides/configuration-server-config-repo%20%20%20%20%20%20%20%20default-label:%20config%20%20profiles:%20%20%20%20active:%20subversionlogging:%20%20levels:%20%20%20%20org.springframework.boot.env.PropertySourcesLoader:%20TRACE%20%20%20%20org.springframework.cloud.config.server:%20DEBUG创建一个svn仓库作为spring%20cloud%20的配置文件库;
这张图片中配置文件的结构写错了;
配置文件的结构应该是在客户端的 配置文件appliction.preperties文件中的两个字段;(spring.cloud.config.name 和 spring.cloud.config.profile)
要符合{spring.cloud.config.name}-{spring.cloud.config.profile}.preperties 或者后缀为.yml的配置文件;
创建svn库后一定要创建一个文件夹;(必须的),不知道是是不是读取库的问题;默认情况下,他会去这你提供的svn库uri中的trunk文件夹下读取(可以配置这个文件夹的名称。配置成空默认去trunk文件夹查找配置文件),配置文件不能放到svn的根目录下(默认去trunk的文件夹下找配置文件);
客户端工程:
客户端的工程结构:
客户端的配置文件:
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.honkib.springcloud</groupId> <artifactId>spring-cloud-config-client</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>1.0.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>http://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>http://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>appliction.javapackage com.hobkib.cloud.democlient;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by liaokailin on 16/4/28. */@SpringBootApplication@RestControllerpublic class Application { @Value("${MySQLdb.datasource.url:World}") String bar; @RequestMapping("/") String hello() { return "Hello " + bar + "!"; } public static void main(String[] args) { SpringApplication.run(Application.class, args); }}bootstrap.preperties 配置文件:server.port=8081spring.cloud.config.uri=http://localhost:${config.port:8888}spring.cloud.config.name=cloud-configspring.cloud.config.profile=${config.profile:dev}spring.application.name=cloud-simple-service这样程序就已经完成了;
接下来就是运行程序了;
先启动configserver 程序;
然后再启动client工程;
成功了的话;我们应该能在client程序的日志中看到这么一条日志:
2017-02-16 15:32:54.209 INFO 9836 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='http://192.168.0.66/svn/cloudconfig/config/cloud-config-dev.properties']]]
这就表明已经读取到日志了;
接下来只要在@Value("${key}")就可以获取到配置文件的值了;
或者好用env.getPreperty来获取配置文件信息;
注:很重要的一点;不知道的是什么问题;configserver服务端一定要用yml配置文件,使用preperties配置文件启动不成功;不识别;
还有就是注意svn的文件目录结构;配置文件名称结构
新闻热点
疑难解答