关键词:
什么是Feign
Feign是Spring Cloud Netflix组件中的一个轻量级RESTFUL和HTTP服务客户端,实现了负载均衡和Rest调用的开源框架,封装了Ribbon和RestTemplate,实现了web Service的面向接口编程,进一步降低了项目的耦合度。
是一种声明式,模板化的HTTP客户端(仅在Consumer中使用),使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务
Feign解决了什么问题
像调用本地方法一样调用远程方法,无感知远程HTTP请求,使用Feign实现负载均衡是首选方案,只需要创建一个接口,然后在上面添加注解即可。
Feign VS OpenFeign
OpenFeign 是spring cloud在Feign的基础上支持了Spring MVC注解,如@RequestMapping,@Pathvariable。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式生产实现类,实现类中做负载均衡并调用服务
Feign入门案例
使用的是前面的springcloud的Eureka中的两个provider,以及注册中心,这里consumer的使用,需要注意,需要单独写一个service,表示请求服务的接口名称
需要调用的服务为service-provider,调用的接口就是selectAll接口,在接口上面要注解调用服务的请求地址
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.GetMapping
@FeignClient("service-provider")
interface ProductService
@GetMapping("/product/test")
fun selectAll():String
build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins
id("org.springframework.boot") version "2.3.7.RELEASE"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("jvm") version "1.3.72"
kotlin("plugin.spring") version "1.3.72"
group = "com"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories
mavenCentral()
dependencies
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.springframework.boot:spring-boot-starter-test")
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies
implementation("org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR12")
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client
implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.2.10.RELEASE")
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:2.2.10.RELEASE")
tasks.withType<Test>
useJUnitPlatform()
tasks.withType<KotlinCompile>
kotlinOptions
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
application.yml
server:
port: 9091
spring:
application:
name: service-consumer
eureka:
client:
register-with-eureka: false
registry-fetch-interval-seconds: 10
service-url:
service-url:
defaultZone: http://localhost:8761/eureka/
#局部的负载均衡策略
#service-provider 为调用的服务的名称
service-provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
启动文件
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.openfeign.EnableFeignClients
@SpringBootApplication
@EnableFeignClients
class ServiceConsumerApplication
fun main(args: Array<String>)
runApplication<ServiceConsumerApplication>(*args)
consumer接口的实现层
import com.service_consumer.service.ProductService
import com.service_consumer.service.TestService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Repository
@Repository
class TestServiceImpl :TestService
@Autowired
lateinit var productService: ProductService
override fun hello(id: String): String
return productService.selectAll()+" "+id
项目结构
Feign负载均衡
可以使用配置文件注解的方式进行全局配置,也可以在application.yml中,根据服务名进行单独配置
import com.netflix.loadbalancer.RandomRule
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
class RibbonConfig
@Bean
fun randomRule(): RandomRule
return RandomRule()
Feign请求传参
get方式的传递
@GetMapping("/product/id")
fun selectById(@PathVariable("id") id:String) : String
然后调用正常调用consumer接口就可以了
post方法传参,在服务中就是@RequstBody注解
@PostMapping("/product/save")
fun saveOne(user:Product):R<*>
Feign性能优化
gzip压缩,大约可以减少70%以上的文件大小
局部配置
只要配置Consumer通过Feign到Provider的请求与相应的Gzip压缩
feign:
compression:
request:
mime-types: text/xml,application/xml,application/json #配置压缩支持的类型
min-request-size: 512 #配置压缩数据大小的最小阈值,默认2048
enabled: true #请求是否开启
response:
enabled: true #响应是否开启
全局配置
server:
port: 9091
compression:
enabled: true
mime-types: application/json,application/xml,text/html,text/xml,text/plain
HTTP连接池
使用HttpClient,它封装了HTTP的请求头,参数,内容体,响应等等,不仅使客户端发送HTTP请求变得容易,而且也方便了开发人员测试接口(基于HTTP协议的)
需要进入如下依赖(一般需要导入第一个,第一个以及集成在了新版的SpringCloud中)
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
implementation("org.apache.httpcomponents:httpclient:4.5.13")
// https://mvnrepository.com/artifact/io.github.openfeign/feign-httpclient
implementation("io.github.openfeign:feign-httpclient:11.8")
需要在consumer的配置文件中,声明使用HTTPClient
feign:
httpclient:
enabled: true
状态查看
需要在resources中添加日志文件logback.xml
请求超时
全局的
ribbon:
ConnectTimeout: 5000 #默认时间是1秒
ReadTimeout: 5000 #请求处理的超时时间
局部的,可以根据不同的微服务的名称进行配置
暑期编程PK赛 得CSDN机械键盘等精美礼品!springcloud无废话入门03:feign声明式服务调用
1.Feign概述 在上一篇的HelloService这个类中,我们有这样一行代码: returnrestTemplate.getForObject("http://hello-service/hello",String.class); 查看详情
springcloud——feign声明式服务调用(代码片段)
什么是FeignFeign是SpringCloudNetflix组件中的一个轻量级RESTFUL和HTTP服务客户端,实现了负载均衡和Rest调用的开源框架,封装了Ribbon和RestTemplate,实现了webService的面向接口编程,进一步降低了项目的耦合度。是一种声... 查看详情
springcloud——feign声明式服务调用(代码片段)
什么是FeignFeign是SpringCloudNetflix组件中的一个轻量级RESTFUL和HTTP服务客户端,实现了负载均衡和Rest调用的开源框架,封装了Ribbon和RestTemplate,实现了webService的面向接口编程,进一步降低了项目的耦合度。是一种声... 查看详情
springcloud入门教程:用声明式rest客户端feign调用远端http服务
首先简单解释一下什么是声明式实现?要做一件事,需要知道三个要素,where,what,how。即在哪里(where)用什么办法(how)做什么(what)。什么时候做(when)我们纳入how的范畴。1)编程式实现:每一个要素(where,what,how)都... 查看详情
springcloud(hoxton.sr3)基础篇:第六章feign声明式服务调用
一、Feign简介 在前面的文章中可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下 ... 查看详情
springcloud之feign实现声明式rest调用
...的HTTP客户端,可帮助我们更加便捷、优雅的调用HTTPapi。springcloud对Feign进行了增强,使Feign支持了SpringMVC注解,并整合了Ribbon和Eureka,从而让Feign的使用更加方便:只需要创建一个接口,并在接口上添加一些注解,代码就完成了... 查看详情
springcloud——feign声明式服务调用(代码片段)
什么是FeignFeign是SpringCloudNetflix组件中的一个轻量级RESTFUL和HTTP服务客户端,实现了负载均衡和Rest调用的开源框架,封装了Ribbon和RestTemplate,实现了webService的面向接口编程,进一步降低了项目的耦合度。是一种声... 查看详情
springcloud:使用feign实现声明式rest调用
一、简介前面我们是使用RestTemplate实现restapi调用的,代码如下:@GetMapping("/user/{id}")public User findById(@PathVariable Long id) throws Exception { return & 查看详情
springcloud:服务调用-声明式客户端访问
环境springcloudEdgware.SR6jdk7sts4.6.0mysql5.7背景通过声明式客户端openfeign进行支付微服务的访问。搭建步骤只需要修改支付服务调用层,增加依赖就可以了。支付服务调用层只需要定义接口,不需要进行实现。packagejiangbo.springcloud.dao;im... 查看详情
openfeign:声明式服务调用
SpringCloudOpenFeign:声明式服务调用一、OpenFeign简介1.什么是OpenFeignOpenFeign目前是SpringCloud二级子项目。平时说的Feign指的是Netflix下的Feign,现在我们学习的是OpenFeign,是Spring提供的。OpenFeign是一种声明式、模板化的HTTP客户端(... 查看详情
openfeign:声明式服务调用
SpringCloudOpenFeign:声明式服务调用一、OpenFeign简介1.什么是OpenFeignOpenFeign目前是SpringCloud二级子项目。平时说的Feign指的是Netflix下的Feign,现在我们学习的是OpenFeign,是Spring提供的。OpenFeign是一种声明式、模板化的HTTP客户端(... 查看详情
干货分享微服务spring-cloud(5.声明式服务调用feign)
Springcloudfeign基于Netflixfeign实现,整合了springcloudribbon与springcloudhystrix,除了提供这两者的强大功能之外,它还提供了一种声明式的web服务客户端定义方式新建springboot工程并命名为demo-springcloud-feign-consumer,新建启动类FeignApplication... 查看详情
springcloud(10):使用feign实现声明式rest调用-构造多参数请求
请求多参数的URL假设请求的URL包含多个参数,如:http://localhost:8086/user1?id=1&username=nihao 1.1、Feign接口@FeignClient(name = "spring-ribbon-eureka-client2")public interface UserFeignClient&nb 查看详情
springcloud系列四feign远程调用服务
一:Feign简介Feign是一种声明式、模板化的HTTP客户端,在SpringCloud中使用Feign,可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。Feign的灵感来源... 查看详情
springcloud声明试服务调用feign
利用fegin去调用服务:先修改两个HELLO-SERVER服务中添加@GetMapping("/hello1")publicStringhello(@RequestParamStringname){return"Hello"+name;}@GetMapping("/hello2")publicUserhello(@RequestHeaderStringname,@RequestHeaderInt 查看详情
springcloud:feign的应用
1、概念 Feign是一种声明式、模板化的HTTP客户端,是一个声明web服务客户端,这便得编写web服务客户端更容易。2、应用 2.1、在项目中,模块与模块之间需要互相调用,比如web模块需要调用service模块的服务,这个时候... 查看详情
springcloud学习-feign入门
feign 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用。传统的webservice中,通过引用wsdl来自动生成一些client的代理类(或stub代码),feign跟这个有点类似,但是更灵活。先回顾一下,上节中... 查看详情
springcloud中hystrixribbon及feign的熔断关系
在SpringCloud中Hystrix、Ribbon以及Feign它们三者之间在处理微服务调用超时从而触发熔断降级的关系是什么? 我们知道在SpringCloud微服务体系下,微服务之间的互相调用可以通过Feign进行声明式调用,在这个服务调用过程中Feign会... 查看详情