Spring Cloud Alibaba Nacos

Posted 狂乱的贵公子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Alibaba Nacos相关的知识,希望对你有一定的参考价值。

1. Spring Cloud Alibaba 介绍

Spring Cloud Alibaba 为分布式应用程序开发提供了一站式解决方案。它包含了开发分布式应用程序所需的所有组件,使得你可以轻松地使用Spring Cloud开发应用程序。

使用Spring Cloud Alibaba,只需要添加一些注释和少量配置即可将Spring Cloud应用程序连接到Alibaba的分布式解决方案,并使用Alibaba中间件构建分布式应用程序系统。

特性:

  • 流量控制和服务降级:Sentinel进行流量控制,断路和系统自适应保护。
  • 服务注册和发现:实例可以在Nachos中注册,并且客户端可以使用Spring管理的Bean发现实例。支持Ribbon。
  • 分布式配置:使用Nacos作为数据存储。
  • 事件驱动:建立与Spring Cloud Stream RocketMQ Binder连接的高度可扩展的事件驱动型微服务。
  • 消息总线:利用Spring Cloud Bus RocketMQ链接分布式系统的节点。
  • 分布式事务:支持高性能、易于使用的分布式事务。
  • Dubbo RPC :通过Dubbo RPC扩展Spring Cloud服务之间调用的通信协议。
  • 阿里云对象存储:阿里云对象存储服务(OSS)是一种加密、安全、经济高效且易于使用的对象存储服务,可让您在云中存储,备份和存档大量数据。

依赖管理:

 1 <dependencyManagement>
 2     <dependencies>
 3         <dependency>
 4             <groupId>com.alibaba.cloud</groupId>
 5             <artifactId>spring-cloud-alibaba-dependencies</artifactId>
 6             <version>2.1.0.RELEASE</version>
 7             <type>pom</type>
 8             <scope>import</scope>
 9         </dependency>
10     </dependencies>
11 </dependencyManagement>

2. Spring Cloud Alibaba Nacos Discovery

Nacos是一个易于使用的动态服务发现、配置和服务管理平台。

服务注册与发现:服务发现是微服务体系架构中的关键组件之一。在这样的体系结构中,为每个客户端手动配置服务列表可能是不太现实的,并且使动态扩展极为困难。Nacos Discovery帮助您自动将服务注册到Nacos Server,并且Nacos Server会跟踪服务并动态刷新服务列表。 

 <dependency>
     <groupId>com.alibaba.cloud</groupId>
     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 </dependency>

Nacos Discovery集成了Netflix Ribbon 、RestTemplate或OpenFeign,可用于服务之间的调用 

2.1. Start a Provider Application

首先,创建一个提供者服务

 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.1.11.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.cjs.example</groupId>
12     <artifactId>nacos-discovery-provider-example</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>nacos-discovery-provider-example</name>
15 
16     <properties>
17         <java.version>1.8</java.version>
18         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
20     </properties>
21 
22     <dependencyManagement>
23         <dependencies>
24             <dependency>
25                 <groupId>org.springframework.cloud</groupId>
26                 <artifactId>spring-cloud-dependencies</artifactId>
27                 <version>Greenwich.SR4</version>
28                 <type>pom</type>
29                 <scope>import</scope>
30             </dependency>
31             <dependency>
32                 <groupId>com.alibaba.cloud</groupId>
33                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
34                 <version>2.1.0.RELEASE</version>
35                 <type>pom</type>
36                 <scope>import</scope>
37             </dependency>
38         </dependencies>
39     </dependencyManagement>
40 
41     <dependencies>
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-actuator</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-starter-web</artifactId>
49         </dependency>
50         <dependency>
51             <groupId>com.alibaba.cloud</groupId>
52             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
53         </dependency>
54     </dependencies>
55 
56     <build>
57         <plugins>
58             <plugin>
59                 <groupId>org.springframework.boot</groupId>
60                 <artifactId>spring-boot-maven-plugin</artifactId>
61             </plugin>
62         </plugins>
63     </build>
64 
65 </project>

application.properties

 server.port=8081
 spring.application.name=nacos-provider
 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
 management.endpoints.web.exposure.include=*

如果您不想使用Nacos进行服务注册和发现,可以设置spring.cloud.nacos.discovery为false

 1 package com.cjs.example.nacos;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 6 import org.springframework.web.bind.annotation.GetMapping;
 7 import org.springframework.web.bind.annotation.PathVariable;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 @SpringBootApplication
