开发配置
1、导包
//所有的SpringCloudAlibaba 应用都需要导入相关依赖包
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<!--父依赖-->
<dependencyManagement>
<dependencies>
<!--Alibaba 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>
<!-- spring-cloud-dependencies -->
<!--根据需求决定是否需要原生SpringCloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringBoot dependency-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
项目开发之中至少要集成nacos的discovery包
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
需要配置服务的还需要额外集成config包
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2、开发
1、在项目启动类上添加注解【@EnableDiscoveryClient】
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2、编写配置文件
编写配置文件bootstrap.yml
server:
port: 36001
spring:
application:
name: nacos-discovery-provider
cloud:
nacos:
config:
file-extension: yaml
group: DEFAULT_GROUP
refresh-enabled: true
server-addr: 192.168.100.215:8848
discovery:
server-addr: 192.168.100.215:8848
group: DEFAULT_GROUP
password: nacos
username: nacos
profiles:
active: dev