zoukankan      html  css  js  c++  java
  • feign 使用

    feign 是netflix 提供的申明式的httpclient调用框架

    整合方法

    1.添加依赖

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

    2.在application 添加注解

    @EnableFeignClients

     3.编写调用代码

    @FeignClient(name = "jpaas-form")
    public interface FormClient {
    
        /**
         * 根据表单别名获取表单相关数据。
         * @param alias
         * @param pk
         * @param initPermission
         * @return
         */
        @GetMapping("/form/core/formPc/getByAlias")
        JsonResult<BpmView> getByAlias(@RequestParam(value = "alias") String alias,
                                              @RequestParam(value = "pk")String pk,
                                              @RequestParam(value = "initPermission") Boolean initPermission);

    1.添加一个接口类。

    2.增加@FeignClient 注解 

      name 指向需要调用的 微服务名称

    3.增加调用方法

      遵循 mvc的写法,如果返回的数据是一个java对象,最好把这个实体做成公共的类,供被调用者和调用者进行使用。

    @GetMapping("/users/{id}")
        UserDTO findById(@PathVariable Integer id);

    方法示例,和spring mvc 差不多。

    4.自定义FEIGN日志级别

    feign 日志级别,就是需要打印feign的调用参数和响应数据。

     使用java代码来实现。

    1.编写配置类

    import feign.Logger;
    import org.springframework.context.annotation.Bean;
    
    public class UserCenterFeignClientConfig {
    
        @Bean
        public Logger.Level level(){
            return  Logger.Level.FULL;
        }
    }

    2.feign client 类指定配置

    @FeignClient(name = "user-center",
    //    fallback = UserCenterFeignClientFallback.class,
    //    fallbackFactory = UserCenterFeignClientFallbackFactory.class,
            configuration = UserCenterFeignClientConfig.class
    )
    public interface UserCenterFeignClient {

    3.在日志中进行指定

    编辑 application.yml

    logging:
      level:
        com.demo.contentcenter.feignclient.UserCenterFeignClient: debug

     注意这个日志界别需要为 debug 级别,只有在debug模式才会输出feign日志。

    4. feign 多参数请求实现

    请参考文章

    http://www.imooc.com/article/289000

    5.使用 feign 访问非 注册服务数据

    有些情况下,我们使用feign 访问外部url,比如访问 博客网首页。

    编写代码:

    @FeignClient(name = "cnblogs", url = "https://www.cnblogs.com")
    public interface ExternalFeignClient {
    @GetMapping("/yg_zhang")
    String index();
    }

    这里需要指定 name 和URL地址。

    6.性能优化

    配置启用http连接池:

    <dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-httpclient</artifactId>
                <version>10.2.3</version>
            </dependency>

    修改 application.yml

    feign:
      client:
        config:
          # 全局配置
          default:
            loggerLevel: basic
    
      httpclient:
        enabled: true
        max-connections: 200
        max-connections-per-route: 50

     7.feign 的常见问题

    http://www.imooc.com/article/289005

  • 相关阅读:
    Java使用jxl修改现有Excel文件内容,并验证其是否对公式的结果产生影响
    Java使用MyBatis的ScriptRunner执行SQL脚本
    Linux下批量解压.Z格式文件
    Java中将一个反斜杠转换成两个反斜杠
    mysql线上操作常用命令
    MySQL主从不一致修复
    slave_exec_mode参数对主从复制的影响
    ssh访问跳过RSA key"yes/no"验证
    k8s更新Pod镜像
    Go 延迟函数 defer 详解
  • 原文地址:https://www.cnblogs.com/yg_zhang/p/12635961.html
Copyright © 2011-2022 走看看