Use of Springcloud-----Routing Gateway (Zuul)

Source: Internet
Author: User

The main function of Zuul is to route forwarding and filtering, for example, to forward all/api-a/* requests to Server A, all/api-b/* requests are forwarded to Server B, zuul default and ribbon combine to achieve load balancing function.

1 Zuul Routing and forwarding

1.1 Creating a Springboot project, introducing dependent dependencies

<Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelversion>4.0.0</modelversion>    <groupId>Com.beifeng.hadoop</groupId>    <Artifactid>Beifeng-spring-cloud-zuul</Artifactid>    <version>0.0.1-snapshot</version>    <Packaging>Jar</Packaging>    <name>Beifeng-spring-cloud-zuul</name>    <URL>http://maven.apache.org</URL>        <Parent>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>1.5.2.RELEASE</version>        <RelativePath/>    </Parent>    <Properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <java.version>1.8</java.version>    </Properties>    <Dependencies>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid>            <Scope>Test</Scope>        </Dependency>        <!--declaring a Web project -        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>        <!--Configure Eureka -        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-eureka</Artifactid>        </Dependency>                <!--Configure Zuul -        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-zuul</Artifactid>        </Dependency>    </Dependencies>        <dependencymanagement>        <Dependencies>            <Dependency>                <groupId>Org.springframework.cloud</groupId>                <Artifactid>Spring-cloud-dependencies</Artifactid>                <version>Dalston.rc1</version>                <type>Pom</type>                <Scope>Import</Scope>            </Dependency>        </Dependencies>    </dependencymanagement>    <repositories>        <Repository>            <ID>Spring-milestones</ID>            <name>Spring Milestones</name>            <URL>Https://repo.spring.io/milestone</URL>            <Snapshots>                <enabled>False</enabled>            </Snapshots>        </Repository>    </repositories></Project>

1.2 Configuring the Routing forwarding policy in the configuration file

Eureka:  Client:   serviceurl:    defaultzone:http://localhost:8761/eureka/#注册服务器地址server:  Port: 8766spring:  application:    Name:cloud-zuulzuul:  routes:   api-a:    path:/api-a/** #所有的/ Api-a the beginning of the request is forwarded to the Cloud-consumer-ribbon server   serviceid:cloud-consumer-ribbon  api-b:    path:/api-b/** # All requests beginning with/API-B are forwarded to the Cloud-consumer-feign server   serviceid:cloud-consumer-feign

1.3 Declaring enable Zuul in the Startup class

@SpringBootApplication @enablezuulproxy@enableeurekaclient  Public class Cloudzuul {    publicstaticvoid  main (string[] args) {        Springapplication.run (Cloudzuul. class , args);}    }

1.4 Start Viewing results

Start the Eureka-server, Eureka-client, Cloud-consumer-ribbon, Cloud-consumer-feign, Cloud-zuul projects in turn

    

2 Filtering of Routes

2.1 Create a custom route filter that specifies the requests to filter and how to filter

@Component Public classMyzuulfilterextendszuulfilter {Logger Logger=loggerfactory.getlogger (Myzuulfilter.class); //specific logic of the filter     PublicObject Run () {RequestContext context=Requestcontext.getcurrentcontext (); HttpServletRequest Request=context.getrequest (); Logger.info ("URL:" +Request.getrequesturl (). toString ()); Object token=request.getparameter ("token"); if(token==NULL||Stringutils.isempty (token.tostring ())) {Logger.info ("Token is empty"); Context.setsendzuulresponse (false); Context.setresponsestatuscode (401); Try{context.getresponse (). Getwriter (). Write ("Token is empty"); } Catch(IOException e) {e.printstacktrace (); }            return NULL; } logger.info ("OK"); return NULL; }    //whether to filter, can write logical judgment     Public BooleanShouldfilter () {RequestContext context=Requestcontext.getcurrentcontext (); HttpServletRequest Request=context.getrequest (); String URL=Request.getrequesturl (). toString (); returnUrl.contains ("/api-a/");//verification of Api-a requests only    }    //Order of filtering@Override Public intFilterorder () {return0; }    //returns the type of filter, pre: Before routing, routing: When routing, post: After routing, error: Called when a fault occurs@Override PublicString FilterType () {return"Pre"; }}

2.2 Starting the viewing effect

Use of Springcloud-----Routing Gateway (Zuul)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.