Spring Cloud學(xué)習(xí)筆記(九) Nacos

Spring Cloud學(xué)習(xí)筆記(九) Nacos

1. 概念

nacos的官方文檔為:https://nacos.io/zh-cn/docs/what-is-nacos.html??偟膩?lái)說(shuō)就是實(shí)現(xiàn)了服務(wù)發(fā)現(xiàn)組件和配置服務(wù)器,整體結(jié)構(gòu)如下:

image

引入nacos后,服務(wù)注冊(cè)與發(fā)現(xiàn)的實(shí)現(xiàn)調(diào)整為:

image

2. 搭建Nacos Server

2.1 下載Nacos

Nacos的下載地址為:https://github.com/alibaba/nacos/releases。找到最新版本的Nacos Server包進(jìn)行下載。

2.2 啟動(dòng)Nacos Server

下載后進(jìn)入bin目錄,執(zhí)行:

<pre mdtype="fences" cid="n47" lang="" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">sh startup.sh -m standalone</pre>

<pre mdtype="fences" cid="n51" lang="" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">startup.sh: line 131: /Users/yubuyun/Documents/workspace/nacos-server/nacos 2/logs/start.out: No such file or directory</pre>

<pre mdtype="fences" cid="n57" lang="" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">找不到或無(wú)法加載主類(lèi)...</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n75" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"><dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
?
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement></pre>

<pre mdtype="fences" cid="n86" lang="xml" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency></pre>

<pre mdtype="fences" cid="n94" lang="yaml" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: blog-center</pre>

<pre mdtype="fences" cid="n116" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">@Autowired
private DiscoveryClient discoveryClient;
?
@GetMapping("/test/nacos")
public List<ServiceInstance> testNacos(){
return this.discoveryClient.getInstances("test-center");
}</pre>

<pre mdtype="fences" cid="n120" lang="json" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">[
{
"serviceId":"test-center",
"host":"192.168.0.102",
"port":8081,
"secure":false,
"metadata":{
"nacos.instanceId":"192.168.0.102#8081#DEFAULT#DEFAULT_GROUP@@test-center",
"nacos.weight":"1.0",
"nacos.cluster":"DEFAULT",
"nacos.healthy":"true",
"preserved.register.source":"SPRING_CLOUD"
},
"uri":"http://192.168.0.102:8081",
"scheme":null,
"instanceId":null
}
]</pre>

<pre mdtype="fences" cid="n134" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">@RestController
public class NacosController {
?
@RequestMapping("/test")
public String test(){
return "hello,world";
}
}</pre>

<pre mdtype="fences" cid="n140" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
?
@GetMapping("/test/nacos")
public void testNacos(){
List<ServiceInstance> serviceInstanceList = this.discoveryClient.getInstances("test-center");
?
if(!CollectionUtils.isEmpty(serviceInstanceList)){
try {
// 從服務(wù)實(shí)例列表中取第一個(gè)實(shí)例的url
String testUrl = serviceInstanceList.
stream().
map(serviceInstance -> {
return serviceInstance.getUri().toString()+"/test";
}).
findFirst().
orElseThrow(()-> new Exception("沒(méi)有找到實(shí)例"));
?
log.info("url={}",testUrl);
// 訪問(wèn)具體的實(shí)例
String result = this.restTemplate.getForObject(testUrl,String.class);
log.info("result={}",result);
?
} catch (Exception e) {
e.printStackTrace();
}
}
}</pre>

<pre mdtype="fences" cid="n144" lang="" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">url=http://192.168.0.102:8081/test
result=hello,world</pre>

  • 作用:提供描述信息,讓微服務(wù)調(diào)用更加靈活。

  • 元數(shù)據(jù)metadata可以分為服務(wù)級(jí)別,集群級(jí)別和實(shí)例級(jí)別。

6. 元數(shù)據(jù)

  • Namespace: 命名空間。如開(kāi)發(fā)環(huán)境,測(cè)試環(huán)境,生產(chǎn)環(huán)境可分為三個(gè)命名空間。

  • Group: 服務(wù)分組,暫時(shí)不用。

  • Service: 微服務(wù)

  • Cluster: 集群,如設(shè)置上海的機(jī)房為一個(gè)集群,深圳的機(jī)房為一個(gè)集群。服務(wù)間調(diào)用時(shí)最好優(yōu)先調(diào)用本集群內(nèi)的實(shí)例,以減少網(wǎng)絡(luò)傳輸。

  • Instance: 具體的實(shí)例。

image

使用Nacos的時(shí)候涉及到幾個(gè)概念:

5. 模型

這樣說(shuō)明blog-center成功訪問(wèn)了test-center應(yīng)用中某一個(gè)實(shí)例的方法了。

最終訪問(wèn)該方法時(shí)打印如下:

然后在blog-center中添加一個(gè)訪問(wèn)test-center的方法:

為了讓blog-center應(yīng)用訪問(wèn)test-center應(yīng)用中某一個(gè)具體的實(shí)例,首先在test-center應(yīng)用加一個(gè)控制器來(lái)處理請(qǐng)求:

4.2 服務(wù)間調(diào)用實(shí)現(xiàn)

這樣就可以看出,test-center這個(gè)應(yīng)用是可以被訪問(wèn)的,uri參數(shù)指明了具體實(shí)例的地址。

這個(gè)方法用以獲取指定服務(wù)名的所有實(shí)例信息,返回如下:

然后在blog-center這個(gè)服務(wù)上添加一個(gè)訪問(wèn)test-center服務(wù)的方法:

image

為此,首先照著第三節(jié)再實(shí)現(xiàn)一個(gè)服務(wù)并注冊(cè)到Nacos Server上,命名為test-center:

4.1 獲取應(yīng)用的所有實(shí)例

4. 服務(wù)間調(diào)用

可以看到應(yīng)用已經(jīng)成功注冊(cè)到Nacos上了。

image

配置完后啟動(dòng)應(yīng)用,然后進(jìn)入Nacos控制臺(tái)查看:

在application.yml文件中配置中指定Nacos服務(wù)端地址和當(dāng)前要注冊(cè)的應(yīng)用名

3.2 添加配置文件

然后再加上Nacos Client依賴(lài):

以支持Spring Cloud和Spring Cloud Alibaba:

在現(xiàn)成的Spring Boot項(xiàng)目中添加依賴(lài):

3.1 添加依賴(lài)

3. 搭建Nocas Client

啟動(dòng)成功后訪問(wèn):http://localhost:8848/nacos/#/login,就可以進(jìn)入控制臺(tái)了。輸入控制臺(tái)和密碼都為nacos就可以進(jìn)入了。

百度后發(fā)現(xiàn)這是因?yàn)楫?dāng)前下載的包結(jié)構(gòu)中nacos 2這個(gè)目錄名默認(rèn)有一個(gè)空格導(dǎo)致的,將這個(gè)目錄名中的空格刪除后重新執(zhí)行命令就可以了。

如果啟動(dòng)還報(bào)錯(cuò),在日志文件里面找到類(lèi)似報(bào)錯(cuò):

那就新建一個(gè)logs目錄,并在里面新建一個(gè)start.out文件用以保存日志。

啟動(dòng)可能會(huì)報(bào)錯(cuò)如下:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容