Spring Cloud OpenFeign使用多个客户端

目标

使用Spring Cloud OpenFeign,在同一个SpringBoot服务中使用2个客户端。

步骤

maven

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

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

配置类

YunClientConfig.java

package cn.com.xxx.config;

import feign.Logger;
import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

/**
 * 消息中心获取token用户配置
 * @author zhangyalin
 */
public class YunClientConfig {

    @Value("${yun.username}")
    private String username;

    @Value("${yun.password}")
    private String password;
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor(username, password);
    }

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

YunAdminClientConfig.java

package cn.com.xxx.config;

import feign.Logger;
import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

/**
 * 消息中心admin用户
 * @author zhangyalin
 */
public class YunAdminClientConfig {

    @Value("${yun.admin.username}")
    private String username;

    @Value("${yun.admin.password}")
    private String password;
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor(username, password);
    }

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

这里有2点值得注意,一个是feign的日志配置。虽然在代码里面配置了,如下方式:

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

但仍然需要,在application.yml里面再配置,客户端接口类所在包的日志级别为DEBUG,即如下:

logging:
  level:
    cn.com.xxx.clients: DEBUG

第2点注意不要再这2个配置类上面配置@Configuration注解,如果配置上这个注解,会出现BasicAuthRequestInterceptor这两个Bean冲突。

客户端接口类

YunClient.java

package cn.com.xxx.clients;

import cn.com.xxx.config.YunClientConfig;
import cn.com.xxx.vo.ApiRes;
import cn.com.xxx.vo.GetTokenRes;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author zhangyalin
 */
@FeignClient(name = "yun", url = "${yun.endPoint}", configuration = YunClientConfig.class)
public interface YunClient {

    /**
     * 获取token接口
     * @param grantType 固定值为 xxxxxxxxx
     * @return token响应对象
     */
    @PostMapping(value = "/xxx/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    ApiRes<GetTokenRes> getToken(@RequestParam(name = "grant_type") String grantType);
}

YunAdminClient.java

package cn.com.xxx.clients;

import cn.com.xxx.config.YunAdminClientConfig;
import cn.com.xxx.vo.ApiRes;
import cn.com.xxx.vo.CheckTokenRes;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author zhangyalin
 */
@FeignClient(name = "yunAdmin", url = "${yun.endPoint}", configuration = YunAdminClientConfig.class)
public interface YunAdminClient {

    /**
     * 检查token接口
     * @param token 待验证的token
     * @param clientId 固定的客户端id
     * @param clientSecret 固定的客户端secret
     * @return 验证结果响应体
     */
    @GetMapping(value = "/xxx/check_token")
    ApiRes<CheckTokenRes> checkToken(@RequestParam(name = "token") String token, @RequestParam(name = "clientId") String clientId,
                                     @RequestParam(name = "clientSecret") String clientSecret);
}

这里通过@FeignClient注解,将2个客户端配置上不同的配置类解决了,同一个SpringBoot服务中,多个客户端配置的问题。

总结

Spring Cloud OpenFeign调用第三方还是挺容易的。这里主要涉及到Spring Cloud OpenFeign的多客户端配置,日志配置和HTTP基本认证问题。

参考:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值