背景:基于springcloud(七)继续
使用路由网关统一管理访问接口
1. 创建工程spring-cloud-zuul,pom.xml文件依赖
<?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.zsx</groupId>
<artifactId>spring-cloud-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-zuul</name>
<description>Spring Cloud Zuul project</description>
<parent>
<groupId>com.zsx</groupId>
<artifactId>spring-cloud-customize-dependencies</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../spring-cloud-customize-dependencies/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.zsx.ZuulApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. application.yml文件内容
spring:
application.name: spring-cloud-zuul
server:
port: 8087
zuul:
routes:
# 以 /api/r 开头的请求都转发给spring-cloud-ribbon
api-ribbon:
path: /api/r/**
serviceId: spring-cloud-ribbon
# 以 /api/f 开头的请求都转发给spring-cloud-feign
api-feign:
path: /api/f/**
serviceId: spring-cloud-feign
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8081/eureka/
3. 引导类
package com.zsx;
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;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
4. 依次启动 启动注册中心,服务提供者、Ribbon消费者、Feign消费者、ZuulApplication引导类
5. 查看请求http://localhost:8087/api/f/feign/hello
6. 查看请求http://localhost:8087/api/r/ribbon/hello
到此Zuul的路由功能配置成功
7. 配置Zuul回调
package com.zsx.config;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@Configuration
public class FeignFallbackProvider implements FallbackProvider {
@Override
public String getRoute() {
// ServiceId,如果需要所有调用都支持回退,则 return "*" 或 return null
return "spring-cloud-feign";
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpHeaders getHeaders() {
// 解决中文乱码问题
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return headers;
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("Unable to connect, please check your network".getBytes("utf-8"));
}
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return HttpStatus.OK.value();
}
@Override
public String getStatusText() throws IOException {
return HttpStatus.OK.getReasonPhrase();
}
@Override
public void close() {
}
};
}
}
8. 重启ZuulApplication,停止Feign消费者
9. 查看请求http://localhost:8087/api/f/feign/hello
到此回调配置成功