微服务架构--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

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

<modelVersion>4.0.0</modelVersion>

<artifactId>microservicecloud-zuul-gateway-9527</artifactId>

<parent>

<groupId>com.*.springcloud</groupId>

<artifactId>microservicecloud</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

<dependencies>

<!-- 热部署 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>springloaded</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<dependency><!-- 引入自定义的api通用包 -->

<groupId>com.*.springcloud</groupId>

<artifactId>microservice-api</artifactId>

<version>${project.version}</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jetty</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-config</artifactId>

</dependency>

<!-- hystrix -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-hystrix</artifactId>

</dependency>

<!-- 主管监控和信息配置 监控信息完善 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<!-- zuul路由相关 -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-zuul</artifactId>

</dependency>

</dependencies>

</project>

*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

猜你喜欢

转载自blog.csdn.net/debugbugbg/article/details/81610450