grace-api
简介:一款http请求代理框架,能够将重复冗余,不好管理的http请求代码简化,提升开发效率。
特性:
1.以hutool http为后端框架
2.通过调用java接口的方式去发送Http请求, 实现了业务逻辑与Http协议之间的解耦
3.支持请求方法:GET, POST
4.支持文件上传
5.支持自定义拦截器
6.支持自动签名处理
7.支持缓存
8.支持自动序列化
9.支持异常降级处理
10.支持全局或者单个客户端端日志级别设置以及超时配置。
快速使用:
1.在pom.xml里面配置仓库地址
<repositories>
<repository>
<id>grace-project</id>
<url>https://gitee.com/graceful-code/maven/raw/master</url>
</repository>
</repositories>
2.引用依赖坐标
<dependency>
<groupId>com.grace</groupId>
<artifactId>grace-api</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
3.使用注解 @EnableGraceHttpProxyClients 开启http代理框架
@SpringBootApplication
@EnableGraceHttpProxyClients
public class GraceTestServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GraceTestServiceApplication.class, args);
}
}
4.定义http代理客户端接口
@GraceHttpClient(value = "test", fallback = HelloApiFallBack.class)
public interface HelloApi {
@GraceRequest(value = "/test", method = RequestMethod.POST)
Result hello(@RequestParam Integer code);
}
5.配置客户端IP
grace:
api:
client:
test:
endpoint: http://127.0.0.1:8088
timeout: 3000
这个test要和接口中 @GraceHttpClient注解里的value值对应起来,多个客户端接口可以使用相同的value,也可以每个客户端都不同。
6.业务调用
@Resource
private HelloApi helloApi;
@GetMapping("/hello")
public Result test(@RequestParam Integer code){
return helloApi.hello(code);
}
7.放弃原来复杂冗余并且不好维护http调用方式
String json = JSONObject.toJsonString(user);
HttpResponse httpResponse = HttpRequest.post("http://localhost:8088/api//user")
.body(json)
.timeout(3000)
.execute();
if (httpResponse.isOk()){
String res = httpResponse.body();
return JSONObject.parseObject(res, Result.class);
}
return Result.builder().code(500).build();