创建服务消费者(Feign)
概述
Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,并和 Eureka 结合,默认实现了负载均衡的效果
Feign 采用的是基于接口的注解
Feign 整合了 ribbon和hystrix
创建服务消费者
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--spring cloud starter feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
主要增加对Feign的依赖
Application
通过@EnableFeignClients注解开启Feign功能
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
application.yml
server:
port: 9002
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka/
instance:
lease-renewal-interval-in-seconds: 10 #服务续约
lease-expiration-duration-in-seconds: 15 #服务剔除>服务续约时间
hostname: localhost
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
spring:
application:
name: eureka-client-feign
feign:
client:
config:
eureka-provider:
#配置feign日志级别
logger-level: full
#开启feign对hystrix的支持,默认为false
hystrix:
enabled: true
logging:
level:
#必须设置feign的服务包日志级别为debug
com.ley.springcloud.feign.service: debug
创建Feign接口
通过@FeignClient("服务名")注解来指定调用哪个服务
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "eureka-provider")
public interface HelloService {
@GetMapping("/hello")
String hello();
}
创建测试用的Controller
import com.ley.springcloud.feign.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class FeignController {
@Autowired
private HelloService helloService;
@GetMapping("/feign/hello")
public String hello() {
return helloService.hello();
}
}
创建服务消费者(Feign)的更多相关文章
- SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解
前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...
- springcloud-Netflix创建服务消费者
目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...
- 创建服务消费者(Ribbon)
概述 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的.Spring cloud 有两种服务调用方式,一种是 ribbon + restTempla ...
- Spring Cloud学习笔记【三】服务消费者Feign
Feign 是一个声明式的 Web Service 客户端,它的目的就是让 Web Service 调用更加简单.它整合了 Ribbon 和 Hystrix,从而让我们不再需要显式地使用这两个组件.F ...
- SpringCloud-创建服务消费者-Feign方式(附代码下载)
场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
- Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign
上一篇文章,讲述了如何通过RestTemplate + Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...
- 服务消费者Feign和Ribbon的区别
1.Ribbon通过注解@EnableEurekaClient/@EnableDiscoveryClient向服务中心注册: PS:选用的注册中心是eureka,那么就推荐@EnableEure ...
- Spring Cloud (4) 服务消费者-Feign
Spring Cloud Feign Spring Cloud Feign 是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单,我们只需要创建接口并 ...
- 8、服务发现&服务消费者Feign
spring cloud的Netflix中提供了两个组件实现软负载均衡调用,分别是Ribbon和Feign.上一篇和大家一起学习了Ribbon. Ribbon :Spring Cloud Ribbon ...
随机推荐
- 调试分析工具 (C/C++)
一.静态分析工具 cppcheck cppcheck主要用于对C/C++源代码进行分析检查的一个开源工具,可以用来检测未使用的变量.越界访问.内存泄漏等问题. 使用方法 cppcheck --enab ...
- Android真机调试不打印日志解决
1.在拨号界面输入:*#*#2846579#*#* 进入测试菜单界面. 2.Project Menu–后台设置–LOG设置 3.LOG开关–LOG打开 LOG级别设置–VERBOSE 4.Dump&a ...
- 10.24的注意事项——解决linux_jni编译错误的问题
公司以opus开源库.因此,我们遇到了一些问题. 我将新下载的opus1.1替换掉老版本号之后,单独编译opus没问题.但是编译相关的文件就会报错. 错误信息例如以下: g++ -Wall -fPIC ...
- java中 8进制 10进制 2进制 16进制 相互转换
十进制转成十六进制: Integer.toHexString(int i) 十进制转成八进制 Integer.toOctalString(int i) 十进制转成二进制 Integer.toBinar ...
- 分布式高级(十三)Docker Container之间的数据共享
sudo docker run -it -v /usr/lib:/usr/lib/dbdata --name dbcontainer-192.168.1.184 ubuntu:14.04 sudo d ...
- Netty In Action中文版 - 第六章:ChannelHandler
本章介绍 ChannelPipeline ChannelHandlerContext ChannelHandler Inbound vs outbound(入站和出站) 接受连接或创建他们仅仅是你的应 ...
- BS学习概述
从最初的牛腩新闻公布系统,到如今的JS,回想一下,自己的BS也算是学了大半,可是有时候想起来还是总是有一种不踏实的感觉,一是由于从开学到如今赶上了三级考试,自考.软考,导致BS学习时间被大大压缩了,代 ...
- python 教程 第十五章、 结构布局
第十五章. 结构布局 #!/usr/bin/env python #(1)起始行 "this is a module" #(2)模块文档 import sys #(3)模块导入 d ...
- ORACLE 时间加减操作
DATE和timestamp类型都可以进行加减操作.可以对当前日期加年.月.日.时.分.秒,操作不同的时间类型,有三种方法: 1 使用内置函数numtodsinterval增加小时,分钟和秒2 加一个 ...
- DataGridTemplateColumn
DataGridTemplateColumn自定义单元格样式 <DataGrid Grid.Row="0" Name="BasicRuleDataGrid" ...