前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >干掉 Feign,Spring Cloud Square 组件发布

干掉 Feign,Spring Cloud Square 组件发布

作者头像
冷冷
发布2021-04-22 15:35:21
9960
发布2021-04-22 15:35:21
举报
文章被收录于专栏:冷冷冷冷冷冷

Spring Cloud Square 是什么

谈起 Spring Cloud 生态大家一定对 Feign 不陌生,如下图所示,Feign 可以把底层(okhttp、httpclient)Rest 的请求进行隐藏,伪装成类似 SpringMVC 的 Controller 一样。你不用再自己拼接 url,拼接参数等等操作,一切都交给 Feign 去做。使用 Feign 调用 API 就像调用本地方法一样,从避免了调用目标微服务时,需要不断的解析/封装 json 数据的繁琐。

Spring Cloud Feign 调用
Spring Cloud Feign 调用

Spring Cloud Square 项目旨在替代原有的 Spring Cloud Feign , 借助 Retrofit 对底层通信类库的封装实现跨服务调用,目前已在 spring-cloud-incubator 孵化器进行孵化 (上一个在孵化器孵化 spring-cloud-loadbalancer 已经正式接替 Ribbon 成为正式推荐组件)。

在了解 Spring Cloud Square 之前,需要先了解以下组件:

  • OkHttp 是一个关于网络请求的第三方类库,其中封装了网络请求的 get、post 等操作的底层实现,是目前最为火热的网络请求框架之一。
  • Retrofit 是一个 RESTful 的 HTTP 网络请求框架,它是基于 OkHttp 的。它是通过注解配置网络参数的,支持多种数据的解析和序列化(Gson、Json、Xml 等,并且对 RxJava 也是支持的。

那么基于 Spring Cloud Square 的服务调用可以抽象成如下图所示:

Spring Cloud Square 调用
Spring Cloud Square 调用

快速上手

1618403200
1618403200

添加依赖

  • 由于目前 spring-cloud-square 未正式发布,需要配置 spring maven 仓库。
<repositories>
  <repository>
    <id>spring-milestones</id>
    <url>https://repo.spring.io/milestone</url>
  </repository>
</repositories>
  • maven 依赖
<dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-square-okhttp</artifactid>
  <version>${square.version}</version>
</dependency>
<dependency>
  <groupid>com.squareup.okhttp3</groupid>
  <artifactid>okhttp</artifactid>
  <version>${okhttp.version}</version>
</dependency>

<!--添加负载均衡支持-->
<dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-starter-loadbalancer</artifactid>
</dependency>

代码配置

@Bean
@LoadBalanced
public OkHttpClient.Builder okHttpClientBuilder() {
    return new OkHttpClient.Builder();
}

代码调用

  • 和最早的 ribbon 调用一样,非常的简单。
@Autowired
OkHttpClient.Builder builder;

@GetMapping
public String req() {
    Request request = new Request.Builder()
            .url("http://square-provider/req").build();
    Response response = builder.build().newCall(request).execute();
    return response.body().string();
}

进阶使用

作为 Spring Cloud Feign 的替代品,square 也支持声明式客户端的形式。注意看以下代码 和 feign 一样的味道

添加依赖

<dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-square-retrofit</artifactid>
  <version>${square.version}</version>
</dependency>

声明调用客户端

@RetrofitClient("square-provider")
public interface DemoService {

    @GET("/")
    Call<string> req();
}

开启客户端扫描

@EnableRetrofitClients

代码调用

@Autowired
DemoService demoService;

@SneakyThrows
@GetMapping("/retrofit")
public String retrofit(){
    return demoService.req().execute().body();
}

简化一下

Retrofit 我们可以添加扩展来处理结果(代码由春哥提供):

/**
 * 原始类型返回的 处理器,去掉默认的 Call
 *
 * @author L.cm
 */
@Configuration(proxyBeanMethods = false)
public class ApiCallAdapterFactory extends CallAdapter.Factory {

    @Override
    public CallAdapter<!--?, ?--> get(@Nonnull Type returnType,
                                 @Nonnull Annotation[] annotations,
                                 @Nonnull Retrofit retrofit) {
        return new CallAdapter&lt;&gt;() {
            @Override
            public Type responseType() {
                return returnType;
            }

            @Override
            public Object adapt(@Nonnull Call<object> call) {
                try {
                    return call.execute().body();
                } catch (IOException e) {
                    // 自定义fallback、或者直接抛出给全局异常处理
                    return null;
                }
            }
        };
    }

}

然后我们上面的代码就可以简化成这样啦:

@RetrofitClient("square-provider")
public interface DemoService {

    @GET("/req")
    String req();
}
@Autowired
DemoService demoService;

@SneakyThrows
@GetMapping("/retrofit")
public String retrofit(){
    return demoService.req();
}

总结

    1. 由于 spring-cloud-square 直接基于 retrofit 实现,整体源码非常的简单,推荐大家一读。
    1. 目前版本暂未实现 fallback 相关的实现。
    1. 本文的配套代码 https://github.com/lltx/spring-cloud-square-demo </object></string>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring Cloud Square 是什么
  • 快速上手
    • 添加依赖
      • 代码配置
        • 代码调用
        • 进阶使用
          • 添加依赖
            • 声明调用客户端
              • 开启客户端扫描
                • 代码调用
                • 简化一下
                • 总结
                相关产品与服务
                负载均衡
                负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档