1.導(dǎo)入依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2.創(chuàng)建配置文件bootstrap.properties票腰,并在添加主要的配置驱闷,這個配置文件的級別是最到的,比application.properties和yml的級別要搞
spring.application.name=mail-coupon? ? #微服務(wù)的名稱
spring.cloud.nacos.config.server-addr=127.0.0.1:8848? ?#nacos的地址
3.打開nacos的主頁玷禽,localhost:8848/nocas,在配置中心中添加一個數(shù)據(jù)集。Data ID在服務(wù)啟動的時候的有顯示?? Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='mail-coupon.properties'}]} 將mail-coupon.properties復(fù)制到Data ID中就好了
4.要想在配置中心中修改配置后,能夠在代碼中實時刷新要在使用配置的類上添加@RefreshScope
@RefreshScope
@RestController
@RequestMapping("mailcoupon/coupon")
public class CouponController {
@Autowired
? ? private CouponServicecouponService;
@Value("${coupon.user.name}")
private StringuserName;
@Value("${coupon.user.age}")
private StringuserAge;
@RequestMapping("/test")
public R test(){
return R.ok().put("name",userName).put("age",userAge);
}
}
5.如果在application.properties中有的配置,配置中心也有波桩,這樣的會優(yōu)先使用配置中心中的