SpringBoot整合Forest

过去的,未来的
2021-05-08 / 0 评论 / 0 点赞 / 1,148 阅读 / 1,350 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2021-05-08,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

前言

Forest是一个高层的、极简的轻量级HTTP调用API框架。
相比于直接使用Httpclient您不再用写一大堆重复的代码了,而是像调用本地方法一样去发送HTTP请求。

一、加入依赖

<dependency>
    <groupId>com.dtflys.forest</groupId>
    <artifactId>spring-boot-starter-forest</artifactId>
    <version>1.3.8</version>
</dependency>

二、配置后端 HTTP API-在SpringBoot配置文件application.yml

目前 Forest 支持okhttp3和httpclient两种后端 HTTP API,若不配置该属性,默认为okhttp3. 当然也可以改为httpclient

forest:
  bean-id: config0 # 在spring上下文中bean的id, 默认值为forestConfiguration
  backend: okhttp3 # 后端HTTP API: okhttp3
  max-connections: 1000 # 连接池最大连接数,默认值为500
  max-route-connections: 500 # 每个路由的最大连接数,默认值为500
  timeout: 3000 # 请求超时时间,单位为毫秒, 默认值为3000
  connect-timeout: 3000 # 连接超时时间,单位为毫秒, 默认值为2000
  retry-count: 1 # 请求失败后重试次数,默认为0次不重试
  ssl-protocol: SSLv3 # 单向验证的HTTPS的默认SSL协议,默认为SSLv3
  logEnabled: true # 打开或关闭日志,默认为true

三、在SpringBoot中开启Forest的包扫描进行动态代理

在SpringBoot启动类添加注解  @ForestScan(basePackages = "com.fcant.service.forest") 
@SpringBootApplication
@ForestScan(basePackages = "com.example.demo.forest")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

四、简单的例子

@Request 默认使用GET方式,且将请求响应的数据以String的方式返回给调用者。
public interface MyClient {
    @Request(url = "http://localhost:5000/hello")
    String simpleRequest();
}

来源:https://www.yuque.com/fcant/sys/cb86in
(有新版本)
https://gitee.com/dromara/forest

0

评论区