springcloud集成nacos

1、预备环境准备

nacos是依赖java环境运行的,先安装jdk1.8环境
jdk1.8下载地址:链接

2、nacos下载

本次使用的是windows版本的nacos
nacos下载地址:链接
在这里插入图片描述
nacos官方地址:链接

3、启动nacos

启动命令(standalone代表着单机模式运行,非集群模式):
在nacos解压目录的bin文件下启动cmd窗口

startup.cmd -m standalone

访问地址:http://本地ip:8848/nacos/index.html
账号:nacos
密码:nacos

4、创建项目

依赖包

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- lombok插件 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- nacos服务注册和发现插件 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!-- nacos服务配置插件 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

注意点:nacos的版本要跟spring-boot版本对应
版本 2.1.x.RELEASE 对应的是 Spring Boot 2.1.x 版本;
版本 2.0.x.RELEASE 对应的是 Spring Boot 2.0.x 版本;
版本 1.5.x.RELEASE 对应的是 Spring Boot 1.5.x 版本。
更多版本参考:链接

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>

5、nacos配置

spring:
  application:
    name: test
  profiles:
    active: dev
  cloud:
    nacos:
      config:
        server-addr: @nacosUrl@
        file-extension: yaml
        namespace: @namespace@

yaml文件解析:

nacos上的命名空间对应{spring.cloud.nacos.config.namespace}
在这里插入图片描述
nacos命名空间下的配置文件的data_id对应
{spring.application.name}-{spring.profiles.active}.{spring.cloud.nacos.config.file-extension}
在这里插入图片描述
双@的使用是在pom.xml配置的
在这里插入图片描述
还要加配置,才能使双@生效
在这里插入图片描述
实体配置获取nacos配置文件的信息(实时刷新)

@Configuration
@Data
@RefreshScope
@ConditionalOnExpression("!'${hello}'.isEmpty()")
@ConfigurationProperties(prefix = "hello")
public class Hello {

    private String world;
}

在这里插入图片描述
@ConfigurationProperties使用该注解要先加下面的依赖

 		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

5、测试

@RestController
@RefreshScope
public class HelloController {

    @Value("${hello.world}")
    private String msg;

    @Autowired
    private Hello hello;

    @GetMapping("hello")
    public String hello(){

        System.out.println(msg);
        System.out.println(hello.getWorld());
        return hello.getWorld();
    }
}

两种方式获取nacos配置文件内容,一个是注解@value,要有@RefreshScope注解,一个是配置实体类
结果展示:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值