首先是去nacos官网下载最新的包:Nacos 快速开始 | Nacos
win下启动命令:startup.cmd -m standalone
这样就可以访问你的nacos 了。
添加一个配置,记住你的 DataId,和Group名字。
创建一个pom项目,引入springCloud
<?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>
<artifactId>obwt-cloud</artifactId>
<groupId>com.obwt</groupId>
<version>0.0.1-SNAPSHOT</version>
<!--用来存放子项目 在子项目为创建的时候可以先注释-->
<modules>
<module>bms-cloud</module>
<!-- <module>tms-cloud</module>-->
<!-- <module>wms-cloud</module>-->
<!-- <module>oms-cloud</module>-->
</modules>
<packaging>pom</packaging>
<!--统一对jar包版本进行管理
子模块继承之后,提供作用: 锁定版本+子modlue不用写groupId和version -->
<properties>
<!-- 指定SpringBoot版本 -->
<spring-boot.version>2.5.3</spring-boot.version>
<!-- 指定SpringCloud版本 -->
<spring-cloud.version>2020.0.3</spring-cloud.version>
<!-- 指定SpringCloudAlibaba版本 -->
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<alibaba.nacos.version>2.0.3</alibaba.nacos.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.18.14</lombok.version>
<mysql.version>5.1.41</mysql.version>
<druid.version>1.2.6</druid.version>
<mybatis.spring.boot.version>2.2.0</mybatis.spring.boot.version>
<mybaits.plus.version>3.3.2</mybaits.plus.version>
<alibaba.json.version>1.2.75</alibaba.json.version>
<!--指定jdk版本-->
<java.version>1.8</java.version>
<spring-cloud-starter-bootstrap.version>3.0.2</spring-cloud-starter-bootstrap.version>
</properties>
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<!-- bootstrap 启动器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>${spring-cloud-starter-bootstrap.version}</version>
</dependency>
<!--mybatis plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybaits.plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>${mybaits.plus.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>${mybaits.plus.version}</version>
<scope>compile</scope>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${alibaba.json.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
更新依赖后,创建一个子模块。一般就是springboot项目
这里可以看下是否可以跑起来。正常你配个web,和数据库地址就应该可以起来。并可以正常访问你的控制器接口。
然后复制这个配置到nacos那个配置。
修改本地配置指向nacos
server:
port: 8001
spring:
application:
# 服务名称
name: bms-cloud
profiles:
active: dev
cloud:
nacos:
#discovery 服务注册
discovery:
server-addr: 192.168.1.166:8848
#分组
group: DEFAULT_GROUP
# 注册到nacos上的服务名称,默认{${spring.application.name}
service: ${spring.application.name}
#config 读取nacos上的服务配置
config:
file-extension: yaml
#分组
group: DEFAULT_GROUP
server-addr: 192.168.1.166:8848
再次启动就可以看到可以从nacos上获取配置文件了。