SpringCloud神兽(五)之Zuul

一. Zuul路由网关概述

Zuul包含了对请求的路由和过滤两个最主要的功能:

其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.

Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。

注意:Zuul服务最终还是会注册进Eureka

Zuul能干嘛:代理、路由、过滤

二. 路由基本配置

①新建模块microservicecloud-zuul-gateway-9527

②修改pom.xml文件

<dependencies>

  

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

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

  

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

  

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

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

  

  <dependency>
   <groupId>com.wanbangee.springcloudgroupId>
   <artifactId>microservicecloud-apiartifactId>
   <version>${project.version}version>
  dependency>

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

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

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

  

  <dependency>
   <groupId>org.springframeworkgroupId>
   <artifactId>springloadedartifactId>
  dependency>

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

 dependencies>

③新建yml文件

server: 

 port: 9527
 
spring: 
 application:
  name: microservicecloud-zuul-gateway

eureka: 
 client: 
  service-url: 
   defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka  
 instance:
  instance-id: gateway-9527.com
  prefer-ip-address: true 

info:
 app.name: wanbangee-microcloud
 company.name: www.wanbangee.com
 build.artifactId: $project.artifactId$
 build.version: $project.version$

④新建启动类

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

⑤测试

不用路由:http://localhost:8001/dept/list

启动路由:http://localhost:9527/mictoservicecloud-dept/dept/list

三. 路由访问映射规则

server: 
  port: 9527

spring: 
  application: 
    name: microservicecloud-zuul-gateway

eureka: 
  client: 
    service-url: 
      defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka,http://eureka7003:7003/eureka  
  instance: 
    instance-id: gateway-9527.com
    prefer-ip-address: true 

# zuul 的访问值只针对外部【如外部浏览器或者前端的异步请求访问需要符合zuul规则】,对内不做代理【微服务与微服务之间的调用不需使用zuul定义的规则】
zuul: 
  prefix: /nhkj #设置统一的前缀
  ignored-services: microservicecloud-dept # 忽略真实服务名称   ignored-services: "*"  #忽略所有的服务名称
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

info: 
  app.name: wanbangee-microcloud
  company.name: www.wanbangee.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

测试:

http://localhost:8001/dept/list

http://localhost:9527/nhkj/mydept/dept/list

你可能感兴趣的:(SpringCloud,zuul)