当前位置: 首页 > java >正文

Spring Cloud Alibaba快速入门02-Nacos

文章目录

  • 前言
  • Nacos安装
  • 启动nacos单机模式
  • 实现注册中心-服务注册
    • 步骤1 - 启动微服务
    • 步骤2 - 引入服务发现依赖
    • 步骤3 - 配置Nacos地址
    • 步骤4 - 查看注册中心效果
    • 步骤5 - 集群模式启动测试
  • 实现注册中心-服务发现
    • 模拟掉线


在这里插入图片描述

前言

Nacos(Naming Configuration Service)是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

它由阿里巴巴开源,并已成为 Spring Cloud Alibaba 生态系统的核心组件,同时完美集成到 Spring Cloud 体系中,可以替代 Netflix Eureka、Consul、ZooKeeper 等组件。

它的核心功能可以拆解为两个词:

  1. Naming (服务发现与注册)
  2. Configuration (配置管理)

Nacos安装

  • Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service的首字母简称,一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
  • 官网:https://nacos.io/zh-cn/docs/v2/quickstart/quick-start.html
  • 安装:
    • 下载安装包【2.4.3】
    • 启动命令: startup.cmd -m standalone

nacos历史版本地址:https://nacos.io/download/release-history/?spm=5238cd80.47ee59c.0.0.189fcd36EUPree

启动nacos单机模式

1.解压nacos-server-2.4.3.zip后进入bin
2.cmd进入黑窗口
3.使用 startup.cmd -m standalone 命令启动nacos单机模式
在这里插入图片描述

实现注册中心-服务注册

步骤流程如下
在这里插入图片描述

步骤1 - 启动微服务

进入services-order
pom.xml依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

步骤2 - 引入服务发现依赖

因为要把order注册到注册中心,所以导入nacos依赖(这里在services父项目中已经导入)

步骤3 - 配置Nacos地址

现在需要将当前项目注册到注册中心,所以需要在application.yml中配置nacos地址(告诉当前应用nacos在哪里)
启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}

application.yml

server:port: 8080servlet:context-path: /
spring:application:name: qf-service-order# 配置nacos地址(告诉当前应用nacos在哪里)cloud:nacos:server-addr: 127.0.0.1:8848

项目结构
在这里插入图片描述

步骤4 - 查看注册中心效果

访问:http://127.0.0.1:8848/nacos/index.html
在这里插入图片描述

在这里插入图片描述

步骤5 - 集群模式启动测试

打开左下角Services
在这里插入图片描述
如果没有添加(为空),则需要先添加
在这里插入图片描述
选择其中spring boot选项
出现以下情况
在这里插入图片描述
点击右键,复制一个服务
在这里插入图片描述
点击后修改服务名称,修改服务端口
在这里插入图片描述

在这里插入图片描述
完整配置
–server.port=8081
在这里插入图片描述
点击ok
配置完成后同一时间启动
1.shift选择要启动的项目
2.点击右键,选择Rerun
在这里插入图片描述
启动成功后可以在nacos上看到启动的服务
在这里插入图片描述

实现注册中心-服务发现

在这里插入图片描述使用@EnableDiscoveryClient开启服务发现功能

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient//开启服务发现功能
@SpringBootApplication
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class, args);}
}

导入单元测试依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><!--只在test目录下生效-->
</dependency>

执行

import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.exception.NacosException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;import java.util.List;@SpringBootTest
public class DiscoveryTest {@AutowiredDiscoveryClient discoveryClient;@Testvoid discoveryClientTest(){for (String service : discoveryClient.getServices()) {System.out.println("service = " + service);//获取ip+portList<ServiceInstance> instances = discoveryClient.getInstances(service);for (ServiceInstance instance : instances) {System.out.println("ip:"+instance.getHost()+";"+"port = " + instance.getPort());}}/*service = qf-service-orderip:192.168.109.1;port = 8080ip:192.168.109.1;port = 8081service = qf-service-productip:192.168.109.1;port = 8181ip:192.168.109.1;port = 8180ip:192.168.109.1;port = 8182*/}@AutowiredNacosServiceDiscovery nacosServiceDiscovery;@Testvoid  nacosServiceDiscoveryTest() throws NacosException {for (String service : nacosServiceDiscovery.getServices()) {System.out.println("service = " + service);List<ServiceInstance> instances = nacosServiceDiscovery.getInstances(service);for (ServiceInstance instance : instances) {System.out.println("ip:"+instance.getHost()+";"+"port = " + instance.getPort());}}/*service = qf-service-orderip:192.168.109.1;port = 8080ip:192.168.109.1;port = 8081service = qf-service-productip:192.168.109.1;port = 8181ip:192.168.109.1;port = 8180ip:192.168.109.1;port = 8182*/}
}

DiscoveryClient是spring的规范,NacosServiceDiscovery是nacos的

模拟掉线

在这里插入图片描述
运行nacosServiceDiscoveryTest()

service = qf-service-order
ip:192.168.109.1;port = 8080
service = qf-service-product
ip:192.168.109.1;port = 8181
ip:192.168.109.1;port = 8180
ip:192.168.109.1;port = 8182

http://www.xdnf.cn/news/20460.html

相关文章:

  • FRCNet
  • Fab资源快速导入UE
  • Shell 脚本实现系统监控与告警
  • Spring Boot中MyBatis的定义与使用
  • IOC为什么交由spring容器管理?
  • 操作系统研发工作心得体会 - 于复杂性中构建秩序
  • 每日一题(2)
  • MySQL学习记录-索引
  • 携程社招前端面经
  • pthread_detach函数
  • 2025最新超详细FreeRTOS入门教程:第二章 FreeRTOS任务创建
  • 设计一个 AB 测试平台
  • 实例和对象的区别
  • 【目录-单选】鸿蒙HarmonyOS开发者基础
  • 自适应滤波器:Ch4 最小均方(LMS)算法
  • [光学原理与应用-433]:晶体光学 - 晶体光学是研究光在单晶体中传播规律及其伴随现象的分支学科,聚焦于各向异性光学媒质的光学特性
  • 上海“我店”模式:消费增值新玩法及其隐忧
  • 论文阅读:VGGT Visual Geometry Grounded Transformer
  • 【C++】引用的本质与高效应用
  • 【高等数学】第十一章 曲线积分与曲面积分——第三节 格林公式及其应用
  • javascript 国际化方法
  • AI 生成式艺术重塑动漫角色创作:从技术逻辑到多元可能性(一)
  • GPT-5发布:统一智能体时代的开启——从“工具”到“协作者”的范式跃迁
  • 详解MySQL环境变量配置及其在备份中的应用
  • 计算机内存的工作原理
  • 打工人日报#20250906
  • 模电仿真软件:MultSim14.3下载与安装
  • 【面板数据】各省制造业出口技术复杂度数据集(2010-2023年)
  • AP1271:高性能低功耗LDO稳压器,为精密电子设备提供稳定动力
  • python graphviz中文测试