springcloud + netflix 微服务治理心得

本文介绍了SpringCloud作为微服务治理框架,如何整合Eureka服务注册与发现、Zuul服务网关以及Feign简化API调用和Hystrix熔断器的功能。并针对常见错误提供了处理方法,如版本兼容性问题和请求方法不匹配等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springcloud是什么?

springcloud是微服务治理框架,它整合了目前市面上运行稳定,效率高,社区活跃的技术栈,如netflix的服务注册与发现技术栈eureka,服务网关技术栈zuul等等,但是它的核心基础还是springboot

springcloud + springboot匹配表

springcloud核心基础是springboot因此运行时他们需要一对一匹配

参考网址:Spring Cloud

服务注册与发现Eureka

eureka-server即注册中心,属性文件配置项中注意这两项置为false

eureka.client.registerWithEureka = false

fetchRegistry = false

springboot启动主程序加上@EnableEurekaServer

eureka-client即服务提供者,属性文件配置项中注意这项指向eureka-server

eureka.client.serviceUrl.defaultZone = http://localhost:8086/eureka(eureka-server)

springboot启动主程序加上@EnableEurekaClient

服务网关Zuul

服务网关的主要作用就两点:一、服务转发;二、服务拦截或者也叫过滤

这种错误:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.springframework.boot.web.servlet.error.ErrorController.getErrorPath()Ljava/lang/String;

处理方法:
这种错误是netflix-zuul版本过低导致,由于netflix-zuul社区更新速度慢于springboot与springcloud,因此要降低springboot,springcloud版本。使用springboot 2.3.12.RELEASE,springcloud Hoxton.SR10 解决

Feign简化了Web API调用服务

web api之间的调用都可以通过注解方式(@FeignClient)进行处理,简化了web api的调用;

其次Feign融入了Ribbon负载均衡和Hystrix熔断器功能;

最后目前Pivotal团队建议使用openfeign替代feign;

Hystrix熔断器

Hystrix熔断器核心就是fallback,当调用的微服务出现故障时,返回对应fallback类方法的指定提示信息。从而不至于长时间等待微服务响应导致服务调用的阻塞,最终导致所有关联该微服务的所有调用瘫痪。在高频和高并发服务中显得尤为重要。

这种错误:

org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘POST’ not supported,405

 org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ not supported,405

处理方法:

上述错误情况,一般是熔断器这边@FeignClient接口类中方法声明的Request方法与微服务那边的定义的Request方法不一致导致,建议两边都统一使用RequestMapping,同时不要指定method方法类型即可。其次也可能是两边使用的SpringBoot Starter Parent版本不一致导致,但是没有测试过。

这种错误:

[Request processing failed; nested exception is feign.RetryableException: Connection refused: connect executing GET http://微服务名/微服务方法] with root cause

This application has no explicit mapping for /error, so you are seeing this as a fallback.

处理方法:

这种错误是netflix-hystrix版本过低导致,由于netflix-hystrix社区更新速度慢于springboot与springcloud,因此要降低springboot,springcloud版本。使用springboot 2.3.12.RELEASE,springcloud Hoxton.SR10 解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张沛洁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值