赞
踩
nacos安装:
nacos下载安装
选择springcloudalibaba和springboot版本
springcloudalibaba 2.2.8.RELEASE
springboot 2.3.12.RELEASE
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.1.0</version> </dependency> </dependencies>
项目建立成功后运行下项目,没有问题,不报错再进行下一步。
删除项目下面的两个properties文件,改为yml文件
spring: application: #nacos的yaml文件与它一致 name: springboot-nacos cloud: nacos: config: server-addr: 127.0.0.1:8848 #这个是命名空间id namespace: config file-extension: yaml group: DEFAULT_GROUP discovery: server-addr: 127.0.0.1:8848 bootstrap: enabled: true # profiles: # active: dev
启动本地nacos,进入建立config命名空间
建立名为springboot-nacos.yaml的配置文件
在里面简单写一点配置信息
server:
port: 8500
建立HelloController文件
@RestController
@RefreshScope
@RequestMapping(value = "/test",method = RequestMethod.GET)
public class HelloController {
@GetMapping("/getValue")
public String getValue() {
System.out.println("helloController_1");
return "test";
}
}
启动项目
浏览器输入url
localhost:8500/test/getValue
如果控制台打印信息,网页显示test内容说明nacos的配置文件已经被使用。
springcloud整合nacos成功。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。