SpringCloud 相关

Spring Cloud框架包括如下功能:

分布式多版本配置管理
服务注册与发现
路由
微服务调用
负载均衡
断路器
分布式消息

Spring Cloud Context:应用程序上下文服务

https://betheme.net/news/txtlist_i78535v.html

EnableDiscoveryClient

官方建议使用EnableDiscoveryClient。区别:@EnableDiscoveryClient注解是基于spring-cloud-commons依赖,并且在classpath中实现。 @EnableEurekaClient注解是基于spring-cloud-netflix依赖,只能为eureka作用。 @EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。

从Spring Cloud 1.0.0.RC1版本开始,就已经不推荐使用EnableEurekaClient和EnableHystrix了。服务注册发现功能被抽象后放入spring-cloud-commons库,该库的EnableDiscoveryClient可以取代旧的EnableEurekaClient,使用注解EnableDiscoveryClient就能启用服务注册发现功能。 同理,EnableHystrix也被EnableCircuitBreaker取代了。

Hystrix

处理分布式系统的延迟和容错的开源库

Enablecircuitbreaker is deprecated

Springboot 版本 2.5.12 不推荐使用 @EnableCircuitBreaker 并建议更换,利用@EnableHystrix注解代替。

Feign

Feign发送的是Get请求,到了提供者这边却变成了Post,报405错误
https://www.cnblogs.com/jingyu-zhang/p/13953441.html

feign常用两种降级方式Fallback和FallbackFactory。
https://blog.csdn.net/weixin_42771651/article/details/121626431

SpringCloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon而是使用spring-cloud-loadbalancer,所以不引入spring-cloud-loadbalancer会报错。


SpringCloud结合 Dubbo 和 Feign 在子模块公共模块引用问题

【解决方案】SpringCloud 结合 Dubbo 时,子模块分离模式下,全部使用Hystrix熔断,不去结合使用Feign的降级。

【分析问题】
在SpringCloud结合Dubbo,API服务 和 Server服务全部用Hystrix熔断机制,或者只在Server端TCP消息处理时使用Hystrix熔断。在API不使用降级(一般小系统)。降低Feign代码入侵,代码好维护。

如果是Service 和 ServiceImpl模块分离模式:
Dubbo只需要暴漏Service实现接口,在启动类只需要开启@EnableDiscoveryClient;多个子服务去实现ServiceImpl方法。在API服务和Backend后台服务同时调用一个指令,向Server端发送TCP指令可以这样使用。

main

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class ApiApplication {

	public static void main(String[] args){
		SpringApplication.run(ApiApplication.class, args);
		System.out.println("api cloud consumer run ok.");
	}
}

API 控制器 CommonController.java

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@DefaultProperties(defaultFallback="ApiFallBack") //声明一个全局降级逻辑方法
@RestController
public class CommonController {

    @Autowired
    private DubboApiClient dubboApiClient;

    /**
     * Server 熔断(超时)
     */
    @GetMapping("/sendCommandTimeout")
    public String sendCommandTimeout(Integer id){
        System.out.println("api, hystrix server timeout:" + id);//只在Server端配置熔断
        return dubboApiClient.sendCommandTimeout(id);
    }


    /** Dubbo 向Server发送字符串测试TCP指令
     */
    @GetMapping("/sendCommand")
    public String sendLockCommand(String cmd){
        return dubboApiClient.sendCommand(cmd);
    }
    
    public String ApiFallBack(Integer id){
        return "失败了";
    }
}
@Component
public class DubboApiClient {

    @DubboReference(check = false)
    private CommonService commonService;
    
    public String sendCommand(String cmd){
        return commonService.sendLockCommand(cmd);
    }
}

取消 Feign的使用

Feign代码侵入性高,最好不要单独暴漏接口,否则FeignServiceImpl 找不到子实现类,fallbackFactory无法指定降级配置类,导致必须再引入pom整个子模块。

使用 Feign 还需要在Service集成 GetMapping,感觉代码侵入性高。所以最好不使用公共模块引入方式。直接将ServiceImpl和Service放在SpringBoot API服务内,直接实现Http调用。

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
 * fallbackFactory 指定降级配置类,并使用了Hystrix的服务降级
 * 
 */
@FeignClient(value = "hystrix-provider", fallbackFactory = FeignServiceFallBack.class)
public interface FeignService {
    @GetMapping("/order/detail/{id}")
    public String findOrderDetailById(int id);
}

其他

Hystrix服务熔断(服务端)与服务降级(客户端)
https://blog.51cto.com/zhangzhixi/3175654

HystrixProperty配置汇总
https://blog.csdn.net/qq_43509535/article/details/113799835

什么是微服务
https://blog.csdn.net/wuxiaobingandbob/article/details/78642020

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值