springboot使用zuul

##配置pom

1<parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.3.5.RELEASE</version> 5 <relativePath /> <!-- lookup parent from repository --> 6 </parent> 7<dependencyManagement> 8 <dependencies> 9 <dependency> 10 <groupId>org.springframework.cloud</groupId> 11 <artifactId>spring-cloud-dependencies</artifactId> 12 <version>Brixton.SR3</version> 13 <type>pom</type> 14 <scope>import</scope> 15 </dependency> 16 </dependencies> 17 </dependencyManagement> 18

##zuul依赖

1<dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-zuul</artifactId> 4 </dependency>

##启用zuul

1@SpringBootApplication 2@EnableZuulProxy 3public class DemoApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(DemoApplication.class, args); 7 } 8}

##application.yaml

1zuul: 2 routes: 3 demo: 4 path: /demo/** 5 url: forward:/demo 6 product: 7 path: /product/** 8 stripPrefix: false 9 10product: 11 ribbon: 12 listOfServers: 192.168.99.100:8080 13 14ribbon: 15 eureka: 16 enabled: false

其中,使用listOfServers可以配置外部服务的多个负载均衡;使用forward可以forward到外部地址,也可以forward内部的相对地址。 ##配置filter

1@Component 2public class DemoFilter extends ZuulFilter { 3 4 private static final Logger LOGGER = LoggerFactory.getLogger(DemoFilter.class); 5 6 @Override 7 public String filterType() { 8 return "pre"; 9 } 10 11 @Override 12 public int filterOrder() { 13 return 0; 14 } 15 16 @Override 17 public boolean shouldFilter() { 18 return true; 19 } 20 21 @Override 22 public Object run() { 23 RequestContext ctx = RequestContext.getCurrentContext(); 24 HttpServletRequest request = ctx.getRequest(); 25 String token = request.getHeader("token"); 26 if(StringUtils.isEmpty(token)){ 27 requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value()); 28 requestContext.setSendZuulResponse(false); 29 throw new RuntimeException("UNAUTHORIZED"); 30 } 31 return null; 32 } 33}

##docs

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

springboot2.1.3.RELEASE+jsp笔记war部署tomcat

springbootjsp<packagingwar</packaging<parent<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterparent</artifa

Springboot简单集成ActiveMQ

Springboot简单集成ActiveMQ消息发送者的实现pom.xml添加依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarteractivemq</artifactId</dependency<d

Spring Cloud Config Server

SpringCloudConfigServer<parent<groupIdorg.springframework.cloud</groupId<artifactIdspringcloudstarterparent</artifactId<versionAngel.SR4</version</parent<depe

Spring Cloud Config Client

SpringCloudConfigClient<parent<groupIdorg.springframework.cloud</groupId<artifactIdspringcloudstarterparent</artifactId<versionAngel.SR4</version</parent

SpringBoot 整合 Swagger2

1、加入pom文件<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarter</artifactId</de