Spring Cloud--Feign开启对Hystrix支持

前言

Github:https://github.com/yihonglei/thinking-in-springcloud

Eureka注册中心:eureka-server

服务提供者(订单服务):eureka-provider-order

Feign-api(服务接口抽象):eureka-feign-api

Feign客户端消费:eureka-consumer-feign-hystrix

一 Feign支持Hystrix

Feign是一个声明式的伪RPC的REST客户端,基于接口的注解方式,很方便客户端配置。

Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

Hystrix基于开源框架Netflix实现了Spring Cloud Hystrix,该框架的目标在于通过控制哪些访问远程系统、

服务等,从而对于网络延迟和故障提供更强大的容错能力。

二 启动eureka-server(注册中心)

执行eureka-server项目EurekaServerApplication类的main方法。

三 启动eureka-provider-order(服务提供者)

执行eureka-provider-order项目EurekaOrderApplication类的main方法。

扫描二维码关注公众号,回复: 10599074 查看本文章

四 eureka-feign-api(feign接口抽象,添加hystrix)

1、项目结构

2、pom.xml依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3、OrderApi(feign抽象order服务接口)

package com.lanhuigu.order;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * order服务,FeignClient标注服务提供者信息
 *
 * @auther: yihonglei
 * @date: 2019-06-30 21:17
 */
@FeignClient(name = "eureka-provider-order", fallback = OrderApiFallBack.class, path = "/order")
public interface OrderApi {

    @RequestMapping("/queryOrderInfo")
    String queryOrdersByUserId();

}

@FeignClient注解name指明要调用的服务,fallback指定降级类,path访问路径。 

4、OrderApiFallBack(hystrix接口降级实现)

package com.lanhuigu.order;

import org.springframework.stereotype.Component;

/**
 * Order订单,服务降级接口
 *
 * @auther: yihonglei
 * @date: 2019-07-06 21:51
 */
@Component
public class OrderApiFallBack implements OrderApi {
    @Override
    public String queryOrdersByUserId() {
        return "触发服务降级接口";
    }
}

服务降级类必须实现对应的接口,以及必须添加@Component注解。

当接口出现问题时,调用服务降级接口返回给调用者。 

5、打成jar包,供别的工程使用

五 eureka-consumer-feign-hystrix(feign开启hystrix)

1、项目结构

2、pom.xml依赖

<!-- Eureka Client -->
<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-netflix-hystrix</artifactId>
</dependency>

<!-- feign-api -->
<dependency>
    <groupId>com.lanhuigu</groupId>
    <artifactId>eureka-feign-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

3、application.yml(配置文件)

# 注册到eureka服务端的微服务名称
spring:
  application:
    name: eureka-consumer-feign-hystrix
# 服务提供端口
server:
  port: 8008
# 注册到eureka服务端的地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  # 显示指定微服务的名称,默认ip:应用名称:端口(192.168.1.7:eureka-consumer-feign-hystrix:8008)
  instance:
    instance-id: eureka-consumer-feign-hystrix-8008
    prefer-ip-address: true
# 开启feign支持hystrix,默认关闭
feign:
  hystrix:
    enabled: true

feign.hystrix.enabled开启feign支持hystrix,在Spring Cloud的E版本之后,默认是关闭的。 

4、UserController(控制层)

package com.lanhuigu.controller;

import com.lanhuigu.order.OrderApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController这个注解等价于spring mvc用法中的@Controller+@ResponseBody
 *
 * @auther: yihonglei
 * @date: 2019-06-17 22:30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private OrderApi orderApi;

    @RequestMapping(value = "/queryUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public String queryUserInfo() {
        String orderInfo = orderApi.queryOrdersByUserId();

        return orderInfo;
    }
}

通过OrderApi直接调用服务接口。 

5、EurekaConsumerFeignHystrixApplication(应用启动类)

package com.lanhuigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

/**
 * @SpringBootApplication 启动一个Spring Boot应用程序
 * @EnableDiscoveryClient 服务发现与注册,当应用启动时,将应用注册到配置的注册中心
 *
 * @auther: yihonglei
 * @date: 2019-06-19 11:27
 */
@EnableDiscoveryClient
@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients(basePackages = "com.lanhuigu")
public class EurekaConsumerFeignHystrixApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerFeignHystrixApplication.class, args);
    }
}

@EnableCircuitBreaker开启hystrix,@EnableFeignClients激活feign。

运行main方法,启动服务。

六 服务访问,熔断测试

1、注册中心

http://localhost:9000

注册中心可以看到eureka-provider-order-8001服务和eureka-consumer-feign-hystrix。

2、访问服务

http://localhost:8008/user/queryUserInfo

3、断掉order服务,再次访问

当order服务断掉后,服务接口访问不同,自动访问降级接口,返回预期结果,从而实现熔断服务降级。

发布了502 篇原创文章 · 获赞 358 · 访问量 118万+

猜你喜欢

转载自blog.csdn.net/yhl_jxy/article/details/95005817