博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring cloud构建互联网分布式微服务云平台-路由网关(zuul)
阅读量:6893 次
发布时间:2019-06-27

本文共 3658 字,大约阅读时间需要 12 分钟。

在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个简答的微服务系统如下图:Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求一七九一七四三三八零

注意: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文件如下:

4.0.0
com.forezp
service-zuul
0.0.1-SNAPSHOT
jar
service-zuul
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
复制代码

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

@EnableZuulProxy@EnableEurekaClient@SpringBootApplicationpublic class ServiceZuulApplication {     public static void main(String[] args) {        SpringApplication.run(ServiceZuulApplication.class, args);    }}复制代码

  加上配置文件application.yml加上以下的配置代码:

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8769spring:  application:    name: service-zuulzuul:  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服务;

依次运行这五个工程;打开浏览器访问: ;浏览器显示:

hi forezp,i am from port:8762复制代码

  打开浏览器访问: ;浏览器显示:

hi forezp,i am from port:8762复制代码

  这说明zuul起到了路由的作用

转载地址:http://nnudl.baihongyu.com/

你可能感兴趣的文章
P2447 [SDOI2010]外星千足虫
查看>>
uva 796(求割边)
查看>>
铁大Facebook——十天冲刺(7)
查看>>
Oracle连接:TNS出错
查看>>
前端开发中,如何优化图像?图像格式的区别?
查看>>
借助第八代智能英特尔® 酷睿™ i7 处理器和 Unreal Swarm* 的强大性能快速构建光照...
查看>>
java之运算符
查看>>
洛谷 P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers Label:ExWater
查看>>
[K/3Cloud]进度条控件编程接口
查看>>
hduoj1090A+B for Input-Output Practice (II)
查看>>
hduoj1096A+B for Input-Output Practice (VIII)
查看>>
Mac Mysql 修改初始化密码
查看>>
textarea 禁止拉伸
查看>>
Struts2运行流程
查看>>
Numpy学习2
查看>>
ios解决本地私有库出现的bug
查看>>
QuickFIX/N入门:三、 如何配置QuickFIX/N
查看>>
找数字(递归,二分查找)
查看>>
access表达式类型不匹配
查看>>
多线程2(线程安全,线程同步,死锁,lock锁,等待唤醒机制)
查看>>