我是靠谱客的博主 激情绿草,这篇文章主要介绍Spring Cloud学习笔记9-推荐!Feign使用Hystrix容错(官人挂了金莲怎么办),现在分享给大家,希望可以做个参考。

开发套路放在前:
a 加依赖

b 加注解

c 写配置


Feign默认已经整合了Hystrix

所以只要有Feign依赖即可,就要需要修改配置启用即可:

feign:
  hystrix:
    enabled: true

依赖:注意,去掉了Hystrix的依赖

<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>
	<groupId>sam.zeng</groupId>
	<artifactId>springCloud-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- 引入spring boot的依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.7.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<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-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
	</dependencies>
	<!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<!-- 添加spring-boot的maven插件,不能少,打jar包时得用 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

controller层:很干净的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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import sam.zeng.entity.User;
import sam.zeng.service.UserService;

@RequestMapping("/users")
@RestController
public class UserController {
	@Autowired
	private UserService userService;

	@GetMapping("/{id}")
	public User findById(@PathVariable Long id) {
		return userService.findById(id);
	}

}

service层:只加了一个@FeignClient注解,低侵入

请注意@FeignClient(name ="providerService",fallback = UserServiceImpl.class),此处指明了,如果调用失败了去找哪个类处理

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import sam.zeng.entity.User;
import sam.zeng.service.impl.UserServiceImpl;

@FeignClient(name ="providerService",fallback = UserServiceImpl.class)
public interface UserService {
	@GetMapping("/users/{id}")
	User findById(@PathVariable("id") Long id);
}
import org.springframework.stereotype.Service;

import sam.zeng.entity.User;
import sam.zeng.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Override
	public User findById(Long id) {
		return new User(id, "默认用户", "默认用户", 0);
	}

}

启动类:加@EnableFeignClients用来启用Feign,低侵入

@SpringBootApplication
@EnableFeignClients
public class ConsumerApp {

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

}

测试:

先正常启用Eureka和provider,测试如下:

关闭服务提供者测试如下:


另:如何获得造成fallback的原因?

修改service层如下就好了:

fallback = UserServiceImpl.class改为了fallbackFactory =UserServiceFallbackFactory.class

注意:如果fallback 和fallbackFactory 同时存在的话,只有fallback会生效(本人测试的就是酱子的)

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import sam.zeng.entity.User;
import sam.zeng.service.fallbackFactory.UserServiceFallbackFactory;

@FeignClient(name ="providerService",fallbackFactory =UserServiceFallbackFactory.class )
public interface UserService {
	@GetMapping("/users/{id}")
	User findById(@PathVariable("id") Long id);
}
import org.springframework.stereotype.Component;

import feign.hystrix.FallbackFactory;
import sam.zeng.entity.User;
import sam.zeng.service.UserService;

@Component
public class UserServiceFallbackFactory implements FallbackFactory<UserService> {

	@Override
	public UserService create(Throwable cause) {
		cause.printStackTrace();
		return new UserService() {
			
			@Override
			public User findById(Long id) {
				return new User(id, "默认用户", "默认用户", 0);
			}
		};
	}

}

总结:

因为Feign默认已经整合了Hystrix,所以,强烈推荐使用此方法容错

少写一行代码少导一个包还是很幸福的。。。。。。

:)

最后

以上就是激情绿草最近收集整理的关于Spring Cloud学习笔记9-推荐!Feign使用Hystrix容错(官人挂了金莲怎么办)的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(169)

评论列表共有 0 条评论

立即
投稿
返回
顶部