Spring Cloud路由网关(zuul)

微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作。共同组建了一个简单的微服务系统。

创建一个zuul工程new-module-Spring Initializr-next-修改Group和Artifact-

Spring Cloud路由网关(zuul)_第1张图片

接着next-finish。之后是

Spring Cloud路由网关(zuul)_第2张图片

其中pom添加


			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		

之后是



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.7.RELEASE
		 
	
	com.zuul
	zuul
	0.0.1-SNAPSHOT
	zuul
	Demo project for Spring Boot

	
		1.8
		Greenwich.SR2
	

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		

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

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

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

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


application.yml


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8760/eureka/
server:
  port: 8769
spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: service-customer
    api-b:
      path: /api-b/**
      serviceId: service-feign

ZuulApplication.java

package com.zuul.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulApplication {

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

}

启动服务注册中心,service-consumer,service-feign及service-producer服务

Spring Cloud路由网关(zuul)_第3张图片

Spring Cloud路由网关(zuul)_第4张图片

Spring Cloud路由网关(zuul)_第5张图片

Spring Cloud路由网关(zuul)_第6张图片

之后启动

Spring Cloud路由网关(zuul)_第7张图片

接下来浏览器访问

http://localhost:8769/api-a/hello?name=meng

Spring Cloud路由网关(zuul)_第8张图片

访问http://localhost:8769/api-b/sayHello?name=xia

上诉浏览器返回结果可以说明zuul起到了路由的作用

zuul不仅可以路由,还能过滤,做一些安全验证

添加Myfilter

package com.zuul.zuul.component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author zhaomengxia
 * @create 2019/8/28 11:45
 */
@Component
public class MyFilter extends ZuulFilter {
    /**
     * filterType返回一个字符串代表过滤器的类型,在zuul定义了四种不同的生命周期的过滤器类型
     * pre指路由之前
     * routing路由之后
     * post路由之后
     * error发送错误调用
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * filterOrder过滤的顺序
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * shouldFilter这里可以写逻辑判断,是否过滤,本文位true,永远过滤
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤器的具体逻辑,可以很复杂,包括sql、nosql去判断请求到底有没有权限访问
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {

        RequestContext context= RequestContext.getCurrentContext();

        HttpServletRequest request=context.getRequest();

        Object accessToken=request.getParameter("token");
        if (accessToken==null){
            context.setSendZuulResponse(false);
            context.setResponseStatusCode(401);
            try {
                context.getResponse().getWriter().write("token is empty");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}

重新启动ZuulApplication

再在浏览器访问http://localhost:8769/api-a/hello?name=meng

访问http://localhost:8769/api-b/sayHello?name=xia

浏览器访问http://localhost:8769/api-a/hello?name=meng&token=1

浏览器访问http://localhost:8769/api-b/sayHello?name=xia&token=12467621(token的值不为空即可)

源代码

https://github.com/zhaomengxia/springboot_mybatis.git

你可能感兴趣的:(springcloud,路由网关zuul)