1.首先找台機安裝好nacos,確保有正常訪問 。
2.加入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>runtime</scope>
</dependency>
<!--nacos連接注冊中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!--nacos 配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3.在starter文件里增加注解
@EnableDiscoveryClient
增加配置文件 bootstrap.properties內容
server.port=18080 #設置服務名 spring.application.name=spring-cloud-two #設置注冊中心連接地址,多個用,號隔開 spring.cloud.nacos.discovery.server-addr=192.168.0.10:8848 #設置nacos配置中心連接地址 spring.cloud.nacos.config.server-addr=192.168.0.10:8848 spring.cloud.nacos.username=nacos spring.cloud.nacos.password=nacos #設置配置文件名的后綴,默認是.properties #spring.cloud.nacos.config.file-extension=yaml #設置所在的命名空間,默認會到public里,填寫命名空間ID #spring.cloud.nacos.config.namespace=wewe-wewe-12-wewe #設置所在的組 #spring.cloud.nacos.config.group=DEFAULT_GROUP #設置作用的文件,當出現a-test.yaml,a-dev.yaml時,定義為dev值則用后者文件 #spring.profiles.active=dev

