准备工作

创建父工程

Spring Cloud Alibaba 的环境在父工程中创建,微服务的各个组件作为子工程,继承父工程的环境。

其中pom修改打包方式为pom,完整pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.2.RELEASE</version>
  9. </parent>
  10. <groupId>com.godfrey</groupId>
  11. <artifactId>spring-cloud-alibaba</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>spring-cloud-alibaba</name>
  14. <description>spring-cloud-alibaba for Spring Boot</description>
  15. <!--打包方式-->
  16. <packaging>pom</packaging>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <spring.cloud.alibaba.version>2.2.3.RELEASE</spring.cloud.alibaba.version>
  20. <spring.cloud.version>Hoxton.SR8</spring.cloud.version>
  21. </properties>
  22. <dependencyManagement>
  23. <dependencies>
  24. <!--Spring Cloud Alibaba-->
  25. <dependency>
  26. <groupId>com.alibaba.cloud</groupId>
  27. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  28. <version>${spring.cloud.alibaba.version}</version>
  29. <type>pom</type>
  30. <scope>import</scope>
  31. </dependency>
  32. <!--Spring Cloud Hoxton-->
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>${spring.cloud.version}</version>
  37. <type>pom</type>
  38. <scope>import</scope>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

1、Nacos 服务注册

去到Nacos github下载Nacos,https://github.com/alibaba/nacos

解压,通过cmdstartup.cmd -m standalone启动单机(非集群)服务或者修改startup.cmd set MODE="cluster"=》set MODE="standalone"。启动后访问http://localhost:8848/nacos 账号、密码皆为nacos

Nacos 搭建成功,接下来注册服务。

在父工程路径下创建provide子模块,,让子工程继承父工程的环境依赖,pom.xml 中添加 nacos 组件。

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba.cloud</groupId>
  8. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  9. </dependency>
  10. </dependencies>

application.yml 中配置

  1. spring:
  2. application:
  3. name: provider
  4. cloud:
  5. nacos:
  6. discovery:
  7. # 指定nacos server地址
  8. server-addr: localhost:8848

启动类ProviderApplication

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

注意:在Spring Cloud官方文档中,从Edgware开始,已经不再强制在启动类上添加@EnableDiscoveryClient注解了,如果不用Nacos作为服务注册的组件,可以添加autoRegister = false在@EnableDiscoveryClient中Spring Cloud Alibaba 02:Nacos服务治理 - 图1Spring Cloud Alibaba 02:Nacos服务治理 - 图2

Spring Cloud Alibaba 02:Nacos服务治理 - 图3

2、Nacos 服务发现与调用

2.1 服务发现

在父工程路径下创建consumer子模块,,让子工程继承父工程的环境依赖,pom.xml 中添加 nacos 发现组件。

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba.cloud</groupId>
  8. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  9. </dependency>
  10. </dependencies>

application.yml中配置

  1. server:
  2. port: 9090

启动类ConsumerApplication

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

通过 discoveryClient 发现注册到 nacos 中的 provider 服务。

  1. @RestController
  2. public class ConsumerController {
  3. private final DiscoveryClient discoveryClient;
  4. @Autowired
  5. ConsumerController(DiscoveryClient discoveryClient){
  6. this.discoveryClient = discoveryClient;
  7. }
  8. @GetMapping("/instances")
  9. public List<ServiceInstance> getInstances() {
  10. return discoveryClient.getInstances("provider");
  11. }
  12. }

1、启动Nacos

2、设置允许启动多个provider服务,通过修改端口号(8080~8082)启动三个provider服务

3、启动consumer服务

Spring Cloud Alibaba 02:Nacos服务治理 - 图4

Spring Cloud Alibaba 02:Nacos服务治理 - 图5

2.2 服务调用:

consumer通过RestTemplate调用provider提供的服务

先去provider controller向外提供调用方法

  1. @RestController
  2. public class ProviderController {
  3. @Value("${server.port}")
  4. private String port;
  5. @GetMapping("/index")
  6. public String index() {
  7. return this.port;
  8. }
  9. }

去consumer模块配置RestTemplate的Bean

  1. @Configuration
  2. public class ConsumerConfig {
  3. @Bean
  4. public RestTemplate getRestTemplate() {
  5. return new RestTemplate();
  6. }
  7. }

controller添加index方法

  1. @RestController
  2. public class ConsumerController {
  3. private final DiscoveryClient discoveryClient;
  4. private final RestTemplate restTemplate;
  5. @Autowired
  6. public ConsumerController(DiscoveryClient discoveryClient, RestTemplate restTemplate) {
  7. this.discoveryClient = discoveryClient;
  8. this.restTemplate = restTemplate;
  9. }
  10. @GetMapping("/instances")
  11. public List<ServiceInstance> getInstances() {
  12. return discoveryClient.getInstances("provider");
  13. }
  14. @GetMapping("/index")
  15. public String index() {
  16. List<ServiceInstance> instances = discoveryClient.getInstances("provider");
  17. //随机访问服务
  18. int index = ThreadLocalRandom.current().nextInt(instances.size());
  19. String url = instances.get(index).getUri() + "/index";
  20. return "consumer随机远程调用provier:" + this.restTemplate.getForObject(url, String.class);
  21. }
  22. }

1、启动Nacos

2、设置允许启动多个provider服务,通过修改端口号(8080~8082)启动三个provider服务

3、启动consumer服务

Spring Cloud Alibaba 02:Nacos服务治理 - 图6

Spring Cloud Alibaba 02:Nacos服务治理 - 图7