zoukankan      html  css  js  c++  java
  • SpringCloud路由网关Zuul

    什么是网关

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

    依赖

      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
      </parent>
        <!--eureka依赖-->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
      </dependencies>
    
      <dependencyManagement>
        <dependencies>
          <!--springCloud依赖-->
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>

    配置文件application.yml

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

    启动类StartZuul

    package com.cloud;
    
    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;
    
    @EnableEurekaClient
    @SpringBootApplication
    @EnableZuulProxy
    public class StartZuul {
        public static void main(String[] args) {
            SpringApplication.run(StartZuul.class,args);
        }
    }

    效果展示

  • 相关阅读:
    jperf windows
    Eclipse+Maven命令创建webapp项目<三>
    Eclipse+Maven创建webapp项目<二>
    Eclipse+Maven创建webapp项目<一>
    在java中定义有几种类加载器
    JAVA创建对象有哪几种方式 ?
    js创建对象的几种常用方式小结(推荐)
    maven安装以及eclipse配置maven
    MyEclipse 10.0安装及激活步骤
    jdk下载网址
  • 原文地址:https://www.cnblogs.com/dabrk/p/12071986.html
Copyright © 2011-2022 走看看