004声明式服务调用Feign & 断路器Hystrix
1、POM配置
和普通Spring Boot工程相比,添加了Eureka Client、Feign、Hystrix依赖和Spring Cloud依赖管理
<dependencies>
<!--Eureka Client依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--声明式服务消费-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- 整合断路器hystrix,其实feign中自带了hystrix,引入该依赖主要是为了使用其中的hystrix-metrics-event-stream,用于dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!-- 服务健康检查,必须添加,否则此服务不会启动hystrix.stream -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<!--Spring Cloud依赖版本管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
02、使能
@SpringBootApplication
@EnableFeignClients //声明式服务消费
@EnableCircuitBreaker //断路器
@EnableEurekaClient //Eureka客户端
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
03、src/main/resources工程配置文件 application.yml
server:
port: 3010
spring:
application:
name: feign-hello-service-consumer
eureka:
client:
serviceUrl:
defaultZone: http://discovery:1000/eureka/
feign:
hystrix:
enabled: true #在新版本Feign中,hystrix默认关闭,必须手动打开
04、声明式服务消费
@FeignClient(name = "hello-service-provider", fallback = HelloFallback.class)
public interface HelloFeignService {
@RequestMapping("/hello")
public String hello(); } @Component
class HelloFallback implements HelloFeignService { @Override
public String hello() {
return "Error";
}
}
05、Controller
@RestController
public class FeignController {
@Autowired
private HelloFeignService helloService; @GetMapping("feign")
public String hello() {
return this.helloService.hello();
}
}
004声明式服务调用Feign & 断路器Hystrix的更多相关文章
- SpringCloud 源码系列(6)—— 声明式服务调用 Feign
SpringCloud 源码系列(1)-- 注册中心 Eureka(上) SpringCloud 源码系列(2)-- 注册中心 Eureka(中) SpringCloud 源码系列(3)-- 注册中心 ...
- SpringCloud开发学习总结(七)—— 声明式服务调用Feign(一)
在实践的过程中,我们会发现在微服务架构中实现客户端负载均衡的服务调用技术Spring Cloud Ribbon<SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon> ...
- SpringCloud之声明式服务调用 Feign(三)
一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样.我们知道,阿里巴巴的doubbo采 ...
- Spring Cloud 声明式服务调用 Feign
一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...
- 【Dalston】【第三章】声明式服务调用(Feign)
当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻.那 ...
- Spring Cloud第七篇 | 声明式服务调用Feign
本文是Spring Cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...
- Spring Cloud Feign 1(声明式服务调用Feign 简介)
Spring Cloud Feign基于Netflix Feign 同时整合了Spring Cloud Ribbon和Spring Cloud Hytrix,除了提供两者的强大功能外,它还提供了一种声 ...
- 声明式服务调用Feign
什么是 Feign Feign 是种声明式.模板化的 HTTP 客户端(仅在 consumer 中使用). 什么是声明式,有什么作用,解决什么问题? 声明式调用就像调用本地方法一样调用远程方法;无 ...
- SpringCloud开发学习总结(七)—— 声明式服务调用Feign(三)
Feign中的Ribbon配置 由于Spring Cloud Feign的客户端负载均衡是通过Spring Cloud Ribbon实现的,所以我们可以直接通过配置Ribbon客户端的方式来自定义各个 ...
随机推荐
- tomcat在bin下的startup.bat下启动报错
测试环境是否安装配置好. 如果环境配置好.报错如下:或者是730013 -----------解决问题:是因为tomcat端口被占用.查看是否启动两个tomcat
- js的节流和防抖
1,节流 节流就是对连续的函数触发,在设定的间隔时间段内,只让其执行一次. 先来看看js高级程序设计3里面节流的代码 function throttle (method, context, wait) ...
- TCPIP详解卷一
strongswan man page. kernel-book IntroductionParallelPro then POSIX THread
- PHP错误与异常处理
https://www.cnblogs.com/zyf-zhaoyafei/p/6928149.html 请一定要注意,没有特殊说明:本例 PHP Version < 7 说起PHP异常处理,大 ...
- CSS定位属性
定位属性 position属性 1. s ...
- odoo 开发 context 上下文的用法
context 这是一个上下文,运用很灵活 得到整个context self.context_get() self.env['res.users'].context_get() 得到cont ...
- windows本地提权——2003笔记
windows主机 用户有三种:Users,Administrator,System 本地拥有administrator权限用户提权到system用户 win2003: at命令 系统调度任务的指令, ...
- Java框架-mybatis02基本的crud操作
1.搭建mybatis框架 1)导入相关jar包 2)编写核心配置文件(配置数据库连接的相关信息以及配置mapper映射文件) 3)编写dao操作 4)编写mapper映射文件 5)编写实体类 2.执 ...
- springboot整合security实现基于url的权限控制
权限控制基本上是任何一个web项目都要有的,为此spring为我们提供security模块来实现权限控制,网上找了很多资料,但是提供的demo代码都不能完全满足我的需求,因此自己整理了一版. 在上代码 ...
- 手写css按钮组
css: .lf{float:left} .btn{ width:60px; height:24px; color:#fff; border-radius:4px; cursor:pointer; b ...