nacos注册中心
一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
# 官方文档
https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md
# 将微服务注册到注册中心
1.在common模块引入pom
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2
3
4
2.在应用的 /src/main/resources/application.properties 配置文件中配置 Nacos Server 地址(YML也行)
(事实上,这一步做也行,因为默认就是localhost:8848
,如果将来改了再写)
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
3.使用 @EnableDiscoveryClient 注解开启服务注册与发现功能
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@RestController
class EchoController {
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return string;
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 启动 Nacos Server
- 首先需要获取 Nacos Server,支持直接下载和源码构建两种方式。
- 直接下载:Nacos Server 下载页 (opens new window)
- 源码构建:进入 Nacos Github 项目页面 (opens new window),将代码 git clone 到本地自行编译打包,参考此文档 (opens new window)。推荐使用源码构建方式以获取最新版本
- 启动 Server,进入解压后文件夹或编译打包好的文件夹,找到如下相对文件夹 nacos/bin,并对照操作系统实际情况之下如下命令。
- Linux/Unix/Mac 操作系统,执行命令
sh startup.sh -m standalone
- Windows 操作系统,执行命令
cmd startup.cmd
- Linux/Unix/Mac 操作系统,执行命令
# naocs启动失败
参考连接:https://github.com/alibaba/nacos/issues/3607
I find the key of this problem, I found every failed start nacos the exceptions include some SQL exception,then i change application.properties and import SQL to my local database,and problem is resolved.
step 1. import data to your database use nacos-mysql.sql
step 2. change application.properties comments like below
#*************** Config Module Related Configurations ***************# ### If user MySQL as datasource: spring.datasource.platform=mysql ### Count of DB: db.num=1 ### Connect URL of DB: db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user=nacos db.password=nacos
1
2
3
4
5
6
7
8
9
10
11step 3.
start your nacos server with standalone MODE
hope to help someone like this situation.
@zongtanghu (opens new window) Give Some Suggest: I think Nacos (opens new window) quick start should give our some suggestions about change configuration.
Looking forward to getting better together.
启动 报错了,网上一搜是缺少数据库.
- 打开conf文件下的mysql文件,创建数据库nacos
- 运行sql文件
- 修改application.properties里的数据库连接,把注释取消掉
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://192.168.56.10:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=root
2
3
4
5
6
7
8
9
10
11
改成独立模式启动
startup.cmd
file MODE="cluster" change to MODE="standalone",Nacos Server can start.
重新启动naocs,已经成功了。
# 应用启动
增加配置,在 nacos-discovery-provider-example 项目的 /src/main/resources/application.properties 中添加基本配置信息
spring.application.name=service-provider server.port=18082
1
2启动应用,支持 IDE 直接启动和编译打包后启动。
- IDE直接启动:找到 nacos-discovery-provider-example 项目的主类
ProviderApplication
,执行 main 方法启动应用。 - 打包编译后启动:在 nacos-discovery-provider-example 项目中执行
mvn clean package
将工程编译打包,然后执行java -jar nacos-discovery-provider-example.jar
启动应用。
- IDE直接启动:找到 nacos-discovery-provider-example 项目的主类
# 验证
从上面naocs启动,就可用看到,naocs的访问地址:
http://192.168.137.1:8848/nacos/index.html
http://127.0.0.1:8848/nacos
2
3
两个都可用,一般我用下面那个
账号密码默认都是nacos
在服务管理-服务列表下,已经发现服务已经注册到注册中心
同理,其他微服务也可用参照《将微服务注册到注册中心》的第2和第3步即可。