路由是微服务架构不可或缺的一部分。例如,/可以映射到您的Web应用程序,/api/users映射到用户服务,/api/shop映射到商店服务。 Zuul是Netflix的基于JVM的路由器和服务器端负载均衡器
Routing is an integral part of a microservice architecture. For example, / may be mapped to your web application, /api/users is mapped to the user service and /api/shop is mapped to the shop service. Zuul is a JVM-based router and server-side load balancer from Netflix.
本教程在【Spring Cloud】第三篇 Client Side Load Balancer | 负载均衡 - Ribbon的基础上添加代码。
How to Include Zuul | 项目中如何包含 Zuul
To include Zuul in your project, use the starter with a group ID of
org.springframework.cloudand an artifact ID ofspring-cloud-starter-netflix-zuul. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.
添加group ID到 pom.xml,注意自己的版本。
Zuul有几种配置的方式
跳过自动添加服务,将zuul.ignored-services 设置服务id列表的模式,如zuul.ignored-services = server-consume或zuul.ignored-services = * 将忽略掉指定或所有的服务,但是如果在路由列表中添加了指定的路由,将不会被忽略,如下所示,除users外将忽略所有服务。
zuul:
ignoredServices: '*'
routes:
users: /myusers/**
要增加或者修改路由器规则,可以添加如下配置,浏览器访问/myusers会转发到users服务,如:/myusers/101转发到/101)。
zuul:
routes:
users: /myusers/**
要对路由器进行更精细的操作,可以指定独立的路径和ServerID(Eureka 服务发现),如下配置:
zuul:
routes:
users:
path: /myusers/**
serviceId: users_service
也可以直接配置http路径的物理位置
zuul:
routes:
users:
path: /myusers/**
url: http://example.com/users_service
实际操作
创建server-zuul工程
完整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.cyinfotech</groupId>
<artifactId>server-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server-zuul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.cyinfotech</groupId>
<artifactId>sc-f-e-04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--Eureka-Client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改父工程pom.xml,在modules添加server-ribbon
<module>server-zuul</module>
修改ServerZuulApplication
package com.cyinfotech.serverzuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ServerZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ServerZuulApplication.class, args);
}
}
在application.yml中添加路由规则
spring:
application:
name: server-zuul
server:
port: 8084
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
aa:
path: /hy/**
serviceId: server-hystrix
bb:
path: /ec/**
serviceId: eureka-client
分别在server-hystrix和eureka-client中添加hi服务路径
ServerHystrixApplication
package com.cyinfotech.serverhystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
@RestController
public class ServerHystrixApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerHystrixApplication.class).web(true).run(args);
}
@Autowired
StoreIntegration storeIntegration;
@Value("${server.port}")
String port;
@RequestMapping("/")
public Object getHi (@RequestParam String name){
return "HI - " + name + ", port:" + port + ", By server-hystrix.";
}
@RequestMapping("/hi")
public Object hi (){
return "HI, By server-hystrix.";
}
}
EurekaClientApplication
package com.cyinfotech.eurekaclient;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class EurekaClientApplication {
@RequestMapping("/hi")
public String home() {
return "Hello world, by eureka-client.";
}
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaClientApplication.class).web(true).run(args);
}
}
访问路由路径:http://localhost:8084/ec/hi
Hello world, by eureka-client.
访问路由路径:http://localhost:8084/hy/hi
HI, By server-hystrix.
本篇教程就到这,至于更深的教程我会加紧学习然后写出来。
欢迎关注我的公众号,跟我留言。
博客地址:https://blog.aprcode.com/sc-f-e-04/
教程源码Github地址:sc-f-e-04
教程源码Gitee地址:sc-f-e-04