0
点赞
收藏
分享

微信扫一扫

SpringCloud-nacos配置

北冥有一鲲 2023-07-13 阅读 56

新建配置

Namespace:代表不同的环境,如开发、测试、生产。

Group:代表某个项目,如电商项目。

DataId:每个项目下往往有若干个工程(微服务),每个配置集(DataId)是一个工程(微服务)的主配置文件。

SpringCloud-nacos配置_nacos

新建一个配置:nacos-config.yml

SpringCloud-nacos配置_spring_02


搭建spring-cloud-nacos服务

1、pom.xml依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.spring.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-config</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- nacos-config 配置中心依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>
</project>

2、新建启动类

package com.spring.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;

@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceNacosConfigApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(ServiceNacosConfigApplication.class, args);

        log.info("Config application is running ......");

        String userName = applicationContext.getEnvironment().getProperty("name");
        String userAge = applicationContext.getEnvironment().getProperty("age");

        System.out.println("name :" + userName + ",age: " + userAge);
    }
}

3、创建配置文件,bootstrap.yml

server:
  port: 8083

spring:
  application:
    name: nacos-config
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
        service: ${spring.application.name} # 注册到 Nacos 的服务名。默认值为 ${spring.application.name}。
        namespace: a4c50879-af8d-434c-add1-b96d1671933a
      # 配置中心
      config:
        server-addr: 127.0.0.1:8848
        namespace: a4c50879-af8d-434c-add1-b96d1671933a
        file-extension: yml
        group: DEFAULT_GROUP
        # 加载共享配置
        shared-configs:
            - data-id: nacos-config.yml
              refresh: true

4、@RefreshScope

@Value注解可以获取到配置中心的值,但是无法动态感知修改后的值,需要利用@RefreshScope注解。

package com.spring.config.controller;

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("/user")
@RefreshScope //配置此类接口动态刷新
public class UserController {

    @Value("${name}")
    public String userName;

    @GetMapping("test")
    public String test(){
        return userName;
    }
}

举报

相关推荐

0 条评论