springcloud(6)springcloud-Config 配置中心 客户端

此次配置,是在 springcloud(4)Feign 消费者   的基础上添加的,请先保证Feign module能正常启动

Feign 项目的创建参考:https://blog.csdn.net/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. 启动项目

启动顺序:

  1. 注册中心
  2. 配置中心
  3. 数据module
  4. 配置中心客户端(Feign项目)

该string 字段就是在 github中配置的字段信息

注意:在线修改github上的配置信息,String字段信息不变(不支持热部署),需重启所有服务后页面才变化

 

关于bootstrap.yml 作用

bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等

application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。

bootstrap.yml 先于 application.yml 加载

详情查看:

https://www.cnblogs.com/BlogNetSpace/p/8469033.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值