此次配置,是在 springcloud(4)Feign 消费者 的基础上添加的,请先保证Feign module能正常启动
Feign 项目的创建参考:https://blog.youkuaiyun.com/lettuce_/article/details/102475469
*****************************************************************************************************
1. maven中再添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2. 创建 bootstrap.yml 配置文件,将application.yml中的Eureka 配置信息转到 bootstrap.yml 里
bootstrap.yml
该yml作用介绍 在文末
# eureka 信息
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#springcloudConfig 服务器信息
spring:
cloud:
config:
label: master
profile: dev
discovery:
enabled: true
serviceId: config-server
application.yml
spring:
application:
name: product-consumer-feign
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
mode: HTML5
servlet:
content-type: text/html
server:
port: 8083
3. 使用@Value 获取属性值
例如,在controller中添加
@Value("${version}")
String version;
并将该属性 添加到返回的Model 中
m.addAttribute("version", version);
4. 修改前端html 页面如下
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>products</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
table {
border-collapse:collapse;
width:400px;
margin:20px auto;
}
td,th{
border:1px solid gray;
}
</style>
</head>
<body>
<div class="workingArea">
<table>
<thead>
<tr>
<th>id</th>
<th>产品名称</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<tr th:each="p: ${ps}">
<td th:text="${p.id}"></td>
<td th:text="${p.name}"></td>
<td th:text="${p.price}"></td>
</tr>
</tbody>
<tr>
<td align="center" colspan="3">
<p th:text="${version}" >how2j springcloud version unknown</p>
</td>
</tr>
</table>
</div>
</body>
</html>
5. 启动项目
启动顺序:
- 注册中心
- 配置中心
- 数据module
- 配置中心客户端(Feign项目)
该string 字段就是在 github中配置的字段信息
注意:在线修改github上的配置信息,String字段信息不变(不支持热部署),需重启所有服务后页面才变化
关于bootstrap.yml 作用
bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
bootstrap.yml 先于 application.yml 加载
详情查看: