SpringCloud OpenFeign

这篇具有很好参考价值的文章主要介绍了SpringCloud OpenFeign。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

OpenFeign

一、OpenFeign是什么

Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单。

Feign是Spring Cloud组件中一个轻量级RESTful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。

OpenFeign是Spring Cloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类

二、Spring Cloud 整合OpenFeign

  1. 添加openFeign依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 启动类添加Feign客户端注解
@EnableFeignClients
public class  OpsApplication {
    public static void main(String[] args) {
        SpringApplication.run(OpsApplication.class, args);
    }
}
  1. 编写Feign调用
@FeignClient(name = "zm-teachers", path = "/teacherInfo")
public interface TeacherClient {
    
}

三、OpenFeign特性

1. 超时时间配置

通过 Options 可以配置连接超时时间和读取超时时间,Options 的第一个参数是连接的超时时间(ms);第二个是请求处理的超时时间(ms)。

  • 配置类配置,全局配置
@Configuration
public class FeignConfig {
    @Bean
    public Request.Options options(){
        return new Request.Options(5000,10000);
    }
}
  • 配置类配置,局部配置
@FeignClient(name = "zm-teachers",configuration = FeignConfig.class)
public interface TeacherClient {
}

public class FeignConfig {
    @Bean
    public Request.Options options(){
        return new Request.Options(5000,10000);
    }
}
  • 配置文件配置,全局配置
#连接超时时间 单位:ms
feign.client.config.default.connectTimeout:5000
#请求处理超时时间 单位:ms
feign.client.config.default.connectTimeout:3000
  • 配置文件配置,局部配置
#连接超时时间 单位:ms
feign.client.config.zm-teachers.connectTimeout:5000
#请求处理超时时间 单位:ms
feign.client.config.zm-teachers.connectTimeout:3000

2. 日志配置

日志级别

日志等级 适用场景 内容
NONE(默认) 性能最佳,适合于生产 不产生任何日志
BASIC 适用于生产环境测试问题 记录请求方法、URL响应状态码和执行时间
HEADERS 在BASIC的基础上,记录请求和响应的header
FULL 适合开发和测试环境定位问题 记录请求和响应的header、bogy和元数据
  • 配置类配置,全局配置

    @Configuration
    public class FeignConfig {
        //日志级别
        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    
  • 配置类配置,局部配置

    @FeignClient(name = "zm-teachers",configuration = FeignConfig.class)
    public interface TeacherClient {
    }
    
    public class FeignConfig {
       //日志级别
        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    
  • 配置文件配置,全局配置

    feign.client.config.default.loggerLevel:FULL
    
  • 配置文件配置,局部配置

    feign.client.config.zm-teachers.loggerLevel:FULL
    

3. 通信组件

  • OkHttp

    <dependency>
      <groupId>io.github.openfeign</groupId>
      <artifactId>feign-okhttp</artifactId>
    </dependency>
    
    feign.okhttp.enabled:true
    
  • HttpClient

    <dependency>
       <groupId>io.github.openfeign</groupId>
       <artifactId>feign-httpclient</artifactId>
    </dependency>
    
    feign.httpclient.enabled:true
    

4. Gzip压缩

GET请求与POST请求的数据长度限制

理论上,GET请求和POST请求的数据长度是没有限制的,但是服务器以及浏览器会对其进行限制

  • 浏览器限制:为了兼容各个浏览器的长度请求,URL长度限制为【2083】个字符。(IE:2083、Firefox:65536、Chrome:8182)
  • 服务器限制:
    • tomecat:maxHttpHeaderSize参数
    • nginx:large_client_header_buffers参数

feign数据压缩配置

#开启feign请求压缩,默认不开始
feign.compression.reqest.enabled:true
#配置支持压缩的MIME TYPE,默认为:text/xml,application/xml,application/json
feign.compression.reqest.mime-types:text/xml,application/xml,application/json
#触发请求数据压缩的最小Size,默认2048KB
feign.compression.reqest.min-request-size: 2048

#开启Feign响应压缩,默认不开启
feign.compression.response.enabled:true
#使用GZip解码器,默认不使用
feign.compression.response.useGzipDecoder:true

数据压缩会给CPU带来负担

注,如果通信组件使用的是okhttp3,Gzip不生效

/*
 * Copyright 2013-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.openfeign.encoding;

import feign.Client;
import feign.Feign;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Configures the Feign response compression.
 *
 * @author Jakub Narloch
 * @see FeignAcceptGzipEncodingInterceptor
 */
@Configuration
@EnableConfigurationProperties(FeignClientEncodingProperties.class)
@ConditionalOnClass(Feign.class)
@ConditionalOnBean(Client.class)
@ConditionalOnProperty(value = "feign.compression.response.enabled", matchIfMissing = false)
//The OK HTTP client uses "transparent" compression.
//If the accept-encoding header is present it disable transparent compression
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
@AutoConfigureAfter(FeignAutoConfiguration.class)
public class FeignAcceptGzipEncodingAutoConfiguration {

	@Bean
	public FeignAcceptGzipEncodingInterceptor feignAcceptGzipEncodingInterceptor(FeignClientEncodingProperties properties) {
		return new FeignAcceptGzipEncodingInterceptor(properties);
	}
}

5. 负载均衡

负载均衡配置

zm-teachers.ribbon.NFLoadBalancerRuleClassName:com.netflix.loadbalancer.RandomRule
zm-teachers.ribbon.ConnectTimeout: 250                 # 连接超时时间
zm-teachers.ribbon. ReadTimeout: 1000                   # ribbon 读取超时时间
zm-teachers.ribbon. OkToRetryOnAllOperations: true      # 是否对所有操作都进行重试
zm-teachers.ribbon.MaxAutoRetriesNextServer: 1         # 切换实例的重试次数
zm-teachers.ribbon.MaxAutoRetries: 1                   # 对当前实例的重试次数
负载均衡策略 说明
权重策略/WeightedResponseTimeRule 根据每个服务提供者的响应时间分配一个权重,响应时间越长、权重越小、被选中的可能性也就越低。实现原理:刚开始使用轮询策略并开启一个计时器,每隔一段时间收集一次所有服务提供的平均响应时间,然后再给每个服务提供者附上权重,权重越高,被选中的概率越大。
最小连接数策略/BestAvailableRule 也叫最小并发策略,他是遍历服务提供者列表,选取连接数最小的一个服务实例。如果有相同的最小连接数,那么会调用轮询策略进行选取。
区域敏感策略/ZoneAvoidanceRule 根据服务所在的区域的性能和服务的可用性来选择服务实例,在没有区域的环境下,改策略和轮询策略类似。
可用敏感性策略/AvailabilityFilteringRule 先过滤到非健康的服务实例,然后再选择连接数较小的服务实例
随机策略/RandomRule 从服务提供者列表中随机选择一个服务实例
重试策略/RetryRule 按照轮询策略来获取服务,如果获取的服务为null或已失效,则在指定时间内进行重试来获取服务,如果超过指定时间依然没有获取到服务实例,返回null

6. Hystrix

复杂分布式服务面临的问题:服务雪崩

解决服务雪崩的方法:降级、隔离、熔断、请求缓存、请求合并。

<dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

Hystrix 回调的两种方式:

  1. fallback
@FeignClient(name = "zm-biz-import-service"fallback = ImportClientHystrix.class)
public interface ImportClient {
    @RequestMapping(method = RequestMethod.POST, path = "/upload/single/file" ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Result<TeacherUploadDto> uploadSingleFile(MultipartFile  param);
}

@Component
public class ImportClientHystrix implements ImportClient {

    @Override
    public Result<TeacherUploadDto> uploadSingleFile(MultipartFile param) {
        log.warn("ImportClientHystrix uploadSingleFile error ");
        return Result.error();
    }
}
  1. fallbackFactory
@FeignClient(name = "zmbiz-asset-query", fallbackFactory = AssetQueryFeignClientFallbackFactory.class)
public interface AssetQueryFeignClient {

    /**
     * @return 根据学生ID查询student_money
     * @param
     * @return
     */
    @GetMapping(value = "/api/studentMoney/queryByStuId")
    HaloResult<List<StudentMoneyDTO>> queryStudentMoneyByStuId(@RequestParam(value = "stuId") Integer stuId);
}

@Component
@Slf4j
public class AssetQueryFeignClientFallbackFactory implements FallbackFactory<AssetQueryFeignClient> {
    @Override
    public AssetQueryFeignClient create(Throwable cause) {
        return new AssetQueryFeignClient() {
            @Override
            public Result<User> addPerson(User user) {
                log.warn("调用zmbiz-user-business服务调用异常,uri = /tBsPerson/add/person, para = {}, e = {}", JSONObject.toJSONString(user), ExceptionUtils.getFullStackTrace(e));
                return Result.errorMessage("亲,服务器升级中,请稍后再试!");
            }
        };
    }
}

推荐使用fallbackFactory,可以通过打印降级的异常,查询出问题原因。

hystrix配置文章来源地址https://www.toymoban.com/news/detail-481394.html

feign.hystrix.enabled:true

#方案一:配置核心线程数量,以及最大线程数量

hystrix.threadpool.default.coreSize = 50
hystrix.threadpool.default.maximumSize = 100
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize = true

#方案二:配置队列长度以及拒绝长度

hystrix.threadpool.default.maxQueueSize: 1000 #BlockingQueue的最大队列数,默认值-1
hystrix.threadpool.default.queueSizeRejectionThreshold: 800 #即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝,默认值5

OpenFeign

一、OpenFeign是什么

Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单。

Feign是Spring Cloud组件中一个轻量级RESTful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。

OpenFeign是Spring Cloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类

二、Spring Cloud 整合OpenFeign

  1. 添加openFeign依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 启动类添加Feign客户端注解
@EnableFeignClients
public class  OpsApplication {
    public static void main(String[] args) {
        SpringApplication.run(OpsApplication.class, args);
    }
}
  1. 编写Feign调用
@FeignClient(name = "zm-teachers", path = "/teacherInfo")
public interface TeacherClient {
    
}

三、OpenFeign特性

1. 超时时间配置

通过 Options 可以配置连接超时时间和读取超时时间,Options 的第一个参数是连接的超时时间(ms);第二个是请求处理的超时时间(ms)。

  • 配置类配置,全局配置
@Configuration
public class FeignConfig {
    @Bean
    public Request.Options options(){
        return new Request.Options(5000,10000);
    }
}
  • 配置类配置,局部配置
@FeignClient(name = "zm-teachers",configuration = FeignConfig.class)
public interface TeacherClient {
}

public class FeignConfig {
    @Bean
    public Request.Options options(){
        return new Request.Options(5000,10000);
    }
}
  • 配置文件配置,全局配置
#连接超时时间 单位:ms
feign.client.config.default.connectTimeout:5000
#请求处理超时时间 单位:ms
feign.client.config.default.connectTimeout:3000
  • 配置文件配置,局部配置
#连接超时时间 单位:ms
feign.client.config.zm-teachers.connectTimeout:5000
#请求处理超时时间 单位:ms
feign.client.config.zm-teachers.connectTimeout:3000

2. 日志配置

日志级别

日志等级 适用场景 内容
NONE(默认) 性能最佳,适合于生产 不产生任何日志
BASIC 适用于生产环境测试问题 记录请求方法、URL响应状态码和执行时间
HEADERS 在BASIC的基础上,记录请求和响应的header
FULL 适合开发和测试环境定位问题 记录请求和响应的header、bogy和元数据
  • 配置类配置,全局配置

    @Configuration
    public class FeignConfig {
        //日志级别
        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    
  • 配置类配置,局部配置

    @FeignClient(name = "zm-teachers",configuration = FeignConfig.class)
    public interface TeacherClient {
    }
    
    public class FeignConfig {
       //日志级别
        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    
  • 配置文件配置,全局配置

    feign.client.config.default.loggerLevel:FULL
    
  • 配置文件配置,局部配置

    feign.client.config.zm-teachers.loggerLevel:FULL
    

3. 通信组件

  • OkHttp

    <dependency>
      <groupId>io.github.openfeign</groupId>
      <artifactId>feign-okhttp</artifactId>
    </dependency>
    
    feign.okhttp.enabled:true
    
  • HttpClient

    <dependency>
       <groupId>io.github.openfeign</groupId>
       <artifactId>feign-httpclient</artifactId>
    </dependency>
    
    feign.httpclient.enabled:true
    

4. Gzip压缩

GET请求与POST请求的数据长度限制

理论上,GET请求和POST请求的数据长度是没有限制的,但是服务器以及浏览器会对其进行限制

  • 浏览器限制:为了兼容各个浏览器的长度请求,URL长度限制为【2083】个字符。(IE:2083、Firefox:65536、Chrome:8182)
  • 服务器限制:
    • tomecat:maxHttpHeaderSize参数
    • nginx:large_client_header_buffers参数

feign数据压缩配置

#开启feign请求压缩,默认不开始
feign.compression.reqest.enabled:true
#配置支持压缩的MIME TYPE,默认为:text/xml,application/xml,application/json
feign.compression.reqest.mime-types:text/xml,application/xml,application/json
#触发请求数据压缩的最小Size,默认2048KB
feign.compression.reqest.min-request-size: 2048

#开启Feign响应压缩,默认不开启
feign.compression.response.enabled:true
#使用GZip解码器,默认不使用
feign.compression.response.useGzipDecoder:true

数据压缩会给CPU带来负担

注,如果通信组件使用的是okhttp3,Gzip不生效

/*
 * Copyright 2013-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.openfeign.encoding;

import feign.Client;
import feign.Feign;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Configures the Feign response compression.
 *
 * @author Jakub Narloch
 * @see FeignAcceptGzipEncodingInterceptor
 */
@Configuration
@EnableConfigurationProperties(FeignClientEncodingProperties.class)
@ConditionalOnClass(Feign.class)
@ConditionalOnBean(Client.class)
@ConditionalOnProperty(value = "feign.compression.response.enabled", matchIfMissing = false)
//The OK HTTP client uses "transparent" compression.
//If the accept-encoding header is present it disable transparent compression
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
@AutoConfigureAfter(FeignAutoConfiguration.class)
public class FeignAcceptGzipEncodingAutoConfiguration {

	@Bean
	public FeignAcceptGzipEncodingInterceptor feignAcceptGzipEncodingInterceptor(FeignClientEncodingProperties properties) {
		return new FeignAcceptGzipEncodingInterceptor(properties);
	}
}

5. 负载均衡

负载均衡配置

zm-teachers.ribbon.NFLoadBalancerRuleClassName:com.netflix.loadbalancer.RandomRule
zm-teachers.ribbon.ConnectTimeout: 250                 # 连接超时时间
zm-teachers.ribbon. ReadTimeout: 1000                   # ribbon 读取超时时间
zm-teachers.ribbon. OkToRetryOnAllOperations: true      # 是否对所有操作都进行重试
zm-teachers.ribbon.MaxAutoRetriesNextServer: 1         # 切换实例的重试次数
zm-teachers.ribbon.MaxAutoRetries: 1                   # 对当前实例的重试次数
负载均衡策略 说明
权重策略/WeightedResponseTimeRule 根据每个服务提供者的响应时间分配一个权重,响应时间越长、权重越小、被选中的可能性也就越低。实现原理:刚开始使用轮询策略并开启一个计时器,每隔一段时间收集一次所有服务提供的平均响应时间,然后再给每个服务提供者附上权重,权重越高,被选中的概率越大。
最小连接数策略/BestAvailableRule 也叫最小并发策略,他是遍历服务提供者列表,选取连接数最小的一个服务实例。如果有相同的最小连接数,那么会调用轮询策略进行选取。
区域敏感策略/ZoneAvoidanceRule 根据服务所在的区域的性能和服务的可用性来选择服务实例,在没有区域的环境下,改策略和轮询策略类似。
可用敏感性策略/AvailabilityFilteringRule 先过滤到非健康的服务实例,然后再选择连接数较小的服务实例
随机策略/RandomRule 从服务提供者列表中随机选择一个服务实例
重试策略/RetryRule 按照轮询策略来获取服务,如果获取的服务为null或已失效,则在指定时间内进行重试来获取服务,如果超过指定时间依然没有获取到服务实例,返回null

6. Hystrix

复杂分布式服务面临的问题:服务雪崩

解决服务雪崩的方法:降级、隔离、熔断、请求缓存、请求合并。

<dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

Hystrix 回调的两种方式:

  1. fallback
@FeignClient(name = "zm-biz-import-service"fallback = ImportClientHystrix.class)
public interface ImportClient {
    @RequestMapping(method = RequestMethod.POST, path = "/upload/single/file" ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Result<TeacherUploadDto> uploadSingleFile(MultipartFile  param);
}

@Component
public class ImportClientHystrix implements ImportClient {

    @Override
    public Result<TeacherUploadDto> uploadSingleFile(MultipartFile param) {
        log.warn("ImportClientHystrix uploadSingleFile error ");
        return Result.error();
    }
}
  1. fallbackFactory
@FeignClient(name = "zmbiz-asset-query", fallbackFactory = AssetQueryFeignClientFallbackFactory.class)
public interface AssetQueryFeignClient {

    /**
     * @return 根据学生ID查询student_money
     * @param
     * @return
     */
    @GetMapping(value = "/api/studentMoney/queryByStuId")
    HaloResult<List<StudentMoneyDTO>> queryStudentMoneyByStuId(@RequestParam(value = "stuId") Integer stuId);
}

@Component
@Slf4j
public class AssetQueryFeignClientFallbackFactory implements FallbackFactory<AssetQueryFeignClient> {
    @Override
    public AssetQueryFeignClient create(Throwable cause) {
        return new AssetQueryFeignClient() {
            @Override
            public Result<User> addPerson(User user) {
                log.warn("调用zmbiz-user-business服务调用异常,uri = /tBsPerson/add/person, para = {}, e = {}", JSONObject.toJSONString(user), ExceptionUtils.getFullStackTrace(e));
                return Result.errorMessage("亲,服务器升级中,请稍后再试!");
            }
        };
    }
}

推荐使用fallbackFactory,可以通过打印降级的异常,查询出问题原因。

hystrix配置

feign.hystrix.enabled:true

#方案一:配置核心线程数量,以及最大线程数量

hystrix.threadpool.default.coreSize = 50
hystrix.threadpool.default.maximumSize = 100
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize = true

#方案二:配置队列长度以及拒绝长度

hystrix.threadpool.default.maxQueueSize: 1000 #BlockingQueue的最大队列数,默认值-1
hystrix.threadpool.default.queueSizeRejectionThreshold: 800 #即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝,默认值5

到了这里,关于SpringCloud OpenFeign的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringCloud源码解析-gateway&openFeign

    springcloud是基于springboot的,gateway各个组件的初始化入口在自动装配 1.2.1 GatewayClassPathWarningAutoConfiguration 主要作用是校验依赖 在GatewayAutoConfiguration之前被加载 校验是否导入了spring-boot-starter-web, gateway非web容器,不需要导入 spring-boot-starter-web 校验是否缺少spring-boot-starter-webflux依

    2024年02月08日
    浏览(37)
  • 【SpringCloud】OpenFeign服务接口调用快速入门

    官网地址:点击跳转 Feign是一个 声明性web服务客户端 。它使编写web服务客户端变得更容易。使用 Feign 创建一个接口并对其进行注释。它具有可插入的注释支持,包括Feign注释和 JAX-RS 注释。Feign 还支持可插拔编码器和解码器。Spring Cloud 添加了对 Spring MVC 注释的支持,以及对

    2024年04月25日
    浏览(29)
  • SpringCloud入门(微服务调用 OpenFeign)——从RestTemplate到OpenFeign & OpenFeign的相关配置 & 源码的分析和请求流程拆解

    在之前的博客中,我们介绍了RestTemplate的使用,博客文章如下连接。但是在使用RestTemplate的时候,需要把生产者的路径拼出来,非常繁琐,另外参数的传递的也比较繁琐,解决方案就是使用openFeign。 SpringCloud入门(RestTemplate + Ribbon)——微服务调用的方式 RestTemplate的使用 使

    2024年04月11日
    浏览(26)
  • SpringCloud openFeign 之 获取被调用服务名

    一. 概述 低版本 feign 只能获取到被调用方法的信息。 只有高版本 feign 才支持获取到被调用服务的信息。 二. 代码实现 三. 特别注意 在升级 feign-core 版本后,可能会出现 java.lang.NoSuchMethodException 异常。这就说明有版本问题,此时需要定位到报错位置,将对应方法所属 jar 包调

    2024年01月16日
    浏览(26)
  • 【SpringCloud】-OpenFeign实战及源码解析、与Ribbon结合

    OpenFeign(简称Feign)是一个声明式的Web服务客户端,用于简化服务之间的HTTP通信。与Nacos和Ribbon等组件协同,以支持在微服务体系结构中方便地进行服务间的通信; OpenFeign在默认情况下集成了Hystrix,提供了服务容错和服务降级的功能。 按照单一职责,也为了满足可复用、可

    2024年02月04日
    浏览(31)
  • SpringCloud - OpenFeign 参数传递和响应处理(全网最详细)

    目录 一、OpenFeign 参数传递和响应处理 1.1、feign 客户端参数传递 1.1.1、零散类型参数传递 1. 例如 querystring 方式传参 2. 例如路径方式传参 1.1.2、对象参数传递 1. 对象参数传递案例 1.1.3、数组参数传递 1. 数组传参案例 1.1.4、集合类型的参数传递(了解) 1.2、feign 客户端响应处

    2024年02月02日
    浏览(30)
  • SpringCloud --- Feign远程调用

    先来看我们以前利用RestTemplate发起远程调用的代码: 存在下面的问题: 代码可读性差,编程体验不统一 参数复杂URL难以维护 Feign是一个声明式的http客户端,官方地址:GitHub - OpenFeign/feign: Feign makes writing java http clients easier 其作用就是帮助我们优雅的实现http请求的发送,解决

    2024年02月05日
    浏览(32)
  • SpringCloud(七) Feign远程调用

    目录 一, RestTemplate远程调用存在的问题 二, Feign的远程调用 2.1 什么是Fegin 2.2 Feign的使用(代替RestTemplate) 1. 引入依赖 2. 添加注解  3. 编写Feign的客户端 4. 测试 5. 总结  2.3 自定义配置 1. 配置文件方式  2. Java代码方式  三, Feign使用优化 3.1 使用连接池 1. 引入依赖 2. 配置连接池

    2024年02月06日
    浏览(29)
  • SpringCloud-基于Feign远程调用

    Spring Cloud 是一个用于构建分布式系统的开发工具包,它提供了一系列的微服务组件,其中之一就是 Feign。Feign 是一种声明式的 Web 服务客户端,它简化了在 Spring Cloud 中进行远程调用的过程。本文将介绍如何在 Spring Cloud 中使用 Feign 进行远程调用。 我们在 Spring Cloud 项目的

    2024年02月21日
    浏览(44)
  • SpringCLoud——Feign的远程调用

    来看一下之前我们使用RestTemplate调用时编写的Contrriller代码: //        2. 利用RestTemplate发起HTTP请求 //        2.1 url 地址         String url = \\\"http://userserver/user/\\\" + order.getUserId(); //        2.2 发送http请求,实现远程调用         User user = restTemplate.getForObject(url,

    2024年02月07日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包