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

单机模式下nacos的安装与使用

一、nacos 的安装(安装包方式)

① 下载安装包 https://nacos.io/download/nacos-server/ 并进行解压缩

unzip nacos-server-$version.zip
# 或者 tar -xvf nacos-server-$version.tar.gz
cd nacos/bin

② 修改配置文件 application.properties ,有 3 个与 客户端访问鉴权 相关的必填项,如果没有填写的话,启动服务时会被要求在控制台输入

### The default token (Base64 String):
nacos.core.auth.plugin.nacos.token.secret.key=### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=
nacos.core.auth.server.identity.value=

(可选)默认情况下,如果不做额外配置,Nacos 会使用嵌入式的 Derby 数据库(无需单独安装),适用于测试或轻量级部署场景,如果想将数据持久化至 mysql,需要修改配置文件

#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
### Deprecated configuration property, it is recommended to use `spring.sql.init.platform` replaced.
# spring.datasource.platform=mysql
# spring.sql.init.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=Asia/Shanghai
# db.user.0=nacos
# db.password.0=nacos### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2### the maximum retry times for push
nacos.config.push.maxRetryTime=50

④ 启动服务 sh startup.sh -m standalone(Linux) 或者 startup.cmd -m standalone(Windows)

⑤ 进入 ${nacos.home}/logs/ 目录下, 使用 tail -f start.out 查看日志,如果看到如下日志,说明服务启动成功

Nacos started successfully in stand alone mode. use embedded storage

⑥ nacos 控制台页面为 http://{ip}:{port}/{context}/index.html ,例如

http://192.168.89.1:8849/nacos/index.html

初始管理员用户和密码都为 nacos

二、nacos 的安装(docker方式)

docker run --name nacos-standalone-derby \-e MODE=standalone \-e NACOS_AUTH_TOKEN=${your_nacos_auth_secret_token} \-e NACOS_AUTH_IDENTITY_KEY=${your_nacos_server_identity_key} \-e NACOS_AUTH_IDENTITY_VALUE=${your_nacos_server_identity_value} \-p 8080:8080 \-p 8848:8848 \-p 9848:9848 \-d nacos/nacos-server:latest

通过 docker logs -f $container_id 命令,查看 Nacos 服务启动日志,如果看到如下日志,说明服务启动成功

Nacos started successfully in xxxx mode. use xxxx storage

关于暴露出来的这 3 个端口,参考 https://nacos.io/blog/faq/nacos-user-question-history15151/

三、nacos 的使用

在 maven 项目中引入 nacos discovery 依赖

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

在 application.yaml 中配置 nacos 地址

server:port: 8081
spring:application:name: order-servicecloud:nacos:server-addr: 192.168.19.130:8848

在项目主类上添加 @EnableDiscoveryClient 注解

@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {// ... 其他代码 ...
}

最后,启动项目即可完成自动注册,在 nacos 控制台中可以看到对应的服务

四、发现并调用服务

/*** 购物车 服务实现类*/
@Service
@RequiredArgsConstructor
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {private final RestTemplate restTemplate;private final DiscoveryClient discoveryClient;// 查询购物车中的商品信息private void handleCartItems(List<CartVO> carts) {// 想查询详情的商品idSet<Long> itemIds = ...;// 获取商品服务列表List<ServiceInstance> instances = discoveryClient.getInstances("product-service");if(CollUtil.isEmpty(instances)) {return;}// 随机选择一个商品服务的实例ServiceInstance instance = instances.get(RandomUtil.randomInt(instances.size()));// 调用商品服务ResponseEntity<List<ItemDTO>> response = restTemplate.exchange(instance.getUri() + "/items?ids={ids}",HttpMethod.GET,null,new ParameterizedTypeReference<List<ItemDTO>>() { },Map.of("ids", CollUtil.join(itemIds,",")));if(!response.getStatusCode().is2xxSuccessful()) {// 查询失败,直接结束return;}List<ItemDTO> items = response.getBody();// ...后续逻辑}
}
http://www.jsqmd.com/news/29972/

相关文章:

  • ch2题解
  • Pymarchenko程序
  • 2025年11月3日。上课积极回答问题喜得三分
  • Tita 项目经营版:全方位赋能企业项目全流程管理
  • 软件需求分析课堂加分 - 2305
  • 互测赛(4)总结
  • 2025 年 11 月冷水机厂家推荐排行榜,螺杆冷水机,工业冷水机,水冷式冷水机,风冷式冷水机,螺杆式冷水机,小型冷水机公司推荐
  • #20232329易杨文轩 2025-2026-1 《网络与系统攻防技术》 实验四实验报告
  • 2025年11月黄褐斑改善产品评测榜:五款热门单品数据化对比推荐
  • 2025年11月黄褐斑改善产品推荐榜:五款热门单品深度对比与评价
  • 简易小说文件查看器
  • 2025年11月珠海酒店评价榜:商务休闲双需求住宿对比指南
  • 2025年建筑模型行业权威测评:中国建筑模型专业公司排名与深度解析
  • 前端chatbox
  • 2025年11月合肥建筑律师推荐榜:全过程覆盖能力对比评价
  • 空间计量模型,包括空间滞后模型、空间误差模型和空间杜宾模型
  • 2025 年塑料挤出设备厂家最新推荐榜单:深度甄选聚焦技术实力、客户认可及设备性能全方位测评塑料板材挤出设备/塑料挤出设备生产线/塑料管材挤出设备生产线公司推荐
  • AtCoder Beginner Contest 430 ABCDEF 题目解析
  • 哪个文件共享平台好用?11款主流工具深度对比
  • 2025年广东空运到印度专线机构权威推荐榜单:广东到印度的海运平台/广东空运到印度物流平台/广东到印度专线双清包税服务源头机构精选
  • 2025年规模大型纸箱厂家权威推荐榜单:化妆品纸箱/包装纸箱/瓦楞纸箱源头厂家精选
  • 2025 年最新推荐锰钢编织筛网厂家排行榜,涵盖耐磨矿用等多类型产品,精选五大靠谱品牌助力企业采购滚筒/平筛/黑钢锰钢编织筛网公司推荐
  • 用四端法测量Fe-Cr-Al丝的电阻率
  • 如何选择企业私有网盘?这9款内网文件共享工具优劣全解析
  • C# Web开发教程(十一)后台主动服务
  • 2025 年国际高中学校最新推荐榜,聚焦办学资质与升学成果深度解析及教学质量与综合实力全面评估松江区 / 青浦区 / 奉贤区 / 崇明区国际高中推荐
  • 2025年汽车超薄低音源头厂家权威推荐榜单:汽车音响超薄低音/汽车超薄低音炮/汽车音响超薄低音炮源头厂家精选
  • 2025年保洁服务公司新排行榜推荐,海獭顾家无忧保洁服务详细介绍
  • 基于开源操作系统搭建K8S高可用集群
  • 2025 年阳台光伏厂家推荐:昱电宝依托昱能科技技术积淀,打造场景化光伏解决方案与服务体系