cloud Alibaba电商后台组件实战:Nacos2.0.3配置持久化与集群(跟着步骤走,绝对不会出错)

本文详细介绍了如何配置Nacos2.0.3实现持久化存储以及搭建集群,包括下载Nacos、创建数据库、配置集群文件、启动服务等步骤。此外,还展示了如何将服务注册到Nacos并编写相关配置,以及通过示例代码展示如何获取Nacos服务实例信息。
摘要由CSDN通过智能技术生成

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>
       <!-- spring cloud alibaba nacos discovery 依赖 -->
       <dependency>
           <groupId>com.alibaba.cloud</groupId>
           <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
           <version>2.2.3.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>com.imooc.ecommerce</groupId>
           <artifactId>e-commerce-mvc-config</artifactId>
           <version>1.0-SNAPSHOT</version>
       </dependency>
       <!-- 数据绑定 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-configuration-processor</artifactId>
           <optional>true</optional>
       </dependency>
       <!-- nacos config -->
       <dependency>
           <groupId>com.alibaba.cloud</groupId>
           <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
       </dependency>
       <!-- zipkin = spring-cloud-starter-sleuth + spring-cloud-sleuth-zipkin-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-zipkin</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.kafka</groupId>
           <artifactId>spring-kafka</artifactId>
           <version>2.5.0.RELEASE</version>
       </dependency>
       <!-- Ribbon -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
       </dependency>
       <!-- open feign -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-openfeign</artifactId>
       </dependency>
       <!-- feign 替换 JDK 默认的 URLConnection 为 okhttp -->
       <dependency>
           <groupId>io.github.openfeign</groupId>
           <artifactId>feign-okhttp</artifactId>
       </dependency>
       <!-- 使用原生的 Feign Api 做的自定义配置, encoder 和 decoder -->
       <dependency>
           <groupId>io.github.openfeign</groupId>
           <artifactId>feign-gson</artifactId>
           <version>11.0</version>
       </dependency>
       <!-- 集成 hystrix -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
       </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

编写一个查看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);
    }

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

查看返回结果

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
    }
  ]
}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷环渊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值