spring cloud alibaba教程:如何使用nacos作为配置中心
startup.cmd -m standalone
1.下载与解压Nacos压缩包(Nacos安装指南具体教程可查看)
下载地址:https://github.com/alibaba/nacos/tags
- 在bin目录下进入cmd,输入
startup.cmd -m standalone
即可
3.启动成功,访问测试(默认端口8848,用户名:nacos,密码:nacos)
4.新建一个springboot项目
5.修改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 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.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- nacos 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR5</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>
- 新建bootstrap.yml
bootstrap.yml
spring:
application:
name: demo
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
prefix: demo1
namespace: 1f5f553b-33d1-4628-99e0-d46c45aaa455
username: nacos
password: nacos
# profiles:
# active: dev
7.运行,成功,已使用了Nacos配置文件demo1的属性
8.新建controller,输出属性字段
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: demo
* @author: daPangDong
* @create: 2022-04-11 17:01
*/
@RestController
public class testController {
@Value("${name}")
private String number;
@GetMapping("/getNumber")
public String getNumber(){
return number;
}
}
9.成功
(^-^)V
相关文章
暂无评论...