当前位置: 首页 > news >正文

Spring Cloud 服务接入到 Apache ShenYu 网关完整案例

Spring Cloud快速开始

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

环境准备

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

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

启动顺序

  • 启动shenyu-admin
  • 启动shenyu-bootstrap
  • 启动注册中心,例如shenyu-examples下的eureka项目
  • 配置shenyu-examples-springcloud注册发现
shenyu:discovery:enable:truetype:eurekaserverList:${eureka.client.serviceUrl.defaultZone}registerPath:${spring.application.name}props:nacosNameSpace:ShenyuRegisterCenter

运行shenyu-examples-springcloud

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

下载 shenyu-examples-eureka 、shenyu-examples-springcloud .

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

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

成功启动会有如下日志:

2021-02-1014:03:51.301 INFO2860---[main]o.s.s.concurrent.ThreadPoolTaskExecutor:Initializing ExecutorService'applicationTaskExecutor'2021-02-1014:03:51.669 INFO2860---[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}2021-02-1014:03:51.676 INFO2860---[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}2021-02-1014:03:51.682 INFO2860---[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}2021-02-1014:03:51.688 INFO2860---[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}2021-02-1014:03:51.692 INFO2860---[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}2021-02-1014:03:52.806 WARN2860---[main]ockingLoadBalancerClientRibbonWarnLogger:You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon isinmaintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it,setthe value of`spring.cloud.loadbalancer.ribbon.enabled`to`false`or remove spring-cloud-starter-netflix-ribbon from your project.2021-02-1014:03:52.848 WARN2860---[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.2021-02-1014:03:52.921 INFO2860---[main]o.s.c.n.eureka.InstanceInfoFactory:Setting initial instance status as: STARTING2021-02-1014:03:52.949 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Initializing Eurekainregion us-east-12021-02-1014:03:53.006 INFO2860---[main]c.n.d.provider.DiscoveryJerseyProvider:Using JSON encoding codec LegacyJacksonJson2021-02-1014:03:53.006 INFO2860---[main]c.n.d.provider.DiscoveryJerseyProvider:Using JSON decoding codec LegacyJacksonJson2021-02-1014:03:53.110 INFO2860---[main]c.n.d.provider.DiscoveryJerseyProvider:Using XML encoding codec XStreamXml2021-02-1014:03:53.110 INFO2860---[main]c.n.d.provider.DiscoveryJerseyProvider:Using XML decoding codec XStreamXml2021-02-1014:03:53.263 INFO2860---[main]c.n.d.s.r.aws.ConfigClusterResolver:Resolving eureka endpoints via configuration2021-02-1014:03:53.546 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Disable delta property:false2021-02-1014:03:53.546 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Single vip registry refresh property:null2021-02-1014:03:53.547 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Force full registry fetch:false2021-02-1014:03:53.547 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Application is null:false2021-02-1014:03:53.547 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Registered Applications size is zero:true2021-02-1014:03:53.547 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Application version is -1:true2021-02-1014:03:53.547 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Getting all instance registry info from the eureka server2021-02-1014:03:53.754 INFO2860---[main]com.netflix.discovery.DiscoveryClient:The response status is2002021-02-1014:03:53.756 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Starting heartbeat executor: renew interval is:302021-02-1014:03:53.758 INFO2860---[main]c.n.discovery.InstanceInfoReplicator:InstanceInfoReplicator onDemand update allowed rate per min is42021-02-1014:03:53.761 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Discovery Client initialized at timestamp1612937033760with initial instances count:02021-02-1014:03:53.762 INFO2860---[main]o.s.c.n.e.s.EurekaServiceRegistry:Registering application SPRINGCLOUD-TEST with eureka with status UP2021-02-1014:03:53.763 INFO2860---[main]com.netflix.discovery.DiscoveryClient:Sawlocalstatus change event StatusChangeEvent[timestamp=1612937033763,current=UP,previous=STARTING]2021-02-1014:03:53.765 INFO2860---[nfoReplicator-0]com.netflix.discovery.DiscoveryClient:DiscoveryClient_SPRINGCLOUD-TEST/host.docker.internal:springCloud-test:8884: registering service...2021-02-1014:03:53.805 INFO2860---[main]o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat started on port(s):8884(http)with context path''2021-02-1014:03:53.807 INFO2860---[main].s.c.n.e.s.EurekaAutoServiceRegistration:Updating port to88842021-02-1014:03:53.837 INFO2860---[nfoReplicator-0]com.netflix.discovery.DiscoveryClient:DiscoveryClient_SPRINGCLOUD-TEST/host.docker.internal:springCloud-test:8884 - registration status:2042021-02-1014:03:54.231 INFO2860---[main]o.d.s.e.s.ShenyuTestSpringCloudApplication:Started ShenyuTestSpringCloudApplicationin6.338seconds(JVM runningfor7.361)
  • 启动shenyu-examples-springcloud之后
  • 在admin的系统的divide插件上即可看到刚注册上来的数据

测试Http请求

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

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

使用IDEA HTTP Client插件模拟http的方式来请求你的SpringCloud服务[本地访问,不使用shenyu代理]:

使用IDEA HTTP Client插件模拟http的方式来请求你的SpringCloud服务[使用shenyu代理]:


Spring Cloud快速开始 | Apache ShenYu

http://www.jsqmd.com/news/354793/

相关文章:

  • 解读2026年别墅门市场,全国高性价比厂商排名及特色分析 - 工业品网
  • 2026最新!8个AI论文工具测评:MBA毕业论文与科研写作必备指南
  • 大模型使用进阶指南:如何将个人经验转化为可复用Agent Skills
  • RAG技术深度剖析:让大模型从“瞎编“到“有据可依“的三大形态与应用指南
  • 从智障到智能:Agent Skills配置完全指南
  • 聊聊十二分装饰怎么样,2026装修效果和行业认可度全解析 - myqiye
  • 直接上结论:8个降AIGC工具测评,专科生降AI率必备攻略
  • 大模型驱动的智能体系统:架构设计与技术实现
  • 学霸同款 9个AI论文写作软件测评:本科生毕业论文+开题报告高效工具推荐
  • 《抗战文军》深度解读:烽火中的 “文军长征” 与求是精神的永恒传承
  • 公众号编辑器素材哪里有?新手常用的公众号排版工具网站推荐 - peipei33
  • 破解长周期Agent落地难题:双Agent架构+状态恢复+测试验证
  • 2026年辅助空压机制造商权威盘点:定制化解决方案引领轨道交通新风向 - 品牌策略师
  • Agent Skills 爆火!一文搞懂它与 MCP、Prompt 的区别
  • 哲思杂笔1
  • 2026年辅助空压机市场展望:专业定制与可靠生产厂家的选择指南 - 品牌策略师
  • AI Agent:2026年AI生态核心与开发实践指南
  • 2026年杭州软装公司公司最新推荐:杭州墙纸/杭州装修公司/杭州墙画品牌/杭州公装设计公司/杭州墙纸设计公司 - 品牌策略师
  • 公众号SVG动效揭秘:这些svg动效工具5大优势提升用户互动体验 - peipei33
  • 夕阳残影
  • Agent Skills标准:让AI Agent按需挂载能力,国内skill0平台已上线
  • 文思杂笔2——意写
  • AI 学习笔记:LLM 的部署与测试
  • AI Agent记忆系统全面解析:形态-功能-动力学三维框架详解
  • 专科生必看!千笔,倍受青睐的降AI率工具
  • 基于微信小程序的垃圾分类信息系统【源码文末联系】 - 实践
  • 2026年不锈钢工字钢厂商盘点:寻找靠谱的源头研发与生产工厂 - 品牌策略师
  • 2026年橱柜小拉手公司排行榜/家具五金拉手 - 品牌策略师
  • 摆脱论文困扰! 9个AI论文软件测评:专科生毕业论文+开题报告高效助手
  • 2026年杭州墙纸设计公司公司最新TOP实力排行:杭州软装公司/杭州装修公司/杭州墙画品牌/杭州公装设计公司/杭州墙布定制公司 - 品牌策略师