Spring Cloud学习笔记(九) Nacos
1. 概念
nacos的官方文档为:https://nacos.io/zh-cn/docs/what-is-nacos.html。总的来说就是实现了服务发现组件和配置服务器,整体结构如下:
引入nacos后,服务注册与发现的实现调整为:
2. 搭建Nacos Server
2.1 下载Nacos
Nacos的下载地址为:https://github.com/alibaba/nacos/releases。找到最新版本的Nacos Server包进行下载。
2.2 启动Nacos Server
下载后进入bin目录,执行:
<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;">找不到或无法加载主类...</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 {
// 从服务实例列表中取第一个实例的url
String testUrl = serviceInstanceList.
stream().
map(serviceInstance -> {
return serviceInstance.getUri().toString()+"/test";
}).
findFirst().
orElseThrow(()-> new Exception("没有找到实例"));
log.info("url={}",testUrl);
// 访问具体的实例
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>
作用:提供描述信息,让微服务调用更加灵活。
元数据metadata可以分为服务级别,集群级别和实例级别。
6. 元数据
Namespace: 命名空间。如开发环境,测试环境,生产环境可分为三个命名空间。
Group: 服务分组,暂时不用。
Service: 微服务
Cluster: 集群,如设置上海的机房为一个集群,深圳的机房为一个集群。服务间调用时最好优先调用本集群内的实例,以减少网络传输。
Instance: 具体的实例。
使用Nacos的时候涉及到几个概念:
5. 模型
这样说明blog-center成功访问了test-center应用中某一个实例的方法了。
最终访问该方法时打印如下:
然后在blog-center中添加一个访问test-center的方法:
为了让blog-center应用访问test-center应用中某一个具体的实例,首先在test-center应用加一个控制器来处理请求:
4.2 服务间调用实现
这样就可以看出,test-center这个应用是可以被访问的,uri参数指明了具体实例的地址。
这个方法用以获取指定服务名的所有实例信息,返回如下:
然后在blog-center这个服务上添加一个访问test-center服务的方法:
为此,首先照着第三节再实现一个服务并注册到Nacos Server上,命名为test-center:
4.1 获取应用的所有实例
4. 服务间调用
可以看到应用已经成功注册到Nacos上了。
配置完后启动应用,然后进入Nacos控制台查看:
在application.yml文件中配置中指定Nacos服务端地址和当前要注册的应用名
3.2 添加配置文件
然后再加上Nacos Client依赖:
以支持Spring Cloud和Spring Cloud Alibaba:
在现成的Spring Boot项目中添加依赖:
3.1 添加依赖
3. 搭建Nocas Client
启动成功后访问:http://localhost:8848/nacos/#/login,就可以进入控制台了。输入控制台和密码都为nacos就可以进入了。
百度后发现这是因为当前下载的包结构中nacos 2这个目录名默认有一个空格导致的,将这个目录名中的空格删除后重新执行命令就可以了。
如果启动还报错,在日志文件里面找到类似报错:
那就新建一个logs目录,并在里面新建一个start.out文件用以保存日志。
启动可能会报错如下: