nacos服务注册
这篇文章介绍一下如何在nacos中注册服务,主要步骤有:一.启动nacos,二.搭建alibaba spring cloud脚手架。三.配置,服务注册。
一.启动nacos
以单机模式启动nacos:.\startup.cmd -m standalone
2.搭建alibaba spring cloud脚手架
访问https://start.aliyun.com/bootstrap.html,GroupID: com.alibaba.cloud,Artifact:nocos-discovery-provider-sample,选择依赖:Nacos Service Discovery,Spring Web,Cloud Bootstrap.
pom文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | <? xml version="1.0" encoding="UTF-8"?> < project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion >4.0.0</ modelVersion > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.6.11</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.alibaba.cloud</ groupId > < artifactId >nocos-discovery-provider-sample</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >nocos-discovery-provider-sample</ name > < description >Demo project for Spring Boot</ description > < properties > < java.version >1.8</ java.version > < spring-cloud-alibaba.version >2021.0.4.0</ spring-cloud-alibaba.version > < spring-cloud.version >2021.0.4</ spring-cloud.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >com.alibaba.cloud</ groupId > < artifactId >spring-cloud-starter-alibaba-nacos-discovery</ artifactId > </ dependency > < dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > </ dependencies > < dependencyManagement > < dependencies > < dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-dependencies</ artifactId > < version >${spring-cloud.version}</ version > < type >pom</ type > < scope >import</ scope > </ dependency > < dependency > < groupId >com.alibaba.cloud</ groupId > < artifactId >spring-cloud-alibaba-dependencies</ artifactId > < version >${spring-cloud-alibaba.version}</ version > < type >pom</ type > < scope >import</ scope > </ dependency > </ dependencies > </ dependencyManagement > < build > < plugins > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-compiler-plugin</ artifactId > < version >3.8.1</ version > < configuration > < source >1.8</ source > < target >1.8</ target > < encoding >UTF-8</ encoding > </ configuration > </ plugin > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
3.配置,服务注册
在application.properties中配置相应的nacos设置,配置文件如下:
spring.application.name=nocos-discovery-provider-sample
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.namespace=public
server.port=8080
启动类:
package com.alibaba.cloud.nocosdiscoveryprovidersample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class NocosDiscoveryProviderSampleApplication { public static void main(String[] args) { SpringApplication.run(NocosDiscoveryProviderSampleApplication.class, args); } }
注意加上标注@EnableDiscoveryClient
尝试写一个服务:
package com.alibaba.cloud.nocosdiscoveryprovidersample.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class ServiceController { @GetMapping("/echo/{message}") public String echo(@PathVariable String message) { return "[echo] :"+message; } }
启动,查看nacos后台,如果看到如下所示,则代表服务注册成功:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?