Spring Cloud Alibaba 极简入门
Author : Flashpig
引言
Spring Cloud Alibaba简介:https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/README-zh.md
包含三个组件:
Sentinel:把流量作为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Nacos:一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
AliCloud OSS: 阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量、安全、低成本、高可靠的云存储服务。您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。
本次入门,只使用Sentinel 和 Nacos。将Spring Cloud的服务注册到Nacos;使用Sentinel限流;配置信息放在Nacos中。
一、工具
既然是Spring Boot应用,当然是使用Spring Tools最合适了。地址:https://spring.io/tools
我使用的版本是Spring Tools 4 for Eclipse
JDK:8
Nacos:nacos-server-0.4.0,地址:https://github.com/alibaba/nacos/releases
Sentinel:sentinel-dashboard-1.3.0,地址:https://github.com/alibaba/Sentinel/releases
二、Spring Boot应用作基础
为了简单、节省时间,我们把服务的提供者和调用者放在同一个项目里。使用Spring Tools建立一个Spring Starter项目,SpringBoot版本:2.0.6,我之前用2.1.0版本实验,没成功。不确定是我的配置有问题,还是Alibaba没兼容SpringBoot 2.1.0版本。所以暂时用2.0.6去实验。
项目结构如下:
以下是pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>win.sc30</groupId>
<artifactId>cloud-platform</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>cloud-platform</name>
<description>Cloud platform on spring cloud alibaba</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<nacos.version>0.4.0</nacos.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、项目详细介绍
- 项目启动类:CloudPlatformApplication
- 配置中心验证接口:ConfigController
- 服务提供者(Provider):ProviderController
- 服务消费者(Consumer):ConsumerController
- 配置文件:bootstrap.properties
配置文件bootstrap.properties内容如下:
spring.application.name=example
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.sentinel.transport.dashboard=localhost:8858
management.endpoints.web.exposure.include=*
其中:application.name=example,设置了应用的名称,在注册服务的时候,会使用“example”这个名字;nacos-config这行配置,指定了配置中心的地址;nacos.discovery这行配置,指定了注册中心的地址。因为Nacos身兼配置和注册两个职位,所以两行配置的地址是一样的;sentinel.transport.dashboard这行配置,指定了sentinel-board的地址,Sentinel Client会自动从sentinel-dashboard中获取各种流控、降级规则。
CloudPlatformApplication.java内容如下:
package win.sc30.platform;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class CloudPlatformApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(CloudPlatformApplication.class, args);
}
}
ConfigController.java内容如下:
package win.sc30.platform.controller.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${useLocalCache:false}")
private boolean useLocalCache;
@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
}
ProviderController.java内容如下:
package win.sc30.platform.controller.provider;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
@RestController
public class ProviderController {
@SentinelResource("service.echo")
@RequestMapping(value = "/service/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
ConsumerController.java内容如下:
package win.sc30.platform.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerController {
private final RestTemplate restTemplate;
@Autowired
public ConsumerController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@RequestMapping(value = "/api/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://example/service/echo/" + str, String.class);
}
}
全部的代码就这些,没有复杂的东西,就不介绍了。
四、启动脚本
Nacos:下载Nacos Server后,解压缩到本地,进入bin目录,运行 sh startup.sh -m standalone,windows下使用startup.cmd脚本。默认端口是 8848
Sentinel:下载的Sentinel Dashboard是个jar包,直接用java运行,命令如下: java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.3.0.jar,我们指定服务端口为8858
CloudPlatformApplication:直接在Spring Tools中运行,Run As Application
五、测试效果
- 测试配置中心:
访问URL http://localhost:8080/config/get, 默认会返回false,在配置中心增加配置example.properties,内容如下
Data ID: example.properties
Group: DEFAULT_GROUP
配置格式: properties
配置内容:useLocalCache=true
然后点击“发布”
这时再次访问http://localhost:8080/config/get,会返回true,表明应用已经从配置中心获取了最新的配置信息。
- 测试服务注册
在Spring Tools中运行 CloudPlatformApplication,在程序启动完毕,访问:http://localhost:8848/nacos/index.html,打开Nacos的管理界面,在“服务管理-》服务列表”页面,可以看到名为“example”的实例。实例数量:1,健康实例数:1,表示服务已经正确注册,可以被调用了。
- 测试服务发现
访问:http://localhost:8080/api/echo/2018,返回结果为“Hello Nacos Discovery 2018”,表示服务可以正常调用。
- 测试限流
访问:http://localhost:8858,打开Sentinel-Dashboard管理界面,会看到example的菜单项,里面有实时监控、流量规则等子菜单。如果没有example菜单项,多刷新几下http://localhost:8080/api/echo/2018这个地址,再刷新Sentinel-Dashboard界面,应该就会出现了。我们接下来测试限流,点开“流控规则”,点击“新增流控规则“,增加一条规则,内容如下:
资源名:service.echo
流控应用:default
阈值类型:QPS
单机阈值:2
其他默认,然后点击新增,这样就增加了一条规则。然后再次访问http://localhost:8080/api/echo/2018这个地址,为了达到限流的效果,我们快速刷新,速度超过1秒2次,这是会出现错误页面,提示:(type=Internal Server Error, status=500),表示访问服务的速度太快,被Sentinel给拦截了。
至此,基本的实验全部结束,我们可以看到阿里开源的这两个组件,使用非常方便,附带的管理界面也很方便、直观。只是默认还没有身份认证和权限控制,这就需要我们自己去改造了。下一步,我们会把Nacos和Sentinel整合,让Sentinel-Dashboard把规则信息存入Nacos中,作持久化保存,应用程序从Nacos中获取流控规则信息。
感谢阅读,下篇再见!