谷歌零碎笔记之Nacos

nacos可以用作注册中心,也可以用作配置中心

nacos简单入门

pom文件

	<parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.6.3</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
          <!-- 
		nacos配置文件
		-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 
		没有它注册不上去
		-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

配置文件:bootstrap.yml

问题点:吧bootstrap.yml名字改成application.yml或者application.yaml,都不能获取到配置中的变量,有人说nacos必须要用bootstrap.yml

server:
  port: 9003
spring:
  application:
    name: nacos-demo
  cloud:
    nacos:
      discovery:
        server-addr: **.***.***.***:8848
      config:
        server-addr: **.***.***.***:8848
        namespace: public
        group: DEFAULT_GROUP
        file-extension: yaml
  profiles:
    active: dev

操作页面,地址:http://IP地址:8848/nacos

操作页面
文件:nacos-demo-dev.yaml
组成:{spring.application.name}+{-}+{profiles.active}+{spring.cloud.nacos.config.file-extension}
四部分,必须一致

  • {spring.application.name}:nacos-demo
  • {-}:-
  • {profiles.active}:dev(会根据配置信息,可为空,如果为空前面-符号不要)
  • {spring.cloud.nacos.config.file-extension}:yaml

启动类加注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosApplication.class,args);
    }
}

测试controller,@RefreshScope // 刷新配置,@Value(“${common}”)获取值

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/provider")
@RefreshScope // 刷新配置
public class NacosProviderController {

    @Value("${info}")
    private String info;

    @Value("${common}")
    private String common;

    @GetMapping("/info")
    public String configInfo() {
        return info+"="+common;
    }
}

如果需要共享文件,需要在bootstrap.yml中加共享配置;shared-configs:

注:共享文件配置和对应文件配置冲突,取对应文件

server:
  port: 9003
spring:
  application:
    name: nacos-demo
  cloud:
    nacos:
      discovery:
        server-addr: ip:8848
      config:
        server-addr: ip:8848
        namespace: public
        group: DEFAULT_GROUP
        file-extension: yaml
        #新的共享配置+动态刷新
        shared-configs:
          - data-id: application.yaml
            group: DEFAULT_GROUP
            refresh: true #动态刷新配置开启
  profiles:
    active: dev

测试OK

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值