SpringCloud系列研究服务网关zuulWord文件下载.docx
《SpringCloud系列研究服务网关zuulWord文件下载.docx》由会员分享,可在线阅读,更多相关《SpringCloud系列研究服务网关zuulWord文件下载.docx(17页珍藏版)》请在冰豆网上搜索。
![SpringCloud系列研究服务网关zuulWord文件下载.docx](https://file1.bdocx.com/fileroot1/2022-10/1/123cd5f8-bd11-48f8-8713-06aebd5d4c22/123cd5f8-bd11-48f8-8713-06aebd5d4c221.gif)
<
?
xmlversion="
1.0"
encoding="
UTF-8"
>
projectxmlns="
http:
//maven.apache.org/POM/4.0.0"
xmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
//maven.apache.org/POM/4.0.0http:
//maven.apache.org/xsd/maven-4.0.0.xsd"
<
modelVersion>
4.0.0<
/modelVersion>
groupId>
com.cloud.microservice<
/groupId>
artifactId>
ms-zuul-demo<
/artifactId>
version>
0.0.1-SNAPSHOT<
/version>
packaging>
jar<
/packaging>
name>
/name>
description>
DemoprojectforSpringBoot<
/description>
parent>
org.springframework.boot<
spring-boot-starter-parent<
1.5.9.RELEASE<
relativePath/>
!
--lookupparentfromrepository-->
/parent>
properties>
project.build.sourceEncoding>
UTF-8<
/project.build.sourceEncoding>
project.reporting.outputEncoding>
/project.reporting.outputEncoding>
java.version>
1.8<
/java.version>
spring-cloud.version>
Edgware.SR1<
/spring-cloud.version>
/properties>
dependencies>
dependency>
org.springframework.cloud<
spring-cloud-starter-eureka<
/dependency>
spring-cloud-starter-zuul<
spring-boot-starter-test<
scope>
test<
/scope>
/dependencies>
dependencyManagement>
spring-cloud-dependencies<
${spring-cloud.version}<
type>
pom<
/type>
import<
/dependencyManagement>
build>
plugins>
plugin>
spring-boot-maven-plugin<
/plugin>
/plugins>
/build>
/project>
2:
application.properties中的配置信息如下:
spring.application.name=ms-gateway-zuul
server.port=8000
#注册中心地址
eureka.client.serviceUrl.defaultZone=http:
//xx.xx.xx.xx:
9000/eureka/
#这里的配置表示,访问/userdemo/**直接重定向到eureka中的ms-demo-provider这个serviceId
zuul.routes.userprovider.path=/userdemo/**
zuul.routes.userprovider.serviceId=ms-demo-provider
3:
在入口类Application中增加@EnableZuulProxy注解,表明这是个网关服务
packagecom.cloud.microservice;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
publicclassZuulDemoApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(ZuulDemoApplication.class,args);
}
}
启动工程,然后刷新Eureka界面,可以看到本地zuul服务已经注册到服务中心。
四、运行测试
打开浏览器,访问http:
//localhost:
8000/userdemo/demo/user/1.0/findAll,返回结果如下,说明网关已经成功转发到相应的服务,接口调用成功。
至此,zuul的简单demo已经完成,其他高级功能有待后续研究。
五、问题总结
如果在通过网关访问接口的时候出现flix.zuul.exception.ZuulException:
Forwardingerror错误,则需要在pom.xml中增加如下依赖:
我由于是用idea模板生成的工程,所以pom里面缺少spring-cloud-starter-eureka这个jar包,后来加上之后问题就解决了。
详细log信息:
2018-01-2616:
36:
24.925WARN32240---[nio-8000-exec-3]o.s.c.n.z.filters.post.SendErrorFilter:
Errorduringfiltering
flix.zuul.exception.ZuulException:
Forwardingerror
atflix.zuul.filters.route.RibbonRoutingFilter.handleException(RibbonRoutingFilter.java:
189)~[spring-cloud-netflix-core-1.4.2.RELEASE.jar:
1.4.2.RELEASE]
atflix.zuul.filters.route.RibbonRoutingFilter.forward(RibbonRoutingFilter.java:
164)~[spring-cloud-netflix-core-1.4.2.RELEASE.jar:
atflix.zuul.filters.route.RibbonRoutingFilter.run(RibbonRoutingFilter.java:
111)~[spring-cloud-netflix-core-1.4.2.RELEASE.jar:
atflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:
112)~[zuul-core-1.3.0.jar:
1.3.0]
atflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:
193)~[zuul-core-1.3.0.jar:
atflix.zuul.FilterProcessor.runFilters(FilterProcessor.java:
157)~[zuul-core-1.3.0.jar:
atflix.zuul.FilterProcessor.route(FilterProcessor.java:
118)~[zuul-core-1.3.0.jar:
atflix.zuul.ZuulRunner.route(ZuulRunner.java:
96)~[zuul-core-1.3.0