响应式编程,对于普通前端而言其实没有太大变化,该等多长时间还是要等多长时间,更多的是用在后台服务间的调用上。
先写个小demo
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiServer {
String value() default "";
}
@ApiServer("http://localhost:8080/user")
public interface IUserApi {
@GetMapping("/")
Flux<ClientUser> getAllUser();
@GetMapping("/{id}")
Mono<ClientUser> getUserById(@PathVariable String id);
@DeleteMapping("/{id}")
Mono<Void> deleteUserById();
@PostMapping("/")
Mono<ClientUser> createUser(@RequestBody Mono<ClientUser> user);
}
@RestController
public class TestController {
@Autowired
IUserApi userApi;
@GetMapping("/")
public void test(){
Flux<ClientUser> users = userApi.getAllUser();
users.subscribe(System.out::println);
}
}
就是做转发到服务端。
当然url调用方式有很多,接下来上完整框架
/**
* 方法调用信息类
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MethodInfo {
private String url;
private HttpMethod method;
private Map<String , Object> params;
private Mono body;
private Class<?> bodyElementType;
//返回是flux还是mono
private boolean returnFlux;
//返回对象类型
private Class<?> returnElementType;
}
/**
* 服务器信息类
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ServerInfo {
private String url;
}
/**
* 代理类接口
*/
public interface ProxyCreator {
//创建代理类
Object createProxy(Class<?> type);
}
/**
* rest请求调用handler
*/
public interface RestHandler {
//初始化服务器信息
void init(ServerInfo serverInfo);
//调用rest请求,返回结果
Object invokeRest(MethodInfo methodInfo);
}
/**
* 使用jdk动态代理实现代理类
*/
&