Spring Cloud快速开始

本文档演示如何将Spring Cloud服务接入到Apache ShenYu网关。您可以直接在工程下找到本文档的示例代码

环境准备

请参考运维部署的内容,选择一种方式启动shenyu-admin。比如,通过 本地部署 启动Apache ShenYu后台管理系统。

启动成功后,需要在基础配置->插件管理中,把springCloud 插件设置为开启。

Spring Cloud快速开始 - 图1

启动网关,如果是通过源码的方式,直接运行shenyu-bootstrap中的ShenyuBootstrapApplication

注意,在启动前,请确保网关已经引入相关依赖。

引入网关对Spring Cloud的代理插件,并添加相关注册中心依赖:

  1. <!-- apache shenyu springCloud plugin start-->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-spring-boot-starter-plugin-springcloud</artifactId>
  5. <version>${project.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-commons</artifactId>
  10. <version>2.2.0.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  15. <version>2.2.0.RELEASE</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.shenyu</groupId>
  19. <artifactId>shenyu-spring-boot-starter-plugin-httpclient</artifactId>
  20. <version>${project.version}</version>
  21. </dependency>
  22. <!-- springCloud if you config register center is eureka please dependency end-->
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  26. <version>2.2.0.RELEASE</version>
  27. </dependency>
  28. <!-- apache shenyu springCloud plugin end-->

eureka配置信息如下:

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. instance:
  6. prefer-ip-address: true

启动shenyu-bootstrap项目。

运行shenyu-examples-springcloud

示例项目中我们使用 eureka 作为 Spring Cloud的注册中心。你可以使用本地的eureka,也可以使用示例中提供的应用。

下载 shenyu-examples-eurekashenyu-examples-springcloud .

启动eureka服务,运行org.apache.shenyu.examples.eureka.EurekaServerApplicationmain方法启动项目。

启动spring cloud服务,运行org.apache.shenyu.examples.springcloud.ShenyuTestSpringCloudApplicationmain方法启动项目。

成功启动会有如下日志:

  1. 2021-02-10 14:03:51.301 INFO 2860 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  2. 2021-02-10 14:03:51.669 INFO 2860 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : springCloud client register success: {"appName":"springCloud-test","context":"/springcloud","path":"/springcloud/order/save","pathDesc":"","rpcType":"springCloud","ruleName":"/springcloud/order/save","enabled":true}
  3. 2021-02-10 14:03:51.676 INFO 2860 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : springCloud client register success: {"appName":"springCloud-test","context":"/springcloud","path":"/springcloud/order/path/**","pathDesc":"","rpcType":"springCloud","ruleName":"/springcloud/order/path/**","enabled":true}
  4. 2021-02-10 14:03:51.682 INFO 2860 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : springCloud client register success: {"appName":"springCloud-test","context":"/springcloud","path":"/springcloud/order/findById","pathDesc":"","rpcType":"springCloud","ruleName":"/springcloud/order/findById","enabled":true}
  5. 2021-02-10 14:03:51.688 INFO 2860 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : springCloud client register success: {"appName":"springCloud-test","context":"/springcloud","path":"/springcloud/order/path/**/name","pathDesc":"","rpcType":"springCloud","ruleName":"/springcloud/order/path/**/name","enabled":true}
  6. 2021-02-10 14:03:51.692 INFO 2860 --- [pool-1-thread-1] o.d.s.client.common.utils.RegisterUtils : springCloud client register success: {"appName":"springCloud-test","context":"/springcloud","path":"/springcloud/test/**","pathDesc":"","rpcType":"springCloud","ruleName":"/springcloud/test/**","enabled":true}
  7. 2021-02-10 14:03:52.806 WARN 2860 --- [ main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
  8. 2021-02-10 14:03:52.848 WARN 2860 --- [ main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with default default cache. You can switch to using Caffeine cache, by adding it to the classpath.
  9. 2021-02-10 14:03:52.921 INFO 2860 --- [ main] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
  10. 2021-02-10 14:03:52.949 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
  11. 2021-02-10 14:03:53.006 INFO 2860 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
  12. 2021-02-10 14:03:53.006 INFO 2860 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
  13. 2021-02-10 14:03:53.110 INFO 2860 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
  14. 2021-02-10 14:03:53.110 INFO 2860 --- [ main] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
  15. 2021-02-10 14:03:53.263 INFO 2860 --- [ main] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
  16. 2021-02-10 14:03:53.546 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Disable delta property : false
  17. 2021-02-10 14:03:53.546 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
  18. 2021-02-10 14:03:53.547 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
  19. 2021-02-10 14:03:53.547 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Application is null : false
  20. 2021-02-10 14:03:53.547 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
  21. 2021-02-10 14:03:53.547 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Application version is -1: true
  22. 2021-02-10 14:03:53.547 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
  23. 2021-02-10 14:03:53.754 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
  24. 2021-02-10 14:03:53.756 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
  25. 2021-02-10 14:03:53.758 INFO 2860 --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
  26. 2021-02-10 14:03:53.761 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1612937033760 with initial instances count: 0
  27. 2021-02-10 14:03:53.762 INFO 2860 --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application SPRINGCLOUD-TEST with eureka with status UP
  28. 2021-02-10 14:03:53.763 INFO 2860 --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1612937033763, current=UP, previous=STARTING]
  29. 2021-02-10 14:03:53.765 INFO 2860 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SPRINGCLOUD-TEST/host.docker.internal:springCloud-test:8884: registering service...
  30. 2021-02-10 14:03:53.805 INFO 2860 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8884 (http) with context path ''
  31. 2021-02-10 14:03:53.807 INFO 2860 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8884
  32. 2021-02-10 14:03:53.837 INFO 2860 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SPRINGCLOUD-TEST/host.docker.internal:springCloud-test:8884 - registration status: 204
  33. 2021-02-10 14:03:54.231 INFO 2860 --- [ main] o.d.s.e.s.ShenyuTestSpringCloudApplication : Started ShenyuTestSpringCloudApplication in 6.338 seconds (JVM running for 7.361)

测试Http请求

shenyu-examples-springcloud项目成功启动之后会自动把加 @ShenyuSpringCloudClient 注解的接口方法注册到网关。

打开插件列表 -> rpc proxy -> springCloud 可以看到插件规则配置列表:

Spring Cloud快速开始 - 图2

下面使用postman模拟http的方式来请求你的SpringCloud服务:

Spring Cloud快速开始 - 图3