剛接觸sPRing cloud 的 ;做了一個(gè)svn做配置文件倉(cāng)庫(kù)demo;
創(chuàng)建一個(gè)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> <!--暴露各種指標(biāo)--> <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程序結(jié)構(gòu):
然后創(chuàng)建一個(gè)application.java%20類(lèi)
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為作為配置文件服務(wù)的注解;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創(chuàng)建一個(gè)svn倉(cāng)庫(kù)作為spring%20cloud%20的配置文件庫(kù);
這張圖片中配置文件的結(jié)構(gòu)寫(xiě)錯(cuò)了;
配置文件的結(jié)構(gòu)應(yīng)該是在客戶端的 配置文件appliction.preperties文件中的兩個(gè)字段;(spring.cloud.config.name 和 spring.cloud.config.profile)
要符合{spring.cloud.config.name}-{spring.cloud.config.profile}.preperties 或者后綴為.yml的配置文件;
創(chuàng)建svn庫(kù)后一定要?jiǎng)?chuàng)建一個(gè)文件夾;(必須的),不知道是是不是讀取庫(kù)的問(wèn)題;默認(rèn)情況下,他會(huì)去這你提供的svn庫(kù)uri中的trunk文件夾下讀?。梢耘渲眠@個(gè)文件夾的名稱(chēng)。配置成空默認(rèn)去trunk文件夾查找配置文件),配置文件不能放到svn的根目錄下(默認(rèn)去trunk的文件夾下找配置文件);
客戶端工程:
客戶端的工程結(jié)構(gòu):
客戶端的配置文件:
<?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這樣程序就已經(jīng)完成了;
接下來(lái)就是運(yùn)行程序了;
先啟動(dòng)configserver 程序;
然后再啟動(dòng)client工程;
成功了的話;我們應(yīng)該能在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']]]
這就表明已經(jīng)讀取到日志了;
接下來(lái)只要在@Value("${key}")就可以獲取到配置文件的值了;
或者好用env.getPreperty來(lái)獲取配置文件信息;
注:很重要的一點(diǎn);不知道的是什么問(wèn)題;configserver服務(wù)端一定要用yml配置文件,使用preperties配置文件啟動(dòng)不成功;不識(shí)別;
還有就是注意svn的文件目錄結(jié)構(gòu);配置文件名稱(chēng)結(jié)構(gòu)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注