1、下载
nacos官网:https://nacos.io/zh-cn/index.html
下载地址:https://github.com/alibaba/nacos/releases
选择对应版本:2.1.0
https://github.com/alibaba/nacos/releases/tag/2.1.0
2、Windows环境配置
进入文件目录
Nacos默认为集群模式(cluster),修改startup.cmd中模式为单机模式(standalone)
成功启动
访问地址,默认账号及密码:nacos
3、项目中集成nacos
(1)在父项目下新建nacos子项目“service-nacos”
(2)如果子项目未引入,手动引入。File->Project Structure
(3)修改pom文件的groupId、artifactId,引入nacos依赖
<?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>com.shiwn</groupId> <artifactId>spring-cloud-alibaba</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.shiwn</groupId> <artifactId>service-nacos</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-nacos</name> <description>nacos服务</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- nacos服务注册与发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
(4)修改配置文件,配置nacos注册中心地址
# 应用服务 WEB 访问端口 server.port=8020 # 应用名称(nacos会将应用名称当作服务名称) spring.application.name=service-nacos # nacos认证信息 spring.cloud.nacos.discovery.username=nacos spring.cloud.nacos.discovery.password=nacos # nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口 spring.cloud.nacos.discovery.server-addr=192.168.1.206:8848 # 注册到 nacos 的指定 namespace,默认为 public spring.cloud.nacos.discovery.namespace=public
(5)订单服务接口
/** * 订单服务 */ @RestController @RequestMapping("/order") public class OrderController { /** * 下单 * @return */ @RequestMapping("/add") public String addOrder(){ System.out.println("下单成功"); return "订单服务"; } }
(6)查看nacos注册服务
4、集群部署配置
https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
标签:spring,boot,Nacos,nacos,Cloud,Spring,com,cloud,discovery From: https://www.cnblogs.com/shiblog/p/16620351.html