基于Zuul(网上找不到音标,读“猪”
第四音)实现API网关,需要按如下步骤实现:
- 搭建Eureka注册中心
- 创建Zuul网关项目
- 创建A项目服务
- 创建B项目服务
流程示意图如下:
启动项目需要按上面顺序执行,否则会报错,下面开始讲解代码。
1. 创建Eureka注册中心1.创建eureka_server项目
2.添加Maven依赖:
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
org.springframework.boot
spring-boot-maven-plugin
3.application.yml 配置信息
server:
port: 8100
eureka:
instance:
hostname: server1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false
fetch-registry: false
4.启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(final String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
2. 创建Zuul网关
1.创建zuul-gateway maven项目
2.添加maven依赖:
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
Finchley.RC1
org.springframework.boot
spring-boot-starter
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.projectlombok
lombok
true
com.alibaba
fastjson
1.2.3
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
org.apache.maven.plugins
maven-resources-plugin
3.0.1
copy-conf
package
copy-resources
UTF-8
${project.build.directory}/ext/conf
ext/conf
logback.xml
true
org.jacoco
jacoco-maven-plugin
0.7.5.201505241946
default-prepare-agent
prepare-agent
default-prepare-agent-integration
prepare-agent-integration
com.spotify
docker-maven-plugin
0.4.3
hy_uav_gateway
src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar
ext/conf/logback.xml
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
3.配置application 依赖信息
###注册 中心
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
server:
port: 80
###网关名称
spring:
application:
name: service-zuul
### 配置网关反向代理
zuul:
routes:
api-a:
path: /api-a/**
serviceId: project_a
api-b:
path: /api-b/**
serviceId: project_b
4.ZuulApplication启动运行,并把过滤器拦截注册到SpringBoot容器
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class AppZuul {
// @EnableZuulProxy 开启Zuul网关代理
// @EnableEurekaClient 注册到EurekaC
public static void main(String[] args) {
SpringApplication.run(AppZuul.class, args);
}
// 注册到SpringBoot 容器
@Bean
public TokenFilter accessFilter() {
return new TokenFilter();
}
}
5.过滤器拦截参数
// 使用网关拦截Token参数
public class TokenFilter extends ZuulFilter {
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
Object accessToken = request.getParameter("accessToken");
if (accessToken == null) {
// 返回错误信息
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
ctx.setResponseBody("accessToken is null");
return null;
}
return null;
}
public boolean shouldFilter() {
return true;// 是否执行该过滤器,此处为true,说明需要过滤
}
@Override
public int filterOrder() {
return 0;// 优先级为0,数字越大,优先级越低
}
@Override
public String filterType() {
return "pre"; // 前置过滤器
}
}
3. 创建A项目及B项目
A服务与B服务的创建方式大同小异,以A项目为例子:
1.创建api-a项目
2.添加maven依赖:
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
Finchley.RC1
org.springframework.boot
spring-boot-starter
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.projectlombok
lombok
true
com.alibaba
fastjson
1.2.3
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
org.apache.maven.plugins
maven-resources-plugin
3.0.1
copy-conf
package
copy-resources
UTF-8
${project.build.directory}/ext/conf
ext/conf
logback.xml
true
org.jacoco
jacoco-maven-plugin
0.7.5.201505241946
default-prepare-agent
prepare-agent
default-prepare-agent-integration
prepare-agent-integration
com.spotify
docker-maven-plugin
0.4.3
hy_uav_gateway
src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar
ext/conf/logback.xml
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
3.配置application 依赖信息(b项目名字为:project_b)
server:
port: 8001
###服务名称
spring:
application:
name: project_a
###注册中心地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
4.启动类(b项目index方法返回“我是B项目”)
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class AIndexController {
@RequestMapping("/")
public String index() {
return "我是A项目....";
}
public static void main(String[] args) {
SpringApplication.run(AIndexController.class, args);
}
}
4. 测试
4.1 运行Eureka注册中心
启动应用:http://localhost:8100/,可以看到Eureka注册中心管理平台。
会发现Zuul网关注册上Eureka中心了
可以看到项目A和项目B都注册上Eureka中心了
浏览器输入http://localhost/api-a,返回内容如下
可以看出tonken为null,要加token。
访问:http://localhost/api-a?token=123456 访问:http://localhost/api-b?token=123456