什么是Nacos?
引入官方的一段话:Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。
Nacos是Alibaba开源的一套微服务的治理方案,文档有中文版的,详情请看:
Nacos官方文档,这里就不啰嗦了...
Nacos的安装过程
首先来到官网如:Nacos下载地址,我们只需要下载nacos-server即可,直接下载zip的解压即可,如图:

解压之后我们来看看项目结构:

bin目录下是各大平台的对于Nacos的启动命令,当我们启动对应的命令时实质是在调用target目录的jar,猿友们可以自己去看,解压之后我们来启动它,命令如下:
startup.sh -m standalone
注意:前期是通过cmd切换到Nacos的bin目录下,我们来看一下启动的结果图:

上图是在启动的过程中的一个截图,可以发现Nacos的很洋气的端口8848,等启动完成后,我们访问 http://localhost:8848/nacos/index.html进入它的管理台如图:

需要我们输入用户名和密码验证,默认都是nacos,之后来到管理台,如图:

至此我们的Nacos安装完毕
构建服务
创建项目名为alibaba-nacos-producer作为服务的提供者
- 在pom.xml文件中,代码如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 配置文件中,代码如下:
server:
port: 8080
spring:
application:
name: alibaba-nacos-producer
cloud:
nacos:
discovery:
server-addr: localhost:8848
- 在我们的启动类上需要贴上注解EnableDiscoveryClient,这样Nacos会发现服务,代码如下:
package com.cacmo.alibaba.nacos.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author cb
*/
@SpringBootApplication
@EnableDiscoveryClient
public class AlibabaNacosProducerApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaNacosProducerApplication.class, args);
}
启动服务,打开Nacos控制台,如图:

发现我们的服务并没有注册进来,郁闷了很久,后来查看了对应各自的版本,才知道问题在哪了,我们来看下当前配置的版本,如图:


由上图我们发现我的springboot版本为2.2.0,而springcloud的版本为Finchleny.SR1版本的,在官方中的要求是这样说明的,我们来看图:

所以,我这里需要用springcloud版本为Greenwich,再一次的启动我们的服务看是否注册进去.

从上图可以看到我们的服务已经注册进去,我们来写一个接口供消费者来调用,代码如下:
package com.cacmo.alibaba.nacos.producer.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author cb
*/
@RestController
public class HelloController {
@RequestMapping("hello")
public String hello(String name){
return "hello:"+name;
}
接着我们来创建服务的消费方,我这里服务名为alibaba-nacos-consumer,建的过程和服务提供方一样,这里就不在贴pom文件了,来看配置文件,代码入下:
server:
port: 9000
spring:
application:
name: alibaba-nacos-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
接着我们写一个调用服务方的接口代码,如下:
package com.cacmp.alibaba.nacos.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author cb
*/
@RestController
public class ConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@RequestMapping("consumer")
public String consumer(){
ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-producer");
String uri = serviceInstance.getUri().toString();
String object = restTemplate.getForObject(uri + "/hello?name="+"nacos!", String.class);
System.out.println(object);
return object;
}
启动我们的消费者项目,同样我们将它也注册到Nacos中,访问http://localhost:9000/consumer,我们会看到如下结果:


关于Nacos的简单使用就是这样的.......,最后附上本案例的代码示例:
网友评论