微服务除了在内部相互调用,有时某些服务也会提供给外部应用。当然不能让外部应用也加入到“大家庭”里,毕竟知人知面不知源码,我们可以派出一个“前台”去接待它们,这就是“网关”。
网关负责对接外部来宾,因此要做好安全措施,什么登陆、权限该上就上。
流程如下:
@RestController
@RequestMapping("/api")
public class ApiController {
/**
* 动态调用rpc服务的关键,在acuprpc-spring-boot-starter中已经生成,可以随时引用
*/
private RpcClientManager rpcClientManager;
public ApiController(RpcClientManager rpcClientManager) {
this.rpcClientManager = rpcClientManager;
}
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public Object invoke(@RequestBody RpcRequestDTO requestDTO) {
RpcServiceInfo serviceInfo = new RpcServiceInfo(requestDTO.getApp(), requestDTO.getService());
RpcClient client = rpcClientManager.lookup(serviceInfo);//获取一个可以提供所需服务的连接
RpcRequest request = new RpcRequest(requestDTO.getApp(), requestDTO.getService(), requestDTO.getMethod());
if (requestDTO.getParameters() != null) {
Map<String, String> map = new HashMap<>();
requestDTO.getParameters().forEach((k, v) -> map.put(k, JsonUtil.toJson(v)));
request.setNamedParameter(map);
}
return client.invoke(request);//调用服务获得返回的json字符串
}
}
这个demo作为一个子模块(acuprpc-spring-boot-starter-gateway)加入了框架的全家桶,直接引入依赖就能使用这个功能。
基于这个方法,可以实现更加复杂的也无需求,这里就不细讲了,本系列结束。
关注我查看本系列其他内容:
1:服务注册/发现(eureka)
2:远程服务调用(grpc)
3:远程服务调用(thrift)
4:request filter
5:服务监控和管理(actuator)
6:调用异常节点自动重试
7:网关支持(gateway)