怎末說呢,耗時兩天大概把能踩得坑都踩了
1,下載安裝
地址
https://github.com/alibaba/nacos/releases
拉到下面找到這里,下載安裝
啟動服務器
Linux/Unix/Mac
啟動命令(standalone代表着單機模式運行,非集群模式):
sh startup.sh -m standalone
如果您使用的是ubuntu系統,或者運行腳本報錯提示[[符號找不到,可嘗試如下運行:
bash startup.sh -m standalone
Windows
啟動命令(standalone代表着單機模式運行,非集群模式):
startup.cmd -m standalone
找到文件的bin下的startup.cmd 文本打開修改如上
雙擊啟動
至此nacos可以正常啟動,
登錄
賬號密碼均為:nacos
bootstrap.yml配置
spring: application: name: com.ebiz.han.user//對應Data ID cloud: nacos: config: server-addr: 127.0.0.1:8848 file-extension: yaml//對應nacos配置的后綴
注意暫時只支持yaml和properties格式
在創建springcloud項目時,最需要注意的問題就是版本對應的問題,請仔細看如下:
上面比較雜,下面來回顧一個完整配置,建項目就不展示了
Data ID
依賴
-
<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> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- <dependency>--> <!-- <groupId>com.ebiz</groupId>--> <!-- <artifactId>spring_cloud_provider_client</artifactId>--> <!-- <version>0.0.1-SNAPSHOT</version>--> <!-- </dependency>--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.4.RELEASE</version> </dependency> </dependencies>
server-addr :nacos地址, 127.0.0.1等價於localhost, 主機地址
注意:bootstrap為nacos的默認配置,也就是說idea會去找bootstrap.yml而不是application,application相當於整個項目的全局配置
、
ConfigController
package com.ebiz.nacos.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${user:false}")
private String user;
@RequestMapping(value = "/get",method = RequestMethod.GET)
public String get(){
return user;
}
}
請求成功
這里我用的postman軟件,感興趣可以去下載!