开发配置

1、导包

  1. //所有的SpringCloudAlibaba 应用都需要导入相关依赖包
  2. <properties>
  3. <java.version>1.8</java.version>
  4. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  5. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  6. <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
  7. <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
  8. </properties>
  9. <!--父依赖-->
  10. <dependencyManagement>
  11. <dependencies>
  12. <!--Alibaba dependency-->
  13. <dependency>
  14. <groupId>com.alibaba.cloud</groupId>
  15. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  16. <version>${spring-cloud-alibaba.version}</version>
  17. <type>pom</type>
  18. <scope>import</scope>
  19. </dependency>
  20. <!-- spring-cloud-dependencies -->
  21. <!--根据需求决定是否需要原生SpringCloud-->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-dependencies</artifactId>
  25. <version>Hoxton.SR3</version>
  26. <type>pom</type>
  27. <scope>import</scope>
  28. </dependency>
  29. <!--SpringBoot dependency-->
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-dependencies</artifactId>
  33. <version>${spring-boot.version}</version>
  34. <type>pom</type>
  35. <scope>import</scope>
  36. </dependency>
  37. </dependencies>
  38. </dependencyManagement>

image.png

项目开发之中至少要集成nacos的discovery包

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  4. </dependency>

需要配置服务的还需要额外集成config包

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  4. </dependency>

2、开发

1、在项目启动类上添加注解【@EnableDiscoveryClient】

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

2、编写配置文件

编写配置文件bootstrap.yml

  1. server:
  2. port: 36001
  3. spring:
  4. application:
  5. name: nacos-discovery-provider
  6. cloud:
  7. nacos:
  8. config:
  9. file-extension: yaml
  10. group: DEFAULT_GROUP
  11. refresh-enabled: true
  12. server-addr: 192.168.100.215:8848
  13. discovery:
  14. server-addr: 192.168.100.215:8848
  15. group: DEFAULT_GROUP
  16. password: nacos
  17. username: nacos
  18. profiles:
  19. active: dev