Nacos2.0.3配置持久化与集群(跟着步骤走,绝对不会出错)
获取nacos
- java 1.8
- maven 3.2
发行版github地址:https://github.com/alibaba/nacos/releases
选择最新的稳定版本 nacos2.0.3
下载解压即可
选择我们需要下载的对应安装包
注意 所有路径上的文件夹一定得是非中文无空格的
不然会出现各种玄学的报错
打开mysql创建数据库 nacos-config(也可以自定义起名)库 执行这conf中的两个sql脚本文件
打开我们的conf 修改持久化文件
之后配置cluster.conf
我们要集群的地址 ip 端口
依次启动即可
查看 Nacos
服务注册进nacos client编写
创建新模块 e-commerce-naocs-client
所需依赖
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.2.3.RELEASEversion>
dependency>
<dependency>
<groupId>com.imooc.ecommercegroupId>
<artifactId>e-commerce-mvc-configartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zipkinartifactId>
dependency>
<dependency>
<groupId>org.springframework.kafkagroupId>
<artifactId>spring-kafkaartifactId>
<version>2.5.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-ribbonartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>io.github.openfeigngroupId>
<artifactId>feign-okhttpartifactId>
dependency>
<dependency>
<groupId>io.github.openfeigngroupId>
<artifactId>feign-gsonartifactId>
<version>11.0version>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
dependencies>
编写对应配置文件
这里我们使用 bootstrap.yml
应为nacos也可以上云配置我们要保证他优先被夹在,bootstrap优先级大于application 于是我们使用bootstrap
server:
port: 8000
servlet:
context-path: /ecommerce-nacos-client
spring:
application:
name: e-commerce-nacos-client #注册到nacos的服务名称 之后的服务调用 等等 这里在naocs配置上云的时候 会师config的前缀
cloud:
nacos:
discovery:
server-addr:http: 192.168.3.8:8848,192.168.3.8:8858,192.168.3.8:8868 #要注册的nacos服务中心的地址
enabled: true # 使用naocs作为服务注册中心
namespace: e25fa4c3-8b0b-4eb5-ae4f-6d1b8c7fd7ea #e25fa4c3-8b0b-4eb5-ae4f-6d1b8c7fd7ea 对应nacos上的配置命名空间的id
#暴露端点
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
代码语言:javascript复制编写一个查看nacos节点信息到方法
@Slf4j
@Service
public class NacosClientService {
public final DiscoveryClient discoveryClient;
public NacosClientService(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
/*
* 打印 nacos client 信息到日志中
* */
public List<ServiceInstance> getNacosClientinfo(String serviceid) {
log.info("request naocs client to get service instance info:[{}]", serviceid);
return discoveryClient.getInstances(serviceid);
}
代码语言:javascript复制controller
@RestController
@Slf4j
@RequestMapping("/nacos-client")
public class NacosClinetController {
private final NacosClientService nacosClientService;
public NacosClinetController(NacosClientService nacosClientService) {
this.nacosClientService = nacosClientService;
}
@GetMapping(value = "/service-instance")
public List<ServiceInstance> logNacosClientinfo(@RequestParam(defaultValue = "e-commerce-nacos-client") String serviceid) {
log.info("coming in log nacos client info :[{}]", serviceid);
return nacosClientService.getNacosClientinfo(serviceid);
}
}
这个时候我们用idea的访问脚本来看返回结果
编写脚本nacos-client.http
### 查询服务示例信息
http://localhost:8000/ecommerce-nacos-client/nacos-client/service-instance
Accept: application/json
查看返回结果
代码语言:javascript复制HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Fri, 03 Dec 2021 11:49:31 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 0,
"message": "",
"data": [
{
"serviceId": "e-commerce-nacos-client",
"host": "192.xxx.x.x",
"port": 8000,
"secure": false,
"metadata": {
"nacos.instanceId": "192.xxx.x.x#8000#DEFAULT#DEFAULT_GROUP@@e-commerce-nacos-client",
"nacos.weight": "1.0",
"nacos.cluster": "DEFAULT",
"nacos.ephemeral": "true",
"nacos.healthy": "true",
"preserved.register.source": "SPRING_CLOUD"
},
"uri": "http://192.xxx.x.x:8000",
"scheme": null,
"instanceId": null
}
]
}