Zuul简单搭建
服务系统配置:
1)添加pom依赖:
<!-- eureka 注册中心组件-需要将zuul注册到注册中心使用注册表进行路由 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- zuul 网关模块 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- 路由断路隔离,对HystrixCommand执行信息进行实时累加需要star中添加@EnableCircuitBreaker,Controller中添加@HystrixCommand(fallbackMethod = "熔断方法") -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<!-- 这个包是用做健康度监控的 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- feign和zuul默认需要依赖ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
2)配置文件-application.properties
#系统标识,注册中心系统名标志 spring.application.name=tp-zuul #配置网关映射端口 server.port=18701 #是否显示主机ip eureka.instance.preferIpAddress=true #指定eureka服务端地址 eureka.client.serviceUrl.defaultZone=http://localhost:port/eureka/ #leaseRenewalIntervalInSeconds,表示eureka client发送心跳给server端的频率 eureka.instance.leaseRenewalIntervalInSeconds=50 #leaseExpirationDurationInSeconds,表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。 #默认为90秒 #如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。 #如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。 #该值至少应该大于leaseRenewalIntervalInSeconds eureka.instance.leaseExpirationDurationInSeconds=30 #表示eureka client间隔多久去拉取服务注册信息,默认为30秒,对于api-gateway,如果要迅速获取服务注册状态,可以缩小该值,比如5秒 eureka.client.registryFetchIntervalSeconds=50 # 开启基于http basic的认证 #spring.security.basic.enable=ture #配置登陆账号 #spring.security.user.name=user #配置登陆密码 #spring.security.user.password=password123 #zuul配置-此三项不配置zuul使用报错com.netflix.zuul.exception.ZuulException: Forwarding error #读取超时时间 zuul.host.readTimeoutMillis=30000 #链接超时时间 zuul.host.connectTimeoutMillis=30000 #socket超时时间 zuul.host.socketTimeout-Millis=30000 #1)第一个映射系统 #tp-cust配置 #映射路径 zuul.routes.tp-cust.path=/query-cust/* #链接映射到的应用 zuul.routes.tp-cust.serviceId=tp-cust #是否去掉url的开始(如tp-app不去掉)/缺少此项可能会导致只用zuul的时候失败;true:http://localhost:port/query-cust/queryUserInfo -> .../tp-cust/queryUserInfo去掉query-cust zuul.routes.tp-cust.stripPrefix=false #2)第二个映射系统 #tp-product配置 #映射路径 zuul.routes.tp-product.path=/query-product/* #映射的应用 zuul.routes.tp-product.serviceId=tp-product #Zuul路由前缀--是否去掉url的开始(如tp-app不去掉) zuul.routes.tp-product.stripPrefix=false #全局配置,zuul不会向下游传递这三个头 zuul.routes.tp-product.sensitiveHeaders=Cookie,Set-Cookie # 忽略某些微服务:(直接写微服务的名字=>可以理解为spring.application.name的值,多个以都好分隔)/ 忽略所有微服务:(后面写 * ) #zuul.ignoredService=tp-product #使ribbon禁用eureka #ribbon.eureka.enable=false #配置tp-cust应用使用的路由 #tp-cust.ribbon.listOfServers=localhost:18702
3)启动类添加@EnableZuulProxy注解
package com.threepicture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
@EnableDiscoveryClient
//Hystrix 仪表盘 与ribbon有区别 EnableCircuitBreaker此注解必须得加
@EnableHystrixDashboard
public class StartApplication {
private static Logger logger = LoggerFactory.getLogger(StartApplication.class);
public static void main(String[] args) {
logger.info("进入zuul网关。。。");
SpringApplication.run(StartApplication.class, args);
}
}

浙公网安备 33010602011771号