spring cloud入门,feign结合hystrix使用示例

在之前讲解了 spring cloud eureka和ribbon,现在开始讲解spring cloud中声明式调用方式feign的相关使用,关于eureka搭建参见 spring cloud eureka和ribbon
搭建consumer-feign,pom依赖如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-test-001</artifactId>
        <groupId>com.leo.test</groupId>
        <version>1.0.0-snapshot</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>service-conumer-feign-001</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
        </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

consumer-feign模块配置文件 application.yml如下:

spring:
  application:
    name:  service-consumer-feign-001
server:
  port:  9020
eureka:
  client:
    service-url:
      defaultZone: http://leo-node:8081/eureka
feign:
  hystrix:
    enabled: true

搭建启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServiceConsumerFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerFeignApplication.class);
    }
}

构造feign调用接口:

import com.leo.test.spring.cloud.test.service.consumer.feign.configuration.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name="service-provider-001",path="/provider/",configuration = FeignConfig.class,fallback = HystrixClient.class)
public interface ConsumerFeignClient {
    @GetMapping(value = "/sayHello")
    String sayHello();
}

可以看到相比于ribbon调用,ribbon需要手动拼接url来进行访问,而feign则通过声明与调用方一样的方法来进行调用,这样简化了请求的复杂度。
feign重试的配置:

import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
public class FeignConfig {
    @Bean
    public Retryer getFeignRetryer() {
        return new Retryer.Default(100, TimeUnit.SECONDS.toMillis(1L),5);
    }
}

并且在这里我们通过hystrix,指定了如果调用服务失败需要怎么处理,

import org.springframework.stereotype.Component;
@Component
public class HystrixClient implements ConsumerFeignClient{
    public String sayHello() {
        return "consumer feign error from hystrix";
    }
}

编写controller访问层:


import com.leo.test.spring.cloud.test.service.consumer.feign.client.ConsumerFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer/feign")
public class ConsumerController {
    @Autowired
    private ConsumerFeignClient feignClient;

    @RequestMapping("/sayHello")
    public String sayHello(){
        return feignClient.sayHello();
    }
}

.我们启动eureka-server和服务提供端,然后启动consumer-feign: 访问: http://localhost:9020/consumer/feign/sayHello,可以看到负载均衡分别调用,显示如下:
在这里插入图片描述

在这里插入图片描述
停止其中一个服务提供者,再次访问,显示如下:
在这里插入图片描述

在这里插入图片描述

到此feign结合hystrix示例完成

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值