springboot使用zuul

Easter79
• 阅读 569

##配置pom

<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>

##zuul依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

##启用zuul

@SpringBootApplication
@EnableZuulProxy
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

##application.yaml

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内部的相对地址。 ##配置filter

@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;
    }
}

##docs

点赞
收藏
评论区
推荐文章
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
springboot2.1.3.RELEASE+jsp笔记war部署tomcat
springbootjsp<packagingwar</packaging<parent<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterparent</artifa
Easter79 Easter79
3年前
Springboot简单集成ActiveMQ
Springboot简单集成ActiveMQ消息发送者的实现pom.xml添加依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarteractivemq</artifactId</dependency<d
Stella981 Stella981
3年前
Spring Cloud Config Server
SpringCloudConfigServer<parent<groupIdorg.springframework.cloud</groupId<artifactIdspringcloudstarterparent</artifactId<versionAngel.SR4</version</parent<depe
Stella981 Stella981
3年前
Spring Cloud Config Client
SpringCloudConfigClient<parent<groupIdorg.springframework.cloud</groupId<artifactIdspringcloudstarterparent</artifactId<versionAngel.SR4</version</parent
Stella981 Stella981
3年前
SpringBoot 整合 Swagger2
1、加入pom文件<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarter</artifactId</de
Stella981 Stella981
3年前
Spring boot自定义parent POM
概述在之前的SpringBoot例子中,我们都会用到这样的parentPOM。<parent<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterparent</artifactId
Easter79 Easter79
3年前
SpringBoot2.0 配置Log4j2记录日志
pom.xml配置<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarter</artifactId<exclusions
Easter79 Easter79
3年前
Springboot2.x整合logback slf4j
Springboot项目的pom里引入的parent<parent<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterparent</artifactId<version2.0.0.RELE
Stella981 Stella981
3年前
SpringBoot2.0 配置Log4j2记录日志
pom.xml配置<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarter</artifactId<exclusions
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k