Nacos官网:https://nacos.io/zh-cn/index.html。
1、注册中心
1.1、项目搭建
创建新项目,项目结构如下:
父项目下有两个子项目nacos-9001、nacos-9002。
1、POM依赖
父项目POM文件:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
子项目POM文件:
<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> </dependencies>
2、配置文件application.yml
nacos-9001、nacos-9002 配置文件基本一样,只不过端口与服务名称不一致。
2.1、nacos-9001
# 服务端口 server: port: 9001 spring: # 应用名称 application: name: nacos-9001 # 注册中心地址 cloud: nacos: server-addr: 192.168.33.55:8848
2.2、nacos-9002
# 服务端口 server: port: 9001 spring: # 应用名称 application: name: nacos-9001 # 注册中心地址 cloud: nacos: server-addr: 192.168.33.55:8848
3、启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication // 开启注册功能 @EnableDiscoveryClient public class Nacos9001Application { public static void main(String[] args) { SpringApplication.run(Nacos9001Application.class, args); } }
nacos-9002的启动类与nacos-9001的启动类似。
1.2、注册中心
启动nacos-9001、nacos-9002,访问注册中心的服务列表,详情如下:nacos-9001、nacos-9002,已经注册到Nacos上了。
标签:应用,spring,boot,Nacos,笔记,nacos,springframework,9001,org From: https://www.cnblogs.com/RunningSnails/p/17319907.html