搭建springcloud的demo之zuul网关(十一)

zuul

zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能复杂将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础,而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础,Zuul和Eureka进行整个,将zuul自身注册为eureka服务治理下的应用,同时从eureka中获得其他微服务的消息,也即以后的访问微服务都是通过zuul跳转后获得;

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

提供=代理+路由+过滤三大功能

路由的基本配置

新建microservicecloud-zuul-gateway-9527

pom.xml

搭建springcloud的demo之zuul网关(十一)_第1张图片


  4.0.0
  
    com.yt.springcloud
    microservicecloud
    0.0.1-SNAPSHOT
  
  microservicecloud-zuul-gateway-9527

	
		
		
			org.springframework.cloud
			spring-cloud-starter-zuul
		
		
			org.springframework.cloud
			spring-cloud-starter-eureka
		
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
		
			org.springframework.cloud
			spring-cloud-starter-hystrix
		
		
			org.springframework.cloud
			spring-cloud-starter-config
		
		
		
			com.yt.springcloud
			microservicecloud-api
			${project.version}
		
		
			org.springframework.boot
			spring-boot-starter-jetty
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
		
		
		
			org.springframework
			springloaded
		
		
			org.springframework.boot
			spring-boot-devtools
		
	




application.yml

搭建springcloud的demo之zuul网关(十一)_第2张图片

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway #注册eureka的名字
 
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 #改hosts文件
    prefer-ip-address: true 
 

 
info: #可写可不写
  app.name: atguigu-microcloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$


hosts修改

hosts文件的位置在C:\Windows\System32\drivers\etc\hosts
搭建springcloud的demo之zuul网关(十一)_第3张图片

127.0.0.1       eureka7001.com
127.0.0.1       eureka7002.com
127.0.0.1       eureka7003.com
127.0.0.1       myzuul.com

主启动类

搭建springcloud的demo之zuul网关(十一)_第4张图片

package com.yt.springcloud;

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

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

启动

  1. 三个eureka集群
  2. 一个服务提供类microservicecloud-provider-dept-8001
  3. 一个路由

测试

网关和微服务注册进入eureka集群:http://localhost:7001/
搭建springcloud的demo之zuul网关(十一)_第5张图片
不使用路由:http://localhost:8001/dept/get/1
搭建springcloud的demo之zuul网关(十一)_第6张图片
使用路由:http://myzuul.com:9527/microservicecloud-dept/dept/get/1
microservicecloud-dept是微服务名称

搭建springcloud的demo之zuul网关(十一)_第7张图片

路由访问映射规则

修改application.yml

搭建springcloud的demo之zuul网关(十一)_第8张图片

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway #注册eureka的名字
 
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 #改hosts文件
    prefer-ip-address: true 
 
 
zuul: 
  routes: 
    mydept.serviceId: microservicecloud-dept  #key 以前是输入微服务名称
    mydept.path: /mydept/**                   #value  现在是映射到mydept(把真实微服务名称隐藏起来)
 
info: #可写可不写
  app.name: atguigu-microcloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$


测试:http://myzuul.com:9527/mydept/dept/get/1

搭建springcloud的demo之zuul网关(十一)_第9张图片
搭建springcloud的demo之zuul网关(十一)_第10张图片

只让路由映射的mydept可以访问!!!

编写application.yml

搭建springcloud的demo之zuul网关(十一)_第11张图片

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway #注册eureka的名字
 
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 #改hosts文件
    prefer-ip-address: true 
 
 
zuul: 
  ignored-services: microservicecloud-dept
  routes: 
    mydept.serviceId: microservicecloud-dept  #key 以前是输入微服务名称
    mydept.path: /mydept/**                   #value  现在是映射到mydept(把真实微服务名称隐藏起来)
 
info: #可写可不写
  app.name: atguigu-microcloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$


访问测试

搭建springcloud的demo之zuul网关(十一)_第12张图片
搭建springcloud的demo之zuul网关(十一)_第13张图片

单个写具体微服务名称,多个可以用"*"

修改application.yml

搭建springcloud的demo之zuul网关(十一)_第14张图片

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway #注册eureka的名字
 
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 #改hosts文件
    prefer-ip-address: true 
 
 
zuul: 
  #ignored-services: microservicecloud-dept 具体某一个微服务的名称
  ignored-services: "*"  #所有微服务,批量处理
  routes: 
    mydept.serviceId: microservicecloud-dept  #key 以前是输入微服务名称
    mydept.path: /mydept/**                   #value  现在是映射到mydept(把真实微服务名称隐藏起来)
 
info: #可写可不写
  app.name: atguigu-microcloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$


设置统一公共前缀

修改application.yml

搭建springcloud的demo之zuul网关(十一)_第15张图片

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway #注册eureka的名字
 
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 #改hosts文件
    prefer-ip-address: true 
 
 
zuul: 
  #ignored-services: microservicecloud-dept 具体某一个微服务的名称
  prefix: /yt  #统一前缀名         
  ignored-services: "*"  #所有微服务,批量处理
  routes: 
    mydept.serviceId: microservicecloud-dept  #key 以前是输入微服务名称
    mydept.path: /mydept/**                   #value  现在是映射到mydept(把真实微服务名称隐藏起来)
 
info: #可写可不写
  app.name: atguigu-microcloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$


测试

搭建springcloud的demo之zuul网关(十一)_第16张图片
搭建springcloud的demo之zuul网关(十一)_第17张图片

总结

搭建springcloud的demo之zuul网关(十一)_第18张图片

翻译于尚硅谷周阳老师:https://www.bilibili.com/video/av22613028?from=search&seid=11377756339718254280

你可能感兴趣的:(springcloud)