当前位置: 首页 > news >正文

springboot链接nacos测试

代码资料链接:https://download.csdn.net/download/ly1h1/90881498

场景说明:本次测试是springboot项目,可以链接上ncaos,将对应命名空间下的配置信息读取出俩,然后可以在接口进行返回显示。

0.环境配置

1.代码结构

2.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.itheima</groupId><artifactId>demonacostest</artifactId><version>0.0.1-SNAPSHOT</version><name>demonacostest</name><description>demonacostest</description><properties><java.version>8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-bootstrap</artifactId>-->
<!--            <version>3.0.3</version>-->
<!--        </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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3.bootstrap.yml如下,不需要application.yml

spring:application:name: nacos-testcloud:nacos:config:server-addr: 43.162.118.209:8848username: nacospassword: nacosnamespace: f90776be-d19e-41c4-b55f-4cdcae33fa57file-extension: yaml
# 本地配置示例
app:local-config: 这是本地配置

4.配置属性模型

ConfigProperties
package com.itheima.demonacostest.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;import java.io.Serializable;@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "base.config")
public class ConfigProperties implements Serializable {private String appKey;private String appKey2;
}

5.接口如下

package com.itheima.demonacostest.controller;import com.itheima.demonacostest.config.ConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api/config")
public class ConfigController {@Autowiredprivate ConfigProperties nacosConfigProperties;@RequestMapping(value = "getConfig", method = RequestMethod.GET)public String getConfig() {return nacosConfigProperties.getAppKey();}@RequestMapping(value = "getConfig2", method = RequestMethod.GET)public String getConfig2() {return nacosConfigProperties.getAppKey2();}
}

6.nacos配置如下

7.效果

相关文章:

  • 项目执行中缺乏风险管理,如何预防潜在问题?
  • 惠斯通电桥测量 数据采集模块 支持恒压/恒流的24位ADC电桥测量
  • PCB设计教程【入门篇】——电路分析基础-元件数据手册
  • 设计模式介绍
  • 解除diffusers库的prompt长度限制(SDXL版)
  • vue原生table表格实现动态添加列,一行添加完换行继续添加。el-select输入框背景颜色根据所选内容不同而改变
  • 深入解读RTP协议:RFC 3550的技术分析与应用
  • 在线地图瓦片URL
  • Spring Framework 的 spring-core 和 Spring Security 兼容版本
  • springboot3+vue3融合项目实战-大事件文章管理系统-自定义校验
  • 预警功能深度测评:如何用系统降低设备突发故障率?
  • 基于 STM32 的 PC ARGB 风扇控制器设计与实现
  • 工作安排小K
  • Elasticsearch生产环境性能调优指南
  • Unity中GPU Instancing使用整理
  • 全方位详解微服务架构中的Service Mesh(服务网格)
  • 互联网大厂Java求职面试:Spring Cloud微服务架构与AI集成挑战
  • 如何在 Android 手机和平板电脑上下载应用程序
  • ATT Global赞助非小号全球行,引领RWA创新浪潮
  • springboot 1.x2.x依赖spring版本
  • 传统文化网站建设/谷歌手机网页版入口
  • 积分商城网站开发/域名收录
  • 网站开发分类/seo外包公司报价