微服务架构--SpringCloud(8)

Zuul 路由网关

 

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

*其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问同意入口的基础而过滤功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础,Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务智治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问问服务都是通过Zuul跳转后获得。

*过滤、保护 隐藏微服务名字 加一层 这一层就算是网关

*外部访问统一接口的网关

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

 

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

 

*官网资料:https://github.com/Netflix/zuul/wiki/Getting-Started

 

*路由基本配置:

*1.新建microservicecloud-zuul-gateway-9527

*2.修改pom

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

 

microservicecloud-zuul-gateway-9527

 

com.*.springcloud

microservicecloud

0.0.1-SNAPSHOT

 

 

org.springframework

springloaded

 

org.springframework.boot

spring-boot-devtools

 

org.springframework.cloud

spring-cloud-starter-eureka

 

com.*.springcloud

microservice-api

${project.version}

 

org.springframework.boot

spring-boot-starter-web

 

org.springframework.boot

spring-boot-starter-test

 

org.springframework.boot

spring-boot-starter-jetty

 

org.springframework.cloud

spring-cloud-starter-config

 

org.springframework.cloud

spring-cloud-starter-hystrix

 

org.springframework.boot

spring-boot-starter-actuator

 

org.springframework.cloud

spring-cloud-starter-eureka

 

org.springframework.cloud

spring-cloud-starter-zuul

 

 

 

 

*3.配置yml

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

prefer-ip-address: true

 

*4.修改hosts C:\Windows\System32\drivers\etc

*5.修改主启动类

@SpringBootApplication

@EnableZuulProxy

public class Zuul_9527_StartSpringCloudApp {

 

public static void main(String[] args) {

SpringApplication.run(Zuul_9527_StartSpringCloudApp.class,args);

}

 

}

 

 

6.测试

http://localhost:8001/dept/get/3

http://myzuul.com:9527/microservicecloud-dept/dept/get/3

 

 

*路由访问映射规则

1.配置yml 加上zull配置

zuul:

routes:

mydept:serviceId: microservicecloud-dept

mydept.path: /mydept/**

http://myzuul.com:9527/mydept/dept/get/3

2.将原真实服务名忽略 继续修改yml 只能通过单一隐藏的路径访问

zuul:

routes:

mydept:serviceId: microservicecloud-dept

mydept.path: /mydept/**

ignored-services: microservicecloud-dept

如果想将多个微服务的名字禁掉 用:“*”

ignored-services: "*"

 

*3.设置统一公共前缀 修改yml 加上

prefix: /*

http://myzuul.com:9527/*/mydept/dept/get/3

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