SpringCloud 集成 nacos-config
程序员文章站
2022-07-12 23:22:28
...
1.下载安装Nacos
注:本文是Windows环境 ,版本是nacos-server-1.4.0
下载链接: https://github.com/alibaba/nacos/releases
解压缩之后得到文件夹,在该目录下单机模式启动,启动命令 startup.cmd -m standalone
本地启动成功后访问http://127.0.0.1:8848/nacos,用户名:nacos 密码: nacos
2.准备代码环境
2.1引入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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>nacos-config</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.2配置文件(由于本文是集成的nacos-cofig,注意是bootstrap.yml,不是application.yml)
server:
port: 8040
spring:
application:
name: nacos-config-demo
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
prefix: nacos-config
profiles:
active: dev
2.3 nacos管理界面新建配置
注(DataId格式为: ${prefix}-${spring.profiles.active}.${file-extension} )
2.4 JAVA代码使用
package com.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class MainApplication {
@Value("${nacos.config:test-local}")
private String config;
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
@RequestMapping("/test")
public String test(){
return config;
}
}
请求测试
推荐阅读
-
Spring Boot集成Mybatis的实例代码(简洁版)
-
springboot2.x集成swagger的方法示例
-
Spring Security OAuth2集成短信验证码登录以及第三方登录
-
spring集成okhttp3的步骤详解
-
spring boot 与kafka集成的示例代码
-
在vue项目中集成graphql(vue-ApolloClient)
-
Shiro集成SSM基于URL权限管理(一)
-
关于安装LNMP集成包后上传图片报500错误的解决方法
-
spring boot2.0.4集成druid,用jmeter并发测试工具调用接口,druid查看监控的结果
-
vue 集成 vis-network 实现网络拓扑图