package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerMovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieFeignApplication.class, args);
}
}
package com.itmuch.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.feign.UserFeignClient; @RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
}
package com.itmuch.cloud.feign;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import com.itmuch.cloud.entity.User; import feign.hystrix.FallbackFactory; @Component
public class HystrixClientFactory implements FallbackFactory<UserFeignClient> { private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFactory.class); @Override
public UserFeignClient create(Throwable cause) {
HystrixClientFactory.LOGGER.info("fallback; reason was: {}", cause.getMessage());
return new UserFeignClientWithFactory() {
@Override
public User findById(Long id) {
User user = new User();
user.setId(-1L);
return user;
}
};
}
}
package com.itmuch.cloud.feign;

import org.springframework.stereotype.Component;

import com.itmuch.cloud.entity.User;

@Component
public class HystrixClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
User user = new User();
user.setId(0L);
return user;
}
}
package com.itmuch.cloud.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.itmuch.cloud.entity.User; @FeignClient(name = "microservice-provider-user", /*fallback = HystrixClientFallback.class, */fallbackFactory = HystrixClientFactory.class)
public interface UserFeignClient {//请求microservice-provider-user的/simple/{id}方法。回调调用的是HystrixClientFactory的findById方法
@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
package com.itmuch.cloud.feign;

public interface UserFeignClientWithFactory extends UserFeignClient {

}
spring:
application:
name: microservice-consumer-movie-feign-with-hystrix-hystrix-factory
server:
port: 7901
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
<?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">
<modelVersion>4.0.0</modelVersion> <artifactId>microservice-consumer-movie-feign-with-hystrix-hystrix-factory</artifactId>
<packaging>jar</packaging> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>

springcloud10---feign-with-hystrix_factory的更多相关文章

  1. Spring Cloud 声明式服务调用 Feign

    一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...

  2. Feign使用Hystrix无效原因及解决方法

    最近项目重构使用了Spring Boot和Spring Cloud.这两者结合确实给项目带来了方便,同时也遇到了一些问题.其中使用feign作为服务消费,但是断路器hystrix一直不起作用让人很费解 ...

  3. 在dropwizard中使用feign,使用hystrix

    前言 用惯了spring全家桶之后,试试dropwizard的Hello World也别有一帆风味.为了增强对外访问API的能力,需要引入open feign.这里简单在dropwizard中使用fe ...

  4. spring cloud feign不支持@RequestBody+ RequestMethod.GET,报错

    1.问题梳理: 异常:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not ...

  5. 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign

    上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...

  6. Spring Cloud Feign 整合 Hystrix

    在前面随笔Spring Cloud 之 Feign的feign工程基础上进行改造 1.pom.xml依赖不变 2.application.yml文件添加feign.hystrix.enabled=tr ...

  7. Spring Cloud 之 Feign

    新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?& ...

  8. SpringCloud Feign对Hystrix(断路由)的支持

    第一步:首先开启Feign对Hystrix的支持,在properties文件中添加以下配置: feign.hystrix.enabled=true. 第二步:在上一篇Feign的基础上添加Hystri ...

  9. SpringCloud Feign使用详解

    添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  10. 工作随笔——自动重发的凶手--feign

    公司使用的feign(https://github.com/OpenFeign/feign)作为http客户端. 开发时debug后端程序,发现同一个请求会多次收到. 为了判断是谁在搞鬼,在客户端和服 ...

随机推荐

  1. 《C++ Primer Plus》14.2 私有继承 学习笔记

    C++(除了成员变量之外)还有另一种实现has-a关系的途径——私有继承.使用私有继承,基类的公有成员和保护成员都将成为派生类的私有成员.(如果使用保护继承,基类的公有成员和保护成员都将称为派生类的保 ...

  2. CALayer的position,anchorPoint属性 与UIView的frame 属性

    彻底理解CALayer的position,anchorPoint属性 与UIView的frame 属性 一.position,anchorPoint两者都是CALayer的属性,都是CGPoint点 ...

  3. 96、facebook Fresco框架库源使用(转载)

    各个属性详情:http://blog.csdn.net/y1scp/article/details/49245535 开源项目链接 facebook Fresco仓库:git clone https: ...

  4. 进程保护--CrossThreadFlags标志位

    原理: 1. 将进程的所有线程的线程CrossThreadFlags标志位设置成Terminated或者System. 效果:任务管理器,WSYSCheck,ICESWORD无法结束进程.. 但PCH ...

  5. LeetCode - Customers Who Never Order

    Description: Suppose that a website contains two tables, the Customers table and the Orders table. W ...

  6. 如何使用腾讯云开发一款 AR 应用介绍

    版权声明:本文由张亚舒原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/117 来源:腾云阁 https://www.qclo ...

  7. FZU 2082(过路费)

    题目链接:传送门 题目大意:中文题,略 题目思路:树链剖分(注意要把边上的权值转移到深度较大的点上来维护) 最后当top[x]==top[y]注意id[x]+1因为是维护的点而题目是边 如果不+可能会 ...

  8. luogu P1379 八数码难题(A*算法入门详细讲解)

     代码实现细节 #include<cstdio> #include<cstring> #include<iostream> using namespace std; ...

  9. 在linux下安装Avria(小红伞)

    1.下载AntiVir PersonalEdition Classic for linux http://www.free-av.com/ 2.解压: tar zxvf antivir.tar.gz ...

  10. ios 自定义NavgationBar的按钮

    UIImage *btnimage = [UIImage imageNamed:@"about.png"]; UIButton *btn = [[UIButton alloc] i ...