SpringCLoud如何搭建Zuul网关集群

  介绍

这篇文章给大家分享的是有关SpringCLoud如何搭建Zuul网关集群的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

1。使用技术

Springboot, SpringCloud, Zuul, Nignx

2。目的

使用Zuul搭建微服务高可用的网关

3。项目创建

3.1创建注册中心(略)

3.2创建一个hello-service的服务工程

3.3创建springcloud-zuul-ha网关服务

3.3.1创建工程(略)

3.3.2 pom。xml

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   & lt; project  xmlns=癶ttp://maven.apache.org/POM/4.0.0", xmlns: xsi=癶ttp://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"比;   & lt;才能modelVersion> 4.0.0</modelVersion>      & lt;才能groupId> qinfeng.zheng</groupId>   & lt;才能artifactId> springcloud-zuul-ha</artifactId>   & lt;才能version> 0.0.1-SNAPSHOT</version>   & lt;才能packaging> jar</packaging>      & lt;才能name> springcloud-zuul-ha</name>   & lt;才能description> Demo  project  for  Spring  Boot      & lt;才能parent>   ,,,& lt; groupId> org.springframework.boot   ,,,& lt; artifactId> spring-boot-starter-parent   ,,,& lt; version> 1.4.2.RELEASE   ,,,& lt; relativePath/祝辞,& lt; !——, lookup  parent 得到repository ——比;   & lt;才能/parent>      & lt;才能properties>   ,,,& lt; project.build.sourceEncoding> UTF-8   ,,,& lt; project.reporting.outputEncoding> UTF-8   ,,,& lt; java.version> 1.8 & lt;/java.version>   & lt;才能/properties>      & lt;才能dependencies>      ,,,& lt; dependency>   ,,,,,& lt; groupId> org.springframework.cloud   ,,,,,& lt; artifactId> spring-cloud-starter-zuul   ,,,& lt;/dependency>   ,,,& lt; dependency>   ,,,,,& lt; groupId> org.springframework.cloud   ,,,,,& lt; artifactId> spring-cloud-starter-eureka   ,,,& lt;/dependency>      & lt;才能/dependencies>   & lt;才能dependencyManagement>   ,,,& lt; dependencies>   ,,,,,& lt; dependency>   ,,,,,,,& lt; groupId> org.springframework.cloud   ,,,,,,,& lt; artifactId> spring-cloud-dependencies   ,,,,,,,& lt; !——Dalston.RC1这个高版本的zuul依赖有问题——比;   ,,,,,,,& lt; version> Brixton.SR7   ,,,,,,,& lt; type> pom   ,,,,,,,& lt; scope> import   ,,,,,& lt;/dependency>   ,,,& lt;/dependencies>   & lt;才能/dependencyManagement>      & lt;才能build>   ,,,& lt; plugins>   ,,,,,& lt; plugin>   ,,,,,,,& lt; groupId> org.springframework.boot   ,,,,,,,& lt; artifactId> spring-boot-maven-plugin   ,,,,,& lt;/plugin>   ,,,& lt;/plugins>   & lt;才能/build>   & lt;/project>

3.3.3应用程序。yml

# # #服务注册地址   尤里卡:   ,客户:   serviceUrl才能:   ,,defaultZone: http://localhost: 8763/尤里卡/# # # api网关端口号   服务器:   ,端口:82   # # #网关名称   春天:   ,应用程序:   名称:才能service-zuul      zuul:   ,路线:   ,# # #定义转发服务规则   api才能:   ,,,路径:/api-hello/* *,, #请求路径中含有api-hello,都会转发到hello-service服务   ,,# # #服务别名,zuul网关默认整合ribbon 自动实现负载均衡轮训效果   ,,,serviceId: hello-service 3.3.4

定义一个过滤器

qinfeng.zheng.filter。AccessFilter

import  com.netflix.zuul.ZuulFilter;   import  com.netflix.zuul.context.RequestContext;   import  org.apache.commons.lang.StringUtils;   import  org.springframework.beans.factory.annotation.Value;   import  org.springframework.stereotype.Component;      import  javax.servlet.http.HttpServletRequest;/* *   *,创建时间:16:07  2018/7/16   *,修改时间:   *,编码人员:ZhengQf   *,版,本:0.0.1   *,功能描述:自定义一个Zuul 过滤器,它在请求路由之前进行过滤   *   *,补:zuul两大功能:1。路由请求   *,2。过滤   */@ component   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

SpringCLoud如何搭建Zuul网关集群