Spring Cloud-hystrix Feign(九)
前面使用ribbon拦截RestTemplate 实现服务的负载均衡 使用Hystrix进行熔断降级请求缓存 用原生的方式 将会有大量的模板代码,feigin就是rabbon和Histrix的整合 同
使用feign只需要通过接口对服务方的绑定 实现多处调用
使用例子
1.引入Pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--开启端点 用于dashboard监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.定义一个接口描述服务信息
@FeignClient("PROVIDER")
@RequestMapping("/user")
public interface UserService { @RequestMapping(value = "/findById",method = RequestMethod.GET)
public User findById(@RequestParam("id") Integer id);
@RequestMapping(value = "/findAll",method = RequestMethod.GET)
public List<User> findAll(); @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
public Boolean deleteById(@RequestParam("id")Integer id); @RequestMapping(value = "/update",method = RequestMethod.POST)
public Boolean update(@RequestBody User user);
}
@FeignClient 是用于服务发现 发现指定服务
@RequestMapping 为请求指定服务 指定方式
@RequestBody 表示参数是封装在报文体出传输 服务端 接收也需要打RequestBody
3.入口类开启Fegin以及服务发现
@SpringBootApplication
@EnableFeignClients //开启feigin
@EnableDiscoveryClient //开启服务发现
public class SpringCloudFeignConsumerApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignConsumerApplication.class, args);
} }
4.通过注入实现调用
@RestController
public class UserContorller {
@Autowired
UserService userService;
@RequestMapping("/findById")
@ResponseBody
public User findById(Integer id) {
return userService.findById(id);
} @RequestMapping("/findAll")
@ResponseBody
public List<User> findAll() {
return userService.findAll();
} @RequestMapping("/deleteById")
@ResponseBody
public Boolean deleteById(Integer id) {
return userService.deleteById(1);
} @RequestMapping("/update")
@ResponseBody
public Boolean update() {
User user=new User();
user.setId(1);
user.setName("测试修改");
User filterUser = userService.findById(user.getId());
filterUser.setName(user.getName());
userService.update(filterUser);
return true;
}
}
继承特性
将接口定义在服务端 服务消费者通过继承方式实现服务订阅。避免 每个服务消费者 都要重复定义接口描述
1.新增一个API项目并被服务端和客户端依赖 同时依赖web 因为需要使用springMVC注解对接口描述
3.pom依赖
<!--定义接口约束 需要用到springMVC的注解-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
<!--用普通jar打包的方式-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
jar包打包方式 都改为传统的打包方式 否则 其他依赖会报错 找不到 符号 因为存在2个入口类
3.api项目添加一个接口描述
@RequestMapping("/user")
public interface UserService { @RequestMapping(value = "/findById",method = RequestMethod.GET)
public User findById(@RequestParam("id") Integer id);
@RequestMapping(value = "/findAll",method = RequestMethod.GET)
public List<User> findAll(); @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
public Boolean deleteById(@RequestParam("id")Integer id); @RequestMapping(value = "/update",method = RequestMethod.POST)
public Boolean update(@RequestBody User user);
}
4.更改provider
@Controller
@RequestMapping("/user")
public class UserContorller implements UserService {
List<User> users; public UserContorller() {
users = new ArrayList<>();
users.add(new User(1, "小明", 1));
users.add(new User(2, "小香", 0));
users.add(new User(3, "小方", 0));
users.add(new User(4, "小张", 0));
users.add(new User(6, "小李", 0)); } @RequestMapping("/findById")
@ResponseBody
public User findById(Integer id) {
return users.stream().filter(c -> c.getId().intValue() == id.intValue()).findAny().get();
} @RequestMapping("/findAll")
@ResponseBody
public List<User> findAll() {
return users;
} @RequestMapping("/deleteById")
@ResponseBody
public Boolean deleteById(Integer id) {
return users.removeIf(c -> c.getId().intValue() == id.intValue());
} @RequestMapping("/update")
@ResponseBody
public Boolean update(@RequestBody User user) {
User filterUser = findById(user.getId());
filterUser.setName(user.getName());
return true;
}
}
5.更改consumer Service继承api的Service
@FeignClient("PROVIDER")
public interface UserService extends com.liqiang.api.UserService {
}
6.consumer调用注入Service
@RestController
public class UserContorller {
@Autowired
UserService userService;
@RequestMapping("/findById")
@ResponseBody
public User findById(Integer id) {
return userService.findById(id);
} @RequestMapping("/findAll")
@ResponseBody
public List<User> findAll() {
return userService.findAll();
} @RequestMapping("/deleteById")
@ResponseBody
public Boolean deleteById(Integer id) {
return userService.deleteById(1);
} @RequestMapping("/update")
@ResponseBody
public Boolean update() {
User user=new User();
user.setId(1);
user.setName("测试修改");
User filterUser = userService.findById(user.getId());
filterUser.setName(user.getName());
userService.update(filterUser);
return true;
}
}
继承的缺点 接口修改 会导致调用端的构建失败 不同通过版本控制可以避免
Ribbon配置
全局配置
通过ribbon.<属性>=..
ribbon:
ConnectTimeout: 500
ReadTimeout=5000
指定服务配置及重试
<服务名>.ribbon.<属性>=... 服务名为@FeginClient配置
#对指定服务设置rabbion全局参数 Fegin接口@FeignClient("PROVIDER")
PROVIDER:
ribbon:
#配置首台服务器重试1次
MaxAutoRetries: 1
#配置其他服务器重试两次
MaxAutoRetriesNextServer: 2
#链接超时时间
ConnectTimeout: 500
#请求处理时间
ReadTimeout: 2000
#每个操作都开启重试机制
OkToRetryOnAllOperations: true
hystrix配置
全局配置
与hystrix配置一样
#配置断路器超时时间,默认是1000(1秒)
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 12000
通过指令配置
hystrix.command.<commandKey> commandKey默认以方法名作为标识
禁用hystrix
全部禁用
feign:
hystrix:
enabled: true #启用hystrix false为禁用
指定服务禁用
@Configuration
public class DisableHystrixConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder(); }
}
@FeignClient(value = "PROVIDER",configuration = DisableHystrixConfiguration.class)
public interface UserService extends com.liqiang.api.UserService {
}
服务降级
1.增加一个Service的接口类
public class UserServiceFaillBack implements UserService {
@Override
public User findById(Integer id) {
return null;
} @Override
public List<User> findAll() {
return null;
} @Override
public Boolean deleteById(Integer id) {
return false;
} @Override
public Boolean update(User user) {
return false;
}
}
@FeignClient(value = "PROVIDER",configuration = DisableHystrixConfiguration.class,fallback = UserServiceFaillBack.class)
public interface UserService extends com.liqiang.api.UserService {
}
请求压缩
feign:
compression:
request:
enabled: true #feign请求gizp压缩 减少网络损耗
response:
enabled: true #feign响应gizp压缩 减少网络损耗
#启用压缩 压缩指定格式 超过对应大小的才开始压缩
#feign.compression.request.enabled=true
#feign.compression.request.mime-types=text/xml,application/xml,application/json #指定格式
#feign.compression.request.min-request-size=2048 #指定大小
日志配置
对服务开启日志输出
#还需要在application配置Logger.Level 详情区主类去看
logging:
level:
com:
liqiang:
feginService:
HelloExtendService: DEBUG
还要在主类设置日志界别
class SpringcloudFeiginConsumerApplication { /***
* 日志可选级别
* • NONE: 不记录任何信息。
* • BASIC: 仅记录请求方法、URL以及响应状态码和执行时间。
* • HEADERS: 除了记录BASIC级别的信息之外, 还会记录请求和响应的头信息。
// * • FULL: 记录所有请求与响应的明细, 包括头信息、 请求体、 元数据等。
// * @return
*/
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudFeiginConsumerApplication.class, args);
} }
可能遇到的问题
如果遇到feign注解无效,同时配置了scan 包扫描。 可以在@EnableFeignClients("你的feign包")或者application.properties 配置feign.client.package=com.crb.ocms.stock
Spring Cloud-hystrix Feign(九)的更多相关文章
- spring cloud: Hystrix(四):feign类似于hystrix的断容器功能:fallback
spring cloud: Hystrix(四):feign使用hystrix @FeignClient支持回退的概念:fallback方法,这里有点类似于:@HystrixCommand(fallb ...
- Spring Cloud 微服务笔记(六)Spring Cloud Hystrix
Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...
- spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix
spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix 首先application.yml / applicatoin.propreties的配置项:fe ...
- spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略
spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...
- 微服务架构之spring cloud hystrix&hystrix dashboard
在前面介绍spring cloud feign中我们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的作用,不会让发生的异常无 ...
- spring cloud 使用feign 遇到问题
spring cloud 使用feign 项目的搭建 在这里就不写了,本文主要讲解在使用过程中遇到的问题以及解决办法 1:示例 @RequestMapping(value = "/gener ...
- Spring Cloud 微服务四:熔断器Spring cloud hystrix
前言:在微服务架构中,一般都是进程间通信,有可能调用链都比较长,当有底层某服务出现问题时,比如宕机,会导致调用方的服务失败,这样就会发生一连串的反映,造成系统资源被阻塞,最终可能造成雪崩.在sprin ...
- Spring Cloud Hystrix理解与实践(一):搭建简单监控集群
前言 在分布式架构中,所谓的断路器模式是指当某个服务发生故障之后,通过断路器的故障监控,向调用方返回一个错误响应,这样就不会使得线程因调用故障服务被长时间占用不释放,避免故障的继续蔓延.Spring ...
- 7、Spring Cloud Hystrix
1.Spring Cloud Hystrix简介 (1).分布式问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败. 多个微服务之间调用的时候,假设微服务A调 ...
- Spring Cloud 整合 Feign 的原理
前言 在 上篇 介绍了 Feign 的核心实现原理,在文末也提到了会再介绍其和 Spring Cloud 的整合原理,Spring 具有很强的扩展性,会把一些常用的解决方案通过 starter 的方式 ...
随机推荐
- Delphi7中的函数与过程(Function and Procedure)
1.锁住空间的位置,可以选择Edit--->Lock component ,也可以在窗体设计面板下面找到组件排版功能栏,第二排里面有个带锁的图标,表示组件可以被锁住.点击一下,组件的大小和位置就 ...
- linux下dd命令详解【转】
本文转载自:http://www.cnblogs.com/licheng/articles/1116492.html 名称: dd 使用权限: 所有使用者dd 这个指令在 manual 里的定义是 ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- hdu 1213(并查集模版题)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- LeetCode.5-最长回文子串(Longest Palindromic Substring)
这是悦乐书的第342次更新,第366篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第3题(顺位题号是5).给定一个字符串s,找到s中最长的回文子字符串. 您可以假设s ...
- 应用JavaScript搭建一个简易页面图片无缝滚动效果
页面图片无缝滚动JavaScript原理:移动的区块包含图片内容,区块相对父级元素进行定位脱离文档流.再令区块的left值每隔固定的时间进行等量减少(或增大)从而实现区块的匀速运动.由于每次间隔移动的 ...
- git的常用命令。。
git的常用命令.. git help <command> 显示command的help git show 显示某次提交的内容 git show $id git co -- <f ...
- SecureCRT连接Ubuntu报The remote system refused the connection.解决方案
使用SecureCRT连接到远程Ubuntu,连接失败报The remote system refused the connection. 进入Ubuntu系统,终端中敲入以下命令: ps -ef|g ...
- php基础知识 书写格式
PHP,是英文超文本预处理语言Hypertext Preprocessor的递归缩写.PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言. php嵌入页面的标记有 ...
- MatLab之Simulink之simple model
Use Simulink to model a system and then simulate the dynamic behavior of that system. 1 Open in Comm ...