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 解决