SpringCloud Zuul Demo

原文地址:https://spring.io/guides/gs/routing-and-filtering/

第一步:创建微服务

创建时勾选web组件即可。具体pom如下:



    4.0.0

    com.example
    book
    0.0.1-SNAPSHOT
    jar

    book
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.0.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

修改Application类

package com.example.book;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class BookApplication {

    @RequestMapping(value = "/available")
    public String available() {
        return "Spring in Action";
    }

    @RequestMapping(value = "/checked-out")
    public String checkedOut() {
        return "Spring Boot in Action";
    }

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

修改applicaton.properties

spring.application.name=book
server.port=8090

这样一个简单的restful微服务就创建好了,启动之后输入地址http://localhost:8090/available,可以看到如下页面:

image.png

第二步 创建网关项目

还是先创建一个springboot项目,然后修改pom文件



    4.0.0

    com.example
    zuuldemo
    0.0.1-SNAPSHOT
    jar

    ZuulDemo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.5.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        8.0.50
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.cloud
            spring-cloud-starter-zuul
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Brixton.SR3
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

修改Application类

package com.example.zuuldemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableZuulProxy
@ComponentScan(value = { "com.example.zuuldemo" })
public class ZuulDemoApplication {

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

修改application.properties

spring.application.name=gateway
server.port=8888
ribbon.eureka.enabled=false

zuul.routes.books.url=http://localhost:8090

创建ZuulFilter类,里面的方法说明可以参考原文

package com.example.zuuldemo.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class DemoFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

        return null;
    }

    private static Logger log = LoggerFactory.getLogger(DemoFilter.class);
}

至此,网关项目也搭建完成了。
启动项目,访问这个地址:http://localhost:8888/books/available,跳转到了book项目

image.png

至此,demo成功。比较简单的一个demo,照着官网做就可以了~后续可以考虑带通配符的网关配置,这样在公司级别的nginx只需要配置这个网关项目的地址就可以,其他的微服务地址可以自己进行配置,方便了很多。

你可能感兴趣的:(SpringCloud Zuul Demo)