springcloud之Zuul
springcloud之Zuul
communication between a front-end application and a REST API that are deployed separately.
The goal is to work around CORS and the Same Origin Policy restriction of the browser and allow the UI to call the API even though they don't share the same origin.
Zuul is a JVM based router and server side load balancer by Netflix.
Dependency: spring-cloud-starter-netflix-zuul
Config properties:
zuul: |
API
port 8081
|
UI
|
Ajax call : /foos/:fooId
UI and API deployed separately.
Zuul Proxy
public class UiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(UiApplication.class, args);
}
}
|
Zuul Filter
Eg post filter
public class ResponseLogFilter extends ZuulFilter {
public String filterType() {
return POST_TYPE;
}
public int filterOrder() {
return 0;
}
public boolean shouldFilter() {
return true;
}
public Object run() throws ZuulException {
return null;
}
}
// modify response
public Object run() throws ZuulException {
RequestContext context = RequestContext.getCurrentContext();
try (final InputStream responseDataStream = context.getResponseDataStream()) {
if(responseDataStream == null) {
logger.info("BODY: {}", "");
return null;
}
String responseData = CharStreams.toString(new InputStreamReader(responseDataStream, "UTF-8"));
logger.info("BODY: {}", responseData);
context.setResponseBody(responseData);
}
catch (Exception e) {
throw new ZuulException(e, INTERNAL_SERVER_ERROR.value(), e.getMessage());
}
return null;
}
public class ResponseLogFilter extends ZuulFilter { |
Zuul Rate limit
|
zuul: |
Notes: Zuul.ratelimit.policy-list
Rate limit of 5 requests per 60 seconds for the serviceSimple endpoint and ate limit of 1 request per 2 seconds
Type
:
origin – rate limit based on the user origin request
url – rate limit based on the request path of the downstream service
user – rate limit based on the authenticated username or ‘anonymous'
Fra
https://www.baeldung.com/spring-rest-with-zuul-proxy