SpringCloud-Alibaba 之 Nacos
并且 Nacos 与目前主流的开源生态都提供了很好的支持:
1、Nacos 是专为 Dubbo 而生的注册中心与配置中心
2、Nacos 会完全兼容 Spring Cloud
3、Nacos 支持 Service Mesh 集成,Kubernetes 集成
除此之外,阿里巴巴正在通过 Dubbo + Nacos 以及一系列开源项目打造服务发现、服务及流量管理、服务共享平台,未来还在不断地发展和演进,相信未来会有更多的地方上使用 Nacos。
单机部署
准备环境
Nacos 依赖 Java 环境来运行,所以部署前需要保证环境要求:
1、64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix。
2、64 bit JDK 1.8+
去 github 上搜索 nacos,发现 releases 发布版本

解压运行

这里 Nacos 单机部署方式使用命令?-m standalone?,如果是 Windows 命令,可以直接双击?startup.cmd?文件即可。
浏览器访问 http://{ip}:8848/nacos/index.html,点击就进入了 Nacos 的可视化管理界面,需要账号密码登录访问,默认都为****nacos
登录之后就能在网站侧边栏上看到 Nacos 的主要功能菜单:配置管理,服务管理,集群管理,命名空间。

服务提供者
通过一个简单的示例来感受一下如何将服务注册到 Nacos,其实和 Eureka 没有太大差别
POM
创建一个工程名为 shop-goods-service 的服务提供者项目,pom.xml 配置如下:
注:因为我们使用 Dubbo 的 RPC,所以也引用了 Dubbo;这个以后会讲


Application
通过**@EnableDiscoveryClient 注解表明是一个 Nacos 客户端,该注解是 Spring Cloud 提供的原生注解**

定义接口实现



注意:GoodsServiceImpl 的接口实现类上面有 @Service 注解,是 org.apache.dubbo.config.annotation.Service
application.yml

启动工程
通过浏览器访问?http://192.168.5.153:8848/nacos,即 Nacos Server 网址

你会发现一个服务已经注册在服务中了,服务名为 goods-provider。这个过程其实蛮简单的,跟之前 dubbo 的使用没什么不一样。
当然这个是服务提供者,要想看到效果,我们再来新建服务消费者
服务消费者
POM
这个 POM 文件和提供者很类似,因为消费者我们将用 web 演示,所以增加个依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Application

application.yml

注意:服务提供者和消费者,yml 配置中,在 2.1.x 后需要增加
spring.main.allow-bean-definition-overriding: true
服务消费
新建一个 Controller,请求某个商品 id 的基本信息。

注意:@Reference 这个注解是 org.apache.dubbo.config.annotation.Reference
评论