一芬萍、 在遠(yuǎn)程倉(cāng)庫(kù)添加配置文件
文件 client01-dev.yml
二衬鱼、改造 client01,添加依賴(lài)
給 client01 做一些配置捻悯,使其可以通過(guò) config server 讀取遠(yuǎn)程配置文件
<!-- config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
三匆赃、修改配置文件
新建一個(gè) bootstrap.yml 文件,添加如下內(nèi)容
spring:
# 為了獲取 Config Server 的配置文件
cloud:
config:
# spring.cloud.config.label 指明遠(yuǎn)程倉(cāng)庫(kù)的分支
label: master
# spring.cloud.config.profile
# dev 開(kāi)發(fā)環(huán)境配置文件
# test 測(cè)試環(huán)境
# pro 正式環(huán)境
profile: dev
# spring.cloud.config.uri 指明配置服務(wù)中心的網(wǎng)址
uri: http://localhost:8181/
我的 application.yml 如下
server:
port: 8763
spring:
application:
name: client01
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# 為了在服務(wù)注冊(cè)中心里顯示實(shí)際的 IP 地址今缚,需添加下面的配置
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
# 為了打開(kāi) feign 的 hystrix 功能
feign:
hystrix:
enabled: true
所以訪(fǎng)問(wèn) Config Server 會(huì)使用的路徑是 http://localhost:8181/client01/dev/master
注意:一定要新建一個(gè) bootstrap.yml 算柳,因?yàn)?bootstrap.yml 優(yōu)先于 application.yml ,在里面配置 Config Server 的路徑姓言,否則會(huì)一直使用默認(rèn)的路徑瞬项,默認(rèn)的路徑端口是8888,啟動(dòng)會(huì)報(bào)錯(cuò)何荚。
四囱淋、在控制器中添加一個(gè)接口,返回 client01-dev.yml 的 myValue
代碼如下
// 獲取 Config Server 的配置文件中的 myValue 的值
// 注意:如果遠(yuǎn)程倉(cāng)庫(kù)中沒(méi)有這個(gè)配置參數(shù)時(shí)餐塘,服務(wù)是啟動(dòng)不了的
@Value("${myValue}")
private String myValue;
/**
* 該接口是為了獲取 config server 的配置文件信息
*/
@RequestMapping(value = "/config",method = RequestMethod.GET)
public String config(){
return myValue;
}
完整代碼如下
import com.dhsg.sc.client01.service.IUseOtherApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private IUseOtherApiService userOtherApiService;
// 該注解可以讀取配置文件中的值賦予下面的變量
@Value("${server.port}")
private String port;
// 獲取 Config Server 的配置文件中的 myValue 的值
// 注意:如果遠(yuǎn)程倉(cāng)庫(kù)中沒(méi)有這個(gè)配置參數(shù)時(shí)妥衣,服務(wù)是啟動(dòng)不了的
@Value("${myValue}")
private String myValue;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(@RequestParam(value = "name", defaultValue = "dhsg") String name) {
return "Hello " + name + " ,I am from port:" + port;
}
/**
* 該接口是通過(guò) feign 組件,訪(fǎng)問(wèn) client02 的接口
*
* @return client02 的端口和服務(wù)名
*/
@RequestMapping(value = "/getclient02name", method = RequestMethod.GET)
public String getclient02name() {
return userOtherApiService.getClient02Name();
}
/**
* 該接口是為了獲取 config server 的配置文件信息
*/
@RequestMapping(value = "/config", method = RequestMethod.GET)
public String config() {
return myValue;
}
}
五、訪(fǎng)問(wèn) http://localhost:8763/config
結(jié)果