11 @EnableDiscoveryClient
12 public class NacosDiscoveryProviderExampleApplication {
13 
14     public static void main(String[] args) {
15         SpringApplication.run(NacosDiscoveryProviderExampleApplication.class, args);
16     }
17 
18     @RestController
19     public class EchoController {
20         @GetMapping(value = "/echo/{string}")
21         public String echo(@PathVariable String string) {
22             return "Hello Nacos Discovery " + string;
23         }
24     }
25 
26 } 

注意,务必先启动Nacos

wget https://github.com/alibaba/nacos/releases/download/1.1.4/nacos-server-1.1.4.tar.gz
tar -zxf nacos-server-1.1.4.tar.gz
cd nacos/bin
sh startup.sh -m standalone
sh shutdown.sh

2.2. Start a Consumer Application

创建一个消费者服务

 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.1.11.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.cjs.example</groupId>
12     <artifactId>nacos-discovery-consumer-example</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>nacos-discovery-consumer-example</name>
15 
16     <properties>
17         <java.version>1.8</java.version>
18         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
20     </properties>
21 
22     <dependencyManagement>
23         <dependencies>
24             <dependency>
25                 <groupId>org.springframework.cloud</groupId>
26                 <artifactId>spring-cloud-dependencies</artifactId>
27                 <version>Greenwich.SR4</version>
28                 <type>pom</type>
29                 <scope>import</scope>
30             </dependency>
31             <dependency>
32                 <groupId>com.alibaba.cloud</groupId>
33                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
34                 <version>2.1.0.RELEASE</version>
35                 <type>pom</type>
36                 <scope>import</scope>
37             </dependency>
38         </dependencies>
39     </dependencyManagement>
40 
41     <dependencies>
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-actuator</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-starter-web</artifactId>
49         </dependency>
50         <dependency>
51             <groupId>com.alibaba.cloud</groupId>
52             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
53         </dependency>
54         <dependency>
55             <groupId>org.springframework.cloud</groupId>
56             <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
57         </dependency>
58         <dependency>
59             <groupId>org.springframework.cloud</groupId>
60             <artifactId>spring-cloud-starter-openfeign</artifactId>
61         </dependency>
62     </dependencies>
63 
64     <build>
65         <plugins>
66             <plugin>
67                 <groupId>org.springframework.boot</groupId>
68                 <artifactId>spring-boot-maven-plugin</artifactId>
69             </plugin>
70         </plugins>
71     </build>
72 
73 </project> 

application.properties 

 server.port=8082
 spring.application.name=nacos-consumer
 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
 management.endpoints.web.exposure.include=* 

使用RestTemplate或者FeignClient调用远程服务

 1 package com.cjs.example.nacos;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.boot.SpringApplication;
 6 import org.springframework.boot.autoconfigure.SpringBootApplication;
 7 import org.springframework.cloud.client.ServiceInstance;
 8 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 9 import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
10 import org.springframework.cloud.openfeign.EnableFeignClients;
11 import org.springframework.cloud.openfeign.FeignClient;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.web.bind.annotation.GetMapping;
14 import org.springframework.web.bind.annotation.PathVariable;
15 import org.springframework.web.bind.annotation.RestController;
16 import org.springframework.web.client.RestTemplate;
17 
18 @SpringBootApplication
19 @EnableDiscoveryClient
20 @EnableFeignClients
21 public class NacosDiscoveryConsumerExampleApplication {
22 
23     public static void main(String[] args) {
24         SpringApplication.run(NacosDiscoveryConsumerExampleApplication.class, args);
25     }
26 
27 
28     @RestController
29     public class NacosController{
30 
31         @Autowired
32         private LoadBalancerClient loadBalancerClient;
33         @Autowired
34         private RestTemplate restTemplate;
35 
36         @Value("${spring.application.name}")
37         private String appName;
38 
39         /**
40          * 使用RestTemplate调用远程服务
41          */
42         @GetMapping("/echo/app-name")
43         public String echoAppName(){
44             // Access through the combination of LoadBalanceClient and RestTemplate
45             ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
46             String path = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName);
47             System.out.println("request path:" +path);
48             return restTemplate.getForObject(path,String.class);
49         }
50 
51         // Instantiate RestTemplate Instance
52         @Bean
53         public RestTemplate restTemplate(){
54             return new RestTemplate();
55         }
56 
57 
58         // ----------------------------------------
59 
60         @Autowired
61         private EchoService echoService;
62以上是关于Spring Cloud Alibaba Nacos的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud - Spring Cloud Alibaba 之 Nacos Config配置中心

Nacos Config 配置中心攻略和详解

spring cloud 微服务无法注册到nacos

springcloud~gateway网关

springcloud复习3

配置中心(Nacos)