Zuul主要是做为请求的转发,它根据请求的uri的前缀,转发到注册在eureka对应的服务上。其实Nginx也有请求转发功能,在实际使用中,zuul经常报错。虽然服务已经注册到了uereka,但zuul通过uri却找不到服务...,此时本人也只能通过等...,然后不到五分钟就又恢复了.......
1.在config-server中添加一下配置
spring:
profiles: dev
zuul:
routes:
api-a:
path: /wx/**
serviceId: wechat
此配置表示,如果是zuul的请求,且时 /wx开头的,都会转发到wechat这个服务上。
2.maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xichuan.dev</groupId>
<artifactId>xichuan-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xichuan-zuul</name>
<description>zuul router</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.添加bootstrap.yml配置
名字必须是bootstrap.yml,因为代表是从config-server拉去配置
server:
port: 2004
eureka:
instance:
preferIpAddress: true
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
nonSecurePort: ${server.port}
client:
serviceUrl:
defaultZone: http://localhost:2001/eureka/
management:
security:
enabled: false
spring:
application:
name: xichuan-zuul
cloud:
config:
##这个profile是在这里配置的
profile: dev
discovery:
enabled: true
serviceId: xichuan-config-server
4.在启动类中添加注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@RestController
public class XichuanZuulApplication {
public static void main(String[] args) {
SpringApplication.run(XichuanZuulApplication.class, args);
}
/**
* 跨域许可设置,可在zuul中进行统一配置
*/
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
final CorsConfiguration corsConfig = new CorsConfiguration();
corsConfig.setAllowCredentials(true);
corsConfig.addAllowedOrigin("*");
corsConfig.addAllowedHeader("*");
corsConfig.addAllowedMethod("OPTIONS");
corsConfig.addAllowedMethod("HEAD");
corsConfig.addAllowedMethod("GET");
corsConfig.addAllowedMethod("PUT");
corsConfig.addAllowedMethod("POST");
corsConfig.addAllowedMethod("DELETE");
corsConfig.addAllowedMethod("PATCH");
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfig);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
@RequestMapping(value = "/")
public String heartbeat() {
return "";
}
}
我们可以看启动日志,从config-server获取配置文件
如果我们直接在浏览器中从config-server获取配置文件也是可以的
在浏览器输入http://localhost:2003/xichuan-zuul/dev
可以看到在eureka中注册到的服务
--------------------------------------------2019-01-07 添加------------------------------------------------------------------------------------------
如果接口是下载请求,或需要处理长时间业务时,导致Zuul超时熔断服务,会返回500错误,可以在配置中添加一下配置,增加超时时间:
# 上传大文件得将超时时间设置长一些,否则会报超时异常
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
github上有我更多的笔记:Raray-chuan (兮川) · GitHub,欢迎stars与following,如果有问题可以在issue中向我咨询
关注我的公众号,获取更多关于后端、大数据的知识