Springboot nocos

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Springboot nocos

CS打赢你   2022-09-28 我要评论

前言

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施

1,  创建工程

先创建maven工程,父工程pom如下:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>configDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>
    <dependencies>
        <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>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2,启动nacos-server服务

访问的url是:http://localhost:8848/nacos/ 默认端口是8848,账号密码是:nacos/nocos

3,编写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.RestController;
/**
 * @author yhq
 * @version 1.0
 * @date 2022/7/15 19:07
 */
@RestController
@RefreshScope  //@RefreshScope:需要配置这个才能动态更新配置。
public class TestController {
    @Value("${name}")
    private String name;
    @GetMapping("/getName")
    public String test(){
        return name;
    }
}

4,添加配置文件boostrap.yaml

springboot默认加载配置文件顺序:

bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml 其中bootstrap.properties 配置为最高优先级先加载的会被后加载的覆盖掉,所以.properties和.yml同时存在时,.properties会失效,.yml会起作用。”

#端口
server:
  port: 8888
#配置项目名称
spring:
  application:
    #configdemo默认是nacos的DateId名称
    name: configdemo
  #指定test的配置文件
  profiles:
    active: test
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        #加载yaml的nacos文件
        file-extension: yaml

可以看到启动时进行加载了文件如下:

5,nacos配置

配置了configdemo和configdemo-test.yaml

注意的是:它的加载规则是:# 1.DataId

- 用来读取远程配置中心的中具体配置文件其完整格式如下:

- ${prefix}-${spring.profile.active}.${file-extension}

a. prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。

b. spring.profile.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档。 注意:当 spring.profile.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}

c. file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。

如果configdemo和configdemo-test.yaml 都存在name的配置,优先configdemo-test.yaml

访问结果如下:

以上是针对同个服务不同环境配置应用情况。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们