API网关服务:Spring Cloud Zuul(六)使用笔记

zuul应用端

在spring-cloud-demo工程中添加module:zuul-app
在pom.xml文件中加入依赖:

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

在resource目录下增加application.properties文件,添加如下内容:

server.port=8080
spring.application.name=zuul-app
eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/

zuul.routes.zuul-use.path=/api/**
zuul.routes.zuul-use.serviceId=zuul-use

在java目录下创建一个SpringBoot项目启动类:ZuulApplication,内容如下:

/**
 * @CalssName ZuulApplication
 * @Description TODO
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulApplication {

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

zuul使用端

在spring-cloud-demo工程中添加module:zuul-use
在pom.xml文件中加入依赖:

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

在resource目录下增加application.properties文件,添加如下内容:

server.port=8903
spring.application.name=zuul-use
eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/

在java目录下创建一个SpringBoot项目启动类:ZuulUseApplication,内容如下:

/**
 * @CalssName ZuulUseApplication
 * @Description TODO
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ZuulUseApplication {

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

创建一个Controller:HelloController,内容如下:

/**
 * @CalssName controller
 * @Description TODO
 */
@RestController
public class HelloController {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello,I'm from zuul-use...";
    }
}

以此启动Eureka服务端、zuul-app和zuul-use,浏览器访问:http://localhost:8080/api/hello,输出如下:

hello,I'm from zuul-use...

说明zuul应用成功

Zuul高级使用

Zuul过滤器的使用示例,以token过滤为例,在zuul-app下建TokenFilter过滤器,内容如下:

/**
 * @CalssName TokenFilter
 * @Description Token Zuul过滤器
 * @Author Alyshen
 */
public class TokenFilter extends ZuulFilter {
    private final Logger logger = LoggerFactory.getLogger(TokenFilter.class);

    @Override
    public String filterType() {
        return "pre"; // 可以在请求被路由之前调用
    }

    @Override
    public int filterOrder() {
        return 0; // filter执行顺序,通过数字指定 ,优先级为0,数字越大,优先级越低
    }

    @Override
    public boolean shouldFilter() {
        return true;// 是否执行该过滤器,此处为true,说明需要过滤
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        logger.info("--->>> TokenFilter {},{}", request.getMethod(), request.getRequestURL().toString());

        String token = request.getParameter("token");// 获取请求的参数

        if (StringUtils.isNotBlank(token)) {
            ctx.setSendZuulResponse(true); //对请求进行路由
            ctx.setResponseStatusCode(200);
            ctx.set("isSuccess", true);
            return null;
        } else {
            ctx.setSendZuulResponse(false); //不对其进行路由
            ctx.setResponseStatusCode(400);
            ctx.setResponseBody("token is empty");
            ctx.set("isSuccess", false);
            return null;
        }
    }
}

在zuul-app的启动类下加如下方法:

@Bean
public TokenFilter tokenFilter() {
    return new TokenFilter();
}

猜你喜欢

转载自www.cnblogs.com/yhongyin/p/11183870.html
今日推荐