企业分布式微服务云SpringCloud SpringBoot mybatis (五)路由网关(zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个简答的微服务系统如下图:
注意:A服务和B服务是可以相互调用的,作图的时候忘记了。并且配置服务也是注册到服务注册中心的。
在Spring Cloud微服务系统中,一种常见的负载均衡方式是,客户端的请求首先经过负载均衡(zuul、Ngnix),再到达服务网关(zuul集群),然后再到具体的服。,服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件由配置服务管理(下一篇文章讲述),配置服务的配置文件放在git仓库,方便开发人员随时改配置。
一、Zuul简介
Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。
zuul有以下功能:
- Authentication
- Insights
- Stress Testing
- Canary Testing
- Dynamic Routing
- Service Migration
- Load Shedding
- Security
- Static Response handling
- Active/Active traffic management
二、准备工作
继续使用上一节的工程。在原有的工程上,创建一个新的工程。
三、创建service-zuul工程
其pom.xml文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
<?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.forezp</groupId>
<artifactId>service-zuul</artifactId>
<version> 0.0 . 1 -SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-zuul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version> 1.5 . 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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</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>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</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>
|
在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:
1
2
3
4
5
6
7
8
9
|
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceZuulApplication. class , args);
}
}
|
加上配置文件application.yml加上以下的配置代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
eureka:
client:
serviceUrl:
defaultZone: http: //localhost:8761/eureka/
server:
port: 8769
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /api-a/**
serviceId: service-ribbon
api-b:
path: /api-b/**
serviceId: service-feign
|
首先指定服务注册中心的地址为http://localhost:8761/eureka/,服务的端口为8769,服务名为service-zuul;以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务;
依次运行这五个工程;打开浏览器访问:http://localhost:8769/api-a/hi?name=forezp ;浏览器显示:
1
|
hi forezp,i am from port: 8762
|
打开浏览器访问:http://localhost:8769/api-b/hi?name=forezp ;浏览器显示:
1
|
hi forezp,i am from port: 8762
|
这说明zuul起到了路由的作用
架构代码如下:
资料和源码来源地址
Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求:一七九一七四三三八零
转载于:https://juejin.im/post/5c6e464b518825790c5b1193
企业分布式微服务云SpringCloud SpringBoot mybatis (五)路由网关(zuul)相关推荐
- 企业分布式微服务云SpringCloud SpringBoot mybatis - 服务消费者(Feign)
一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注 ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (九)服务链路追踪(Spring Cloud Sleuth)...
这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Add sleuth to the classpath of a Spring Bo ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (七)高可用的分布式配置中心(Spring Cloud Config)...
讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下: 一. ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (十一)docker部署spring cloud项目
一.docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (二)服务消费者(rest+ribbon)
一.ribbon简介 Ribbon is a client side load balancer which gives you a lot of control over the behaviour ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (二)Spring Boot属性配置文件详解...
相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (十二)断路器监控(Hystrix Dashboard)...
在我的第四篇文章断路器讲述了如何使用断路器,并简单的介绍了下Hystrix Dashboard组件,这篇文章更加详细的介绍Hystrix Dashboard. 一.Hystrix Dashboard简 ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (九)Spring Boot多数据源配置与使用(JdbcTemplate支持)...
之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (十)Spring Boot多数据源配置与使用Spring-data-jpa支持...
Spring-data-jpa支持 对于数据源的配置可以沿用上例中DataSourceConfig的实现. 新增对第一数据源的JPA配置,注意两处注释的地方,用于指定数据源对应的Entity实体和Re ...
最新文章
- mysql 字符串 空格函数_mysql中的去除空格函数
- ERROR: cannot launch node of type [pointcloud_to_laserscan/pointcloud_to_laserscan]
- 捡到vivo手机怎么清除账号_大学保安两天两次跳湖为路人捡手机,您怎么看?...
- python 分析两组数据的差异_R语言limma包差异基因分析(两组或两组以上)
- js reduce实现中间件_MapReduce 模型
- Power Automate Desktop概览
- Android设计模式之——抽象工厂模式
- RFC 8998: ShangMi (SM) Cipher Suites for TLS 1.3
- 微信小程序开发资料汇总
- Oracle Goldengate 安装配置
- 高等数学(第七版)同济大学 习题1-8 个人解答
- 关于magic-api的使用入门
- Android显示人民币双横线的¥符号
- AD10 BOM制作
- 黑龙江省大学计算机学校排名2015,2015黑龙江省最佳大学排行榜
- linux notifier的理解和应用
- python打开autocad
- 海康、大华IP摄像机RTSP地址及格式详解
- 中通快递关键业务和复杂架构挑战下的 Kubernetes 集群服务暴露实践
- 大数据----机器学习---神经网络
热门文章
- 全球及中国特斯拉产业销量预测及投资机遇调研报告2021-2027年
- how to fix error 25007 : loadlibraryshim()
- struct的成员对齐
- oracle访问远程db2,如何在DB2与Oracle之间实现远程拷贝(二)
- 纯css用图片代替checkbox和radio,无js实现方法
- 关于获取各种浏览器可见窗口大小(转载)
- div+css相对定位和绝对定位
- 为Chrome多账户添加单独的快捷方式
- shell中使用echo命令改变输出显示样式
- [转]URLRewrite隐藏.aspx后缀名的应用(转载+延伸)