<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency>
@SpringBootApplication @EnableZuulProxy public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
zuul: routes: demo: path: /demo/** url: forward:/demo product: path: /product/** stripPrefix: false product: ribbon: listOfServers: 192.168.99.100:8080 ribbon: eureka: enabled: false
其中,使用listOfServers能夠配置外部服務的多個負載均衡;使用forward能夠forward到外部地址,也能夠forward內部的相對地址。git
@Component public class DemoFilter extends ZuulFilter { private static final Logger LOGGER = LoggerFactory.getLogger(DemoFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String token = request.getHeader("token"); if(StringUtils.isEmpty(token)){ requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value()); requestContext.setSendZuulResponse(false); throw new RuntimeException("UNAUTHORIZED"); } return null; } }
zuul-demogithub