springcloud-路由网关(zuul)

一、什么是网关

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能, 类似nginx转发。

二、创建service项目

略参考上面博客

三、创建生产者项目 springcloud_eureka_member并启动

1 导入pom文件节点

 <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.1.4.RELEASEversion>
  parent>

<dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-openfeignartifactId>
    dependency>

<dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-netflix-zuulartifactId>
    dependency>


<dependencyManagement>
    <dependencies>
      
      <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-dependenciesartifactId>
        <version>Greenwich.RELEASEversion>
        <type>pomtype>
        <scope>importscope>
      dependency>
    dependencies>
  dependencyManagement>

 

2 创建application.yml文件

  ##应用名称
  spring:
    application:
      name: member-server

      #声明当前 eurakaserver端口号
  server:
    port: 9001
  #配置eureka
  eureka:
    client:


      #配置地址
      service-url:
        defaultZone: http://localhost:8888/eureka

 

 

3 创建service层接口

public interface MemberService {
    public String getMember();
}

 

 

4 创建service层接口实现类

@RestController
public class MemberServiceImpl implements MemberService {

    @RequestMapping("/member")
    @Override
    public String getMember() {
        System.out.println("member getMember方法");
        return "member";
    }
}

 

 

5  创建启动程序类StartProvider

@SpringBootApplication
@EnableEurekaClient
public class StartProvider {
    public static void main(String[] args) {
        SpringApplication.run(StartProvider.class,args);
    }

 

 

 

6 运行效果

springcloud-路由网关(zuul)_第1张图片

 

 

四、创建消费者项目 springcloud_eureka_order

1 创建application.yml文件

  ##应用名称
  spring:
    application:
      name: order-server

      #声明当前 eurakaserver端口号
  server:
    port: 9002
  #配置eureka
  eureka:
    client:
      #配置地址
      service-url:
        defaultZone: http://localhost:8888/eureka

 

2 创建service层接口

@FeignClient("member-server")
public interface OrderService {

    @RequestMapping("/member")
    public String getOrder();
}

 

 

3 创建启动程序类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class StartProvider {
    public static void main(String[] args) {
        SpringApplication.run(StartProvider.class,args);
    }

 

 

4 运行效果

springcloud-路由网关(zuul)_第2张图片

 

springcloud-路由网关(zuul)_第3张图片

 

 

springcloud-路由网关(zuul)_第4张图片

 

 

 

 

 

 五、创建项目springcloud_zuul

1 创建application.yml文件

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka
server:
  port: 10001
spring:
  application:
    name: zuul
##配置zuul网关
zuul:
  routes:
    api-a:
      path: /member-zuul/**
      service-id: member-server
    api-b:
      path: /order-zuul/**
      service-id: order-server

 

 

2 创建启动程序类

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class StartZuul {
    public static void main(String[] args) {
        SpringApplication.run(StartZuul.class,args);
    }
}

 

 

3  运行效果

springcloud-路由网关(zuul)_第5张图片

 

springcloud-路由网关(zuul)_第6张图片

 

 

4 创建服务过滤类 

 

@Component
public class MyZuulFilter extends ZuulFilter {
    /**
     * 过滤类型
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

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

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

    @Override
    public Object run() throws ZuulException {
        //获取token参数
        RequestContext rc = RequestContext.getCurrentContext();
        HttpServletRequest request = rc.getRequest();

        String token = request.getParameter("token");
        if(token!=null){
            return null;
        }
        //表示不进行路由
        rc.setSendZuulResponse(false);
        rc.setResponseStatusCode(401);

        try {
            rc.getResponse().setContentType("text/html;charset=utf-8");
            rc.getResponse().getWriter().write("没有传递正确参数,请校验后重试~");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

 

5 运行效果当加上token时

springcloud-路由网关(zuul)_第7张图片

 

当没有token时

springcloud-路由网关(zuul)_第8张图片

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