当前位置:   article > 正文

Spring Cloud Alibaba 极简入门_springcloud alibaba极简入门

springcloud alibaba极简入门

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>win.sc30</groupId>
  7. <artifactId>cloud-platform</artifactId>
  8. <version>1.0</version>
  9. <packaging>jar</packaging>
  10. <name>cloud-platform</name>
  11. <description>Cloud platform on spring cloud alibaba</description>
  12. <parent>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-parent</artifactId>
  15. <version>2.0.6.RELEASE</version>
  16. <relativePath /> <!-- lookup parent from repository -->
  17. </parent>
  18. <properties>
  19. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  21. <java.version>1.8</java.version>
  22. <nacos.version>0.4.0</nacos.version>
  23. </properties>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-actuator</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>com.alibaba.csp</groupId>
  51. <artifactId>sentinel-datasource-nacos</artifactId>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-starter-test</artifactId>
  56. <scope>test</scope>
  57. </dependency>
  58. </dependencies>
  59. <dependencyManagement>
  60. <dependencies>
  61. <dependency>
  62. <groupId>org.springframework.cloud</groupId>
  63. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  64. <version>0.2.0.RELEASE</version>
  65. <type>pom</type>
  66. <scope>import</scope>
  67. </dependency>
  68. </dependencies>
  69. </dependencyManagement>
  70. <build>
  71. <plugins>
  72. <plugin>
  73. <groupId>org.springframework.boot</groupId>
  74. <artifactId>spring-boot-maven-plugin</artifactId>
  75. </plugin>
  76. </plugins>
  77. </build>
  78. </project>

三、项目详细介绍

  1. 项目启动类:CloudPlatformApplication
  2. 配置中心验证接口:ConfigController
  3. 服务提供者(Provider):ProviderController
  4. 服务消费者(Consumer):ConsumerController
  5. 配置文件:bootstrap.properties

配置文件bootstrap.properties内容如下:

  1. spring.application.name=example
  2. spring.cloud.nacos.config.server-addr=127.0.0.1:8848
  3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
  4. spring.cloud.sentinel.transport.dashboard=localhost:8858
  5. 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内容如下:

  1. package win.sc30.platform;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.client.RestTemplate;
  8. @SpringBootApplication
  9. @EnableDiscoveryClient
  10. public class CloudPlatformApplication {
  11. @LoadBalanced
  12. @Bean
  13. public RestTemplate restTemplate() {
  14. return new RestTemplate();
  15. }
  16. public static void main(String[] args) {
  17. SpringApplication.run(CloudPlatformApplication.class, args);
  18. }
  19. }

ConfigController.java内容如下:

  1. package win.sc30.platform.controller.config;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.cloud.context.config.annotation.RefreshScope;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. @RequestMapping("/config")
  8. @RefreshScope
  9. public class ConfigController {
  10. @Value("${useLocalCache:false}")
  11. private boolean useLocalCache;
  12. @RequestMapping("/get")
  13. public boolean get() {
  14. return useLocalCache;
  15. }
  16. }

ProviderController.java内容如下:

  1. package win.sc30.platform.controller.provider;
  2. import org.springframework.web.bind.annotation.PathVariable;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.alibaba.csp.sentinel.annotation.SentinelResource;
  7. @RestController
  8. public class ProviderController {
  9. @SentinelResource("service.echo")
  10. @RequestMapping(value = "/service/echo/{string}", method = RequestMethod.GET)
  11. public String echo(@PathVariable String string) {
  12. return "Hello Nacos Discovery " + string;
  13. }
  14. }

ConsumerController.java内容如下:

  1. package win.sc30.platform.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.client.RestTemplate;
  8. @RestController
  9. public class ConsumerController {
  10. private final RestTemplate restTemplate;
  11. @Autowired
  12. public ConsumerController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
  13. @RequestMapping(value = "/api/echo/{str}", method = RequestMethod.GET)
  14. public String echo(@PathVariable String str) {
  15. return restTemplate.getForObject("http://example/service/echo/" + str, String.class);
  16. }
  17. }

全部的代码就这些,没有复杂的东西,就不介绍了。

 

四、启动脚本

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中获取流控规则信息。

感谢阅读,下篇再见!

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/986991
推荐阅读
相关标签
  

闽ICP备14008679号