微服务学习笔记七 Spring Cloud Feign负载均衡及服务熔断

Feign:
与Ribbon一样,Feign也是由Netflix提供的,Feign是一个声明式、
模块化的Web Service客户端,它简化了开发者编写Web客户端的操作,
开发者可以通过简单的接口和注解来调用HTTP API ,Spring Cloud Feign,
它整合了Ribbon和Hystrix,具有可插拔、基于注解、负载均衡、服务熔断
等一系列便捷功能。
相比较于Ribbon+RestTemplate的方式,Feign大大简化了代码的开发,
Feign支持多种注解,包括Feign注解、JAX-RS注解、Spring MVC注解等,
Spring Cloud 对Feign进行了优化,整合了Ribbon和Eureka,从而让Feign
的使用更加方便。
Ribbon和Feign的区别:
Ribbon是一个通用的HTTP客户端工具,Feign是基于Ribbon实现的。
Feign的特点:
1)Feign是一个声明式的Web Service客户端。
2)支持Feign注解、SpringMVC注解、JAX-RS注解。
3)Feign基于Ribbon实现,使用起来更加简单。
4)Feign集成了Hystrix,具备服务熔断的功能。
在这里插入图片描述
创建Module,pom.xml添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

创建配置文件,application.yml

server:
  port: 8050
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

创建启动类

package com.shuang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class,args);
    }
}

创建声明式接口

package com.shuang.feign;

import com.shuang.entity.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collection;

@FeignClient(value = "provider")
public interface FeignProviderClient {
    @GetMapping("/student/findAll")
    public Collection<Student> findAll();

    @GetMapping("/student/index")
    public String index();
}

Handler

package com.shuang.controller;

import com.shuang.entity.Student;
import com.shuang.feign.FeignProviderClient;
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;

import java.util.Collection;

@RestController
@RequestMapping("/feign")
public class FeignHandler {
    @Autowired
    private FeignProviderClient feignProviderClient;

    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return feignProviderClient.findAll();
    }

    @GetMapping("/index")
    public String index(){
        return feignProviderClient.index();
    }
}

服务熔断,application.yml中添加熔断机制

server:
  port: 8050
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true

feign.hystrix.enable:是否开启熔断器。
创建 FeignProviderClient 接口的实现类FeignError,定义容错处理逻辑,通过@Component
注解将FeignError实例注入IOC容器中。

package com.shuang.feign.impl;

import com.shuang.entity.Student;
import com.shuang.feign.FeignProviderClient;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class FeignError implements FeignProviderClient {
    @Override
    public Collection<Student> findAll() {
        return null;
    }

    @Override
    public String index() {
        return "服务器维护中。。。。";
    }
}

在FeignProviderClient定义处通过@FeignClient的fallback属性设置映射。

package com.shuang.feign;

import com.shuang.entity.Student;
import com.shuang.feign.impl.FeignError;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collection;

@FeignClient(value = "provider", fallback = FeignError.class)
public interface FeignProviderClient {
    @GetMapping("/student/findAll")
    public Collection<Student> findAll();

    @GetMapping("/student/index")
    public String index();
}

依次启动:注册中心、Feign

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

希境

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

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

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

打赏作者

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

抵扣说明:

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

余额充值