Spring Cloud入门教程之路由网关 Zuul(五)(Finchley版本+Boot2.0)

xiaoxiao2021-02-28  23

路由网关什么是Zuul?

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

zuul有以下功能:

AuthenticationInsightsStress TestingCanary TestingDynamic RoutingService MigrationLoad SheddingSecurityStatic Response handlingActive/Active traffic management

 

 

 

一、使用Zuul

1、修改pom.xml

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>

完整pom.xml

<?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> <groupId>com.cloud</groupId> <artifactId>serverzuul</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>serverzuul</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RC2</spring-cloud.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>

 

 

 

2、编写Controller类

在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能

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

 

 

首先指定服务注册中心的地址为http://localhost:8761/eureka/,服务的端口为8767,服务名为service-zuul;以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务

 

3、配置文件

server.port=8767 spring.application.name=service-zuul #指定服务中心地址 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ feign.hystrix.enabled=true zuul.routes.api-a.path=/api-a/** zuul.routes.api-a.serviceId=service-ribbon zuul.routes.api-b.path=/api-b/** zuul.routes.api-b.serviceId=service-feign

4、通过zuul指定的ribbon去消费

 

 

 

 

5、通过zuul指定的通过feign去消费

 

 

 

 

Spring Boot与Spring Cloud学习使用可参看笔者博客

       ①Spring Cloud入门教程之服务注册与发现Eureka

       ②Spring Cloud入门教程之服务消费者 Ribbon

       ③Spring Cloud入门教程之服务消费者 Feign

       ④Spring Cloud入门教程之断路器 Hystrix

       ⑤Spring Cloud入门教程之断路由网关 Zuul

       ⑥Spring Cloud入门教程之分布式配置中心 Spring Cloud Config

       ⑦idea下新建Spring Boot项目并配置启动

       ⑧Spring Boot无法自动注入bean问题解决方案

       ⑨idea 设置Spring Boot热部署

 

 

转载请注明原文地址: https://www.6miu.com/read-2628819.html

最新回复(0)