Nacos 名字的由來(取紅色的英文字符): Dynamic
Na
ming andCo
nfigurationS
ervice 動態命名和配置服務html
Nacos 是阿里巴巴開源的一個更易於構建雲原生應用的動態服務發現、配置管理和服務管理平臺。java
Nacos 既能夠做爲註冊中心也能夠做爲配置中心,至關於SpringCloud中 Eureka + Config 的組合,而且Eureka 註冊中心已經宣佈中止更新了,Nacos提供了比它們更強大的功能,並將其合二爲一幫助開發者更容易構建分佈式系統。git
Nacos 做爲註冊中心在不一樣場景下能夠切換成 CAP理論
中的 CP
或 AP
模式github
服務發現與服務健康檢測web
動態配置服務spring
動態DNS服務apache
服務機器元數據管理bootstrap
Nacos官網地址ubuntu
Nacos 依賴 Java 環境來運行。若是您是從代碼開始構建並運行Nacos,還須要爲此配置 Maven環境,請確保是在如下版本環境中安裝使用:
Linux/Unix/Mac
啓動命令(standalone表明着單機模式運行,非集羣模式):
sh startup.sh -m standalone
若是您使用的是ubuntu系統,或者運行腳本報錯提示[[符號找不到,可嘗試以下運行:
bash startup.sh -m standalone
Windows
啓動命令:
cmd startup.cmd
或者雙擊startup.cmd運行文件。
啓動後打開Nacos服務地址 http://192.168.10.1:8848/nacos/index.html ,初始用戶名和密碼均是nacos。
服務註冊
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
服務發現
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'
發佈配置
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"
獲取配置
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
Linux/Unix/Mac
sh shutdown.sh
Windows
cmd shutdown.cmd
或者雙擊shutdown.cmd運行文件。
下面是咱們常見的nacos 界面,給你們介紹一個左側的菜單功能
本示例說明了如何使用 Nacos Discovery Starter 實現 SpringCloud 應用服務發現。
Nacos 是阿里巴巴開源的一個更易於構建雲原生應用的動態服務發現、配置管理和服務管理平臺。
第一步: 建立一個普通的 SpringCloud 項目
第二步:在項目中添加 spring-cloud-starter-alibaba-nacos-discovery
完整 pom 文件以下所示:
<dependencies> <!-- nacos discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--spring cloud alibaba 2.2.0.RELEASE--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
第三步:/src/main/resources/application.properties 文件中添加Nacos服務地址相關配置
spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # nacos地址 application: name: nacos-provider-server # 服務名 server: port: 7001 # 端口號
第四步:主啓動類中加入註解 @EnableDiscoveryClient 開啓服務註冊與發現
@SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @RestController class EchoController { @GetMapping(value = "/echo/{string}") public String echo(@PathVariable String string) { return "Nacos server: " + string; } } }
第五步:啓動Nacos服務器
第六步:啓動應用
瀏覽器訪問 http://localhost:7001/echo/HelloWorld
查看Nacos服務器管理臺界面,能夠看到在服務列表中多了剛纔啓動的服務
爲了便於使用,NacosServerList 實現了 com.netflix.loadbalancer.ServerList 接口,並在 @ConditionOnMissingBean 的條件下進行自動注入。若是您有定製化的需求,能夠本身實現本身的 ServerList。
Nacos Discovery Starter 默認集成了 Ribbon ,因此對於使用了 Ribbon 作負載均衡的組件,能夠直接使用 Nacos 的服務發現。
下面按照步驟建立項目 cloud-nacos-consumer-8001 服務,驗證如何使用 RestTemplate 與 FeignClient
(1)添加 @LoadBlanced 註解,使得 RestTemplate 接入 Ribbon
@Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }
(2)配置 FeignClient
@FeignClient(name = "nacos-provider-server") public interface EchoService { @GetMapping(value = "/echo/{str}") String echo(@PathVariable("str") String str); }
使用 @FeignClient 註解將 EchoService 這個接口包裝成一個 FeignClient,屬性 name 對應服務名 service-provider。
echo 方法上的 @RequestMapping 註解將 echo 方法與 URL "/echo/{str}" 相對應,@PathVariable 註解將 URL 路徑中的 {str}
對應成 echo 方法的參數 str。
(3)完成以上配置後,將二者自動注入到 TestController 中。
@RestController public class TestController { @Autowired private RestTemplate restTemplate; @Autowired private EchoService echoService; @GetMapping(value = "/echo-rest/{str}") public String rest(@PathVariable String str) { return restTemplate.getForObject("http://nacos-provider-server/echo/" + str, String.class); } @GetMapping(value = "/echo-feign/{str}") public String feign(@PathVariable String str) { return echoService.echo(str); } }
(4)啓動項目
(5)在瀏覽器輸入 http://localhost:8001/echo-rest/helloWorld, http://localhost:8001/echo-feign/helloWorld 訪問成功
Spring Cloud Nacos Discovery 遵循了 spring cloud common 標準,實現了 AutoServiceRegistration、ServiceRegistry、Registration 這三個接口。
在 spring cloud 應用的啓動階段,監聽了 WebServerInitializedEvent 事件,當Web容器初始化完成後,即收到 WebServerInitializedEvent 事件後,會觸發註冊的動做,調用 ServiceRegistry 的 register 方法,將服務註冊到 Nacos Server。
NacosServerList 實現了 com.netflix.loadbalancer.ServerList 接口,並在 @ConditionOnMissingBean 的條件下進行自動注入,默認集成了Ribbon。
若是須要有更加自定義的能夠使用 @Autowired 注入一個 NacosRegistration 實例,經過其持有的 NamingService 字段內容直接調用 Nacos API。
Nacos 配置中心支持 namespace(命名空間)、group(分組)、dataId(數據id) 三個屬性肯定一個配置
- namespace:即命名空間,能夠配置成 dev、test、pro 用於切換不用環境的配置,
默認 public
- group:即分組,能夠將同一個項目下的不用服務配置命名同一個分組統一管理,
默認 DEFAULT_GROUP
- dataId: 即數據id,一般
dataId = ${spring.application.name}.${file-extension:properties}
或者dataId =${spring.application.name}-${profile}.${file-extension:properties}
兩種狀況組成- 三者均可以在配置文件中自定義配置
namespace、group、dataId 三者之間的關係以下圖所示
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
spring: application: name: nacos-provider-server # 應用名 cloud: nacos: config: server-addr: 127.0.0.1:8848 # nacos 服務地址 server: port: 7001 #端口號
- 若是須要使用域名配置nacos服務地址,格式必須按照
domain name:port
,例如 nacos.abc.com:80- file-extension 默認值爲 properties
注意:根據前面介紹的dataId生成規則,這裏的 dataId = ${spring.application.name}.${file-extension:properties}
即:dataId = nacos-provider-server.properties
@SpringBootApplication @EnableDiscoveryClient public class NacosProviderMain7001 { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosProviderMain7001.class, args); String userName = applicationContext.getEnvironment().getProperty("user.name"); String userAge = applicationContext.getEnvironment().getProperty("user.age"); System.err.println("user name :" +userName+"; age: "+userAge); } @RestController class EchoController { @GetMapping(value = "/echo/{string}") public String echo(@PathVariable String string) { return "Nacos server: " + string; } } }
- nacos 支持配置動態刷新
- nacos 配置動態刷新是默認開啓的
- 若是想要關閉動態刷新功能,修改配置爲
spring.cloud.nacos.config.refresh.enabled=false
便可關閉動態刷新- 若是須要在SpringBoot 配置類中動態讀取 Nacos 配置有兩種方式
- 配置類上加註解 @RefreshScope(springcloud 提供) ,配置屬性上依然使用 @Value 註解
- 或者直接將配置屬性上的@Value註解替換爲@NacosValue(nacos 提供)註解,並設置autoRefreshed=true
@SpringBootApplication @EnableDiscoveryClient public class NacosProviderMain7001 { public static void main(String[] args) throws InterruptedException { ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosProviderMain7001.class, args); while (true){ String userName = applicationContext.getEnvironment().getProperty("user.name"); String userAge = applicationContext.getEnvironment().getProperty("user.age"); System.err.println("user name :" +userName+"; age: "+userAge); TimeUnit.SECONDS.sleep(1); } } @RestController class EchoController { @GetMapping(value = "/echo/{string}") public String echo(@PathVariable String string) { return "Nacos server: " + string; } } }
若是在項目啓動過程當中修改 nacos中的配置,那麼控制檯打印的結果也會隨之變化