1.创建项目如下
2.创建项目勾选web,zuul,eurekaclient
3.pom.xml文件内容许下
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.cloud</groupId> 12 <artifactId>spring-cloud-starter-netflix-zuul</artifactId> 13 </dependency> 14 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-test</artifactId> 18 <scope>test</scope> 19 </dependency> 20 </dependencies>
4.属性文件如下
1 #给该服务起名字 2 spring.application.name=eureka-zuul 3 4 #定义该服务的端口号 5 server.port=7600 6 7 #路由配置:配置访问映射路径 8 zuul.routes.client-school-provider=/pro/** 9 #zuul.routes.classes-client-consumer=/con/** 10 11 ##zuul中默认就已经集成了Ribbon负载均衡和Hystix熔断机制。但是所有的超时策略 12 ##都是走的默认值,比如熔断超时时间只有1s,很容易就触发了,因此建议我们手动配置如下: 13 ##否则就会报出如下错误: 14 ##There was an unexpected error (type=Gateway Timeout, status=504). 15 ##com.netflix.zuul.exception.ZuulException: Hystrix Readed time out 16 ##这个错误是因为拉取服务的默认时间太短,导致拉取服务失败了,最终导致访问失败 17 #是否开启重试功能 18 #zuul.retryable=true 19 ##是否对所有操作重试 20 #ribbon.OkToRetryOnAllOperations=true 21 ###对当前服务的重试次数 22 #ribbon.MaxAutoRetries=1 23 24 ##同一实例的不同服务的重试次数 25 #ribbon.MaxAutoRetriesNextServer=2 26 27 28 #zuul.host.socket-timeout-millis= 60000 29 #zuul.host.connect-timeout-millis=60000 30 31 ##链接超时时间 32 #ribbon.ConnectTimeout=60000 33 ###通信超时时间 34 #ribbon.ReadTimeout=60000 35 36 37 ##熔断器超时时长 38 #hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000 39 40 41 #注册中心配置 42 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
5.启动类如下
1 package cn.kgc; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 import org.springframework.cloud.netflix.feign.EnableFeignClients; 7 import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 8 9 @EnableEurekaClient 10 @EnableZuulProxy 11 @SpringBootApplication 12 public class EurekaZuulApplication { 13 14 public static void main(String[] args) { 15 SpringApplication.run(EurekaZuulApplication.class, args); 16 } 17 18 }