參考:sorry,全找不到了,當時沒記錄,最後後知後覺以爲應該記錄,因此後面的都有在asfood父項目中的doc文件夾下記錄,望見諒。git
1.1. pom.xmlgithub
<!-- 父項目以來 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>
@EnableConfigServer @SpringBootApplication public class TomatoApplication { public static void main(String[] args) { SpringApplication.run(TomatoApplication.class, args); } }
1.3 配置web
server: port: 55590 spring: application: name: asfood-tomato profiles: active: dev cloud: config: server: git: # 配置git倉庫的地址 #訪問地址: http://localhost:55590/{filename}/{env}/{branch} uri: https://github.com/molyjao/mlims # git倉庫地址下的相對地址,能夠配置多個,用,分割。 #search-paths: asfoodconfig # git倉庫的帳號 #username: #jiu_shaan@163.com # git倉庫的密碼 #password: #配置以後訪問git配置須要輸入用戶名密碼 security: user: name: tomato password: tomato
啓動工程,若是能夠使用 http://localhost:55590/{文件名不帶後面環境}/{環境}/{git分支}訪問 ,並能夠展現裏面內容便可。spring
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>
2.2 啓動類bootstrap
@SpringBootApplication public class KetchupApplication { public static void main(String[] args) { SpringApplication.run(KetchupApplication.class, args); } }
控制層:安全
@RestController @RefreshScope //刷新配置使用的註解, public class KetchupController { //qqname爲配置文件的內容的一個key,':'後面是默認值 @Value("${qqname:defaultqqname}") private String str; @RequestMapping("/ketchup") String hello() { return "Hello " + str + "!"; } }
bootstrap.ymlapp
spring: cloud: config: name: asfood profile: dev label: master uri: http://localhost:55590/ #discovery: enabled: true # 默認false,設爲true表示使用註冊中心中的configserver配置而不本身配置configserver的uri serviceId: asfood-tomato # 指定config server在服務發現中的serviceId,默認爲:configserver #因爲服務端配置了訪問須要用戶名和密碼,因此此處也須要配置 username: tomato password: tomato
application.ymlmaven
server: port: 55591 spring: application: name: asfood-ketchup profiles: active: dev #日誌 logging: file: ./logs/ketchup.log management: security: enabled: false #actuator是否須要安全保證 默認爲true 不加會報錯
如今啓動服務端,後啓動客戶端,能夠訪問就正常了,spring-boot
若是客戶端啓動報錯:找不到所配置的讀取的文件中的key, xxx placeholder ${xxx} 這個錯誤就是沒有找到配置文件(保證不會手誤,key寫的不同),若是此時你的服務端的頁面訪問配置文件,不能訪問到配置文件中的內容,這個須要再次百度,若是是服務端能夠訪問到配置文件中的內容,這個時候須要檢查客戶端的服務端地址等的配置,檢查服務端和客戶端啓動類的註釋,一個是server一個是client還有問題百度吧,我也初學。。。還有個好網站,stackoverflow。網站
自動刷新配置文件訪問: 客戶端ip:port/refresh
有須要能夠參考這裏(在asfood-ketchup-config-client和asfood-tomato-config-server中): https://github.com/molyjao/mlims.git