欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud OpenFeign 熔断

程序员文章站 2022-04-27 10:29:02
SpringCloud OpenFeign 熔断实现OpenFeign 消费者OpenFeign maven配置 org.springframework.cloud spring-cloud-starter-openfeign

SpringCloud OpenFeign 熔断实现

OpenFeign 消费者 OpenFeign 默认支持负载均衡

OpenFeign maven配置

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>

OpenFeign 接口

/**
 *Feign是声明性Web服务客户端。它使编写Web服务客户端更加容易。要使用Feign,
 * 请创建一个接口并对其进行注释。
 * 它具有可插入注释支持,包括Feign注释和JAX-RS注释。
 * Feign还支持可插拔编码器和解码器。
 * Spring Cloud添加了对Spring MVC注释的支持,并
 * 支持使用HttpMessageConvertersSpring Web中默认使用的注释。
 * Spring Cloud集成了Ribbon和Eureka以及Spring Cloud LoadBalancer,
 * 以在使用Feign时提供负载平衡的http客户端。
 * https://docs.spring.io/spring-cloud-openfeign/docs/2.2.5.RELEASE/reference/html/
 */
@FeignClient(value="nacos-priovider",fallback=OpenFeignServiceImpl.class )
@Component
public interface OpenFeignService{
	/**
     * 使用@GetMapping时候成产者和消费者方法必须保持一致
     */
    @GetMapping(value = "/hello", headers={"Content-Type=application/json;charset=UTF-8"})
    public String hello();
}

OpenFeign 接口实现

@Component
public class OpenFeignServiceImpl implements OpenFeignService {
    @Override
    public String hello() {
    	/**
         * 添加业务处理
         */
        return " 网络异常,服务暂时无法访问。 !!!";
    }
}

OpenFeign application.yml

feign:
  hystrix:
    enabled: true//默认关闭

OpenFeign FeignController

@RestController
public class FeignController {
    @Resource
    private OpenFeignService openFeignService;

    @GetMapping(value = "/hello")
    public String Dms(Long id){
        System.out.println("====openfeign=="+id);
        String paymentById = openFeignService.hello();
        return paymentById ;
    }
}

OpenFeign 启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class NacosConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

OpenFeign 成产者和注册中心这里就不说了

参考:SpringCloud-Nacos 下载安装 https://blog.csdn.net/Lihshan/article/details/111224826

启动多个生产者

1、使用Maven clean、package
2、找到项目 target 目录执行命令指定端口号启动
java -jar provider-0.0.1-SNAPSHOT.jar --server.port=8001

本文地址:https://blog.csdn.net/Lihshan/article/details/111928960