Dubbo là một framework dịch vụ phân tán mã nguồn mở được sử dụng rộng rãi bởi nhiều công ty internet trong nước, và nhìn từ góc độ quốc tế thì đây cũng là một framework SOA toàn diện. Là một chủ đề nghiên cứu kỹ thuật quan trọng, tại Dangdang chúng tôi đã phát triển thêm một số tính năng mới cho Dubbo dựa trên nhu cầu nội bộ và đặt tên cho phiên bản này là Dubbox (viết tắt của Dubbo eXtensions)
Các tính năng chính của Dubbox:
- Hỗ trợ gọi từ xa theo phong cách REST (HTTP + JSON/XML)
- Hỗ trợ các cơ chế сериализation Java hiệu suất cao dựa trên Kryo và FST
- Hỗ trợ hệ thống HTTP remoting tích hợp Tomcat
- Nâng cấp Spring
- Nâng cấp client ZooKeeper
Chi tiết có thể tham khảo: https://www.oschina.net/p/dubbox
Trong quá trình phát triển, đôi khi cần giới hạn quyền truy cập và danh sách trắng là một phương pháp hiệu quả. Đối với ứng dụng Java Web, bộ lọc của Spring có thể chặn các cuộc gọi giao diện Web; tuy nhiên đối với giao diện dubbo, bộ lọc của Spring không còn hiệu lực. Dubbo cung cấp cơ chế mở rộng Filter, bạn có thể tự định nghĩa Filter để đạt được chức năng này.
1. Triển khai Filter tùy chỉnh
public class AuthenticationFilter implements Filter {
private AuthService authService = new DefaultAuthServiceImpl();
private String unauthorizedRedirectUrl = "http://127.0.0.1:5432/auth/error";
@Override
public Result execute(Invoker<?> target, Invocation methodCall) throws RpcException {
HttpServletRequest httpRequest = (HttpServletRequest) RpcContext.getContext().getRequest();
HttpServletResponse httpResponse = (HttpServletResponse) RpcContext.getContext().getResponse();
String accessToken = httpRequest.getParameter("authToken");
boolean isValid = authService.verifyAccess(accessToken);
if (isValid) {
return target.invoke(methodCall);
} else {
ApiResponse errorResponse = new ApiResponse();
errorResponse.setCode("AUTH_401");
errorResponse.setPayload(null);
sendResponse(JsonConverter.toJson(errorResponse), httpResponse);
return new RpcResult();
}
}
private void sendResponse(String content, HttpServletResponse response) {
PrintWriter output = null;
try {
response.setContentType("application/json; charset=utf-8");
output = response.getWriter();
output.write(content);
} catch (IOException e) {
// Xử lý ngoại lệ
} finally {
if (output != null) {
output.close();
}
}
}
public AuthService getAuthService() {
return authService;
}
public void setAuthService(AuthService service) {
this.authService = service;
}
public String getErrorRedirectUrl() {
return unauthorizedRedirectUrl;
}
public void setErrorRedirectUrl(String url) {
this.unauthorizedRedirectUrl = url;
}
}
2. Cấu hình triển khai
Tạo tệp văn bản đơn giản tại thư mục resources: META-INF/dubbo/com.alibaba.dubbo.rpc.Filter
Nội dung tệp:
authFilter=com.example.dubbo.security.AuthenticationFilter
Sau đó cấu hình bộ lọc này cho giao diện trong file cấu hình:
<dubbo:protocol id="apiProtocol" name="rest" port="3000"/>
<dubbo:service interface="com.example.api.user.UserApi"
ref="userServiceImpl"
version="2.0"
group="user-service"
protocol="apiProtocol"
filter="authFilter"
timeout="30000" />