Add simple custom zuul filter example
This commit is contained in:
6
ribbon-default-config/resources/application.yml
Normal file
6
ribbon-default-config/resources/application.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
application:
|
||||
name: standalone
|
||||
|
||||
server:
|
||||
port: 7777
|
||||
@@ -1,14 +1,64 @@
|
||||
package demo;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulServer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableZuulServer
|
||||
public class ZuulApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZuulApplication.class, args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZuulApplication.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
@Component
|
||||
class SampleStaticResponseFilter extends ZuulFilter {
|
||||
|
||||
private static final String URI = "/static";
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return "route";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
String path = RequestContext.getCurrentContext().getRequest().getRequestURI();
|
||||
if (checkPath(path))
|
||||
return true;
|
||||
if (checkPath("/" + path))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean checkPath(String path) {
|
||||
return URI.equals(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
// Set the default response code for static filters to be 200
|
||||
ctx.setResponseStatusCode(HttpServletResponse.SC_OK);
|
||||
// first StaticResponseFilter instance to match wins, others do not set body
|
||||
// and/or status
|
||||
if (ctx.getResponseBody() == null) {
|
||||
ctx.setResponseBody("Hello World");
|
||||
ctx.setSendZuulResponse(false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
spring.application.name: zuulproxy
|
||||
spring.application.name: zuul
|
||||
server.port: 9900
|
||||
zuul.routes.ui.path: /ui/**
|
||||
zuul.routes.ui.url: http://localhost:8080
|
||||
zuul.routes.ui.path: /static/**
|
||||
@@ -1,11 +1,14 @@
|
||||
package demo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.boot.test.WebIntegrationTest;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClient;
|
||||
@@ -15,18 +18,24 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ZuulApplication.class)
|
||||
@DirtiesContext
|
||||
@WebIntegrationTest(randomPort=true)
|
||||
@WebIntegrationTest(randomPort = true)
|
||||
public class ZuulApplicationTests {
|
||||
|
||||
@Autowired
|
||||
DiscoveryClient discoveryClient;
|
||||
|
||||
@Value("${local.server.port}")
|
||||
int port;
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
assertEquals("Hello World", new TestRestTemplate()
|
||||
.getForObject("http://localhost:" + this.port + "/static", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoveryClientIsNoop() {
|
||||
assertTrue("discoveryClient is wrong type", discoveryClient instanceof NoopDiscoveryClient);
|
||||
assertTrue("discoveryClient is wrong type",
|
||||
this.discoveryClient instanceof NoopDiscoveryClient);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user