下载nacos 服务端jar
这边使用的是1.4.1 版本的服务包
下载地址为 https://github.com/alibaba/nacos/releases/tag/1.4.1
在windows 单机方式启动的 命令为 startup.cmd -m standalone 默认点击启动文件startup.cmd是集群方式启动的, 需要自己手动敲命令以单机的方式启动
启动的默认端口是8848 按默认的端口启动就可以了。
启动后, 访问本地的nacos http://localhost:8848/nacos 默认登录用户密码都是 nacos
进去后可以对其修改密码, 本地的密码默认应该就够了。
进去的界面如下
本地服务要是想注册进来则 注册到该地址就行
搭建个父工程 , springbootstudy ,建相应的moudle 工程 分别为 studyuser,studyorder
公用工程 studycommon,公用工程 放对应的公共类,公共的jar包
引入注册中心的配置,在 studycommon 的配置如下
<?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.jiuge</groupId> <artifactId>springbootstudy</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.jiuge.common</groupId> <artifactId>studycommon</artifactId> <version>0.0.1-SNAPSHOT</version> <name>studycommon</name> <description>common project for Spring Boot</description> <packaging>jar</packaging> <properties> <java.version>1.8</java.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
studyuser 子模块的工程配置如下
<?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.jiuge</groupId> <artifactId>springbootstudy</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.jiuge.user</groupId> <artifactId>studyuser</artifactId> <version>0.0.1-SNAPSHOT</version> <name>studyuser</name> <description>user project for Spring Boot</description> <packaging>jar</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 引入公用类包 --> <dependency> <groupId>com.jiuge.common</groupId> <artifactId>studycommon</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
studyuser的application.yml配置如下
server: port: 8010 spring: application: name: user-server cloud: nacos: discovery: server-addr: localhost:8848 namespace: 2a57e550-6295-4269-b1b4-268c46021020
springbootstudy 父工程的配置如下
<?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.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.jiuge</groupId> <artifactId>springbootstudy</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootstudy</name> <description>alibaba project for Spring Boot</description> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> <spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version> <dubbo.version>2.7.8</dubbo.version> </properties> <!-- 子模块工程 --> <modules> <module>studycommon</module> <module>studyuser</module> <module>studyorder</module> </modules> <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> </dependencies> <!-- maven 依赖管理 --> <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> <!-- 引入阿里云配置仓库 --> <repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>alimaven</id> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动 studyuser服务,启动日志如下
nacos管理界面能看到 studyuser注册 上来的实例,就说明成功了
至此, nacos 的服务注册就算成功了。