27 微服务配置拉取

1)引入nacos-config依赖

首先,在user-service服务中,引入nacos-config的客户端依赖:

复制代码
<!--nacos配置管理依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

2)添加bootstrap.yaml

然后,在user-service中添加一个bootstrap.yaml文件,内容如下:

复制代码
spring:
  application:
    name: userservice # 服务名称
  profiles:
    active: dev #开发环境,这里是dev 
  cloud:
    nacos:
      server-addr: localhost:8848 # Nacos地址
      config:
        file-extension: yaml # 文件后缀名

3)读取nacos配置

在user-service中的UserController中添加业务逻辑,读取pattern.dateformat配置:

复制代码
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${pattern.dateformat}")
    private String dateformat;
    
    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
    }
    // ...略
}

注意不要听弹幕的用@NacosValue,就是用@Value,不是lombok包的,是org.springframework.beans.factory.annotation.Value包

bootstrap的依赖也不需要导入

复制代码
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.0</version>
        </dependency>
相关推荐
s_w.h1 天前
【 linux 】进程的调度算法
linux·运维·服务器
Traving Yu1 天前
多租户SaaS架构
架构
艾莉丝努力练剑1 天前
【Linux网络】Linux 网络编程:传输层UDP
linux·运维·服务器·网络·计算机网络·udp
陈eaten1 天前
centos 7等保整改学习
linux·运维·服务器·网络安全·centos·等保
牢七1 天前
契约锁分析
linux·运维·服务器
承渊政道1 天前
Linux系统学习【进程概念从入门到深入理解】
linux·服务器·笔记·学习·ubuntu·系统架构·bash
无忧.芙桃1 天前
进程间通信的基本概念(上)
linux·运维·服务器
草莓熊Lotso1 天前
【Linux系统加餐】从原理到封装:基于建造者模式实现System V信号量工业级C++封装
android·linux·运维·服务器·网络·c++·建造者模式
广州灵眸科技有限公司1 天前
瑞芯微(EASY EAI)RV1126B 核心板供电电路
linux·运维·服务器·单片机·嵌入式硬件·电脑
keyipatience1 天前
18.Linux进程退出和进程等待机制详解
linux·运维·服务器