【后面的话】书接上文,后面曾经讲过了SpringCloud的注册核心Eureka、Ribbon和Feign等等,如果有不分明的也能够去看看我的微服务系列文章。这篇文章我要说的是微服务中的网关。
壹、Zuul的简介
Zuul的次要性能是路由转发和过滤器。路由性能是微服务的一部分,比方/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon联合实现了负载平衡的性能。
zuul有以下性能:
Authentication
Insights
Stress Testing
Canary Testing
Dynamic Routing
Service Migration
Load Shedding
Security
Static Response handling
Active/Active traffic management
贰、筹备工作
新建一个feign子工程lovin-cloud-zuul,用于前面的操作。上面是次要的pom依赖:
<parent>
<artifactId>lovincloud</artifactId>
<groupId>com.eelve.lovincloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lovin-cloud-zuul</artifactId>
<packaging>jar</packaging>
<name>lovincloudzuul</name>
<version>0.0.1</version>
<description>zuul</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 这里为了平安,我这里还是增加spring-boot-starter-security,同时配置路由规定发送/api-ribbon/打头开始的服务转发到lovinribbonclient而发送/api-feign/打头的服务转发到lovinfeignclient,能够看出这里是配置相应的路由规定。
server:
port: 8882 # 服务端口号
spring:
application:
name: lovincloudzuul # 服务名称
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
zuul:
routes:
api-ribbon:
path: /api-ribbon/**
serviceId: lovinribbonclient
api-feign:
path: /api-feign/**
serviceId: lovinfeignclient
eureka:
client:
serviceUrl:
defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 注册到的eureka服务地址
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: lovin
user.password: lovin
- 配置spring-boot-starter-security,这里为了不便我这里放开所有申请
package com.eelve.lovin.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* @ClassName WebSecurityConfig
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/18 12:17
* @Version 1.0
**/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
- 在主类上增加@EnableZuulProxy ,当然也须要注册到注册核心:
package com.eelve.lovin;
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;
/**
* @ClassName LovinEurekaClientApplication
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/15 16:37
* @Version 1.0
**/
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class LovinCloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(LovinCloudZuulApplication.class,args);
}
}
- 这里为了不便测试,这里配置相应的过滤规定:
package com.eelve.lovin.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* @ClassName MyFilter
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/18 12:44
* @Version 1.0
**/
@Component
@RefreshScope // 应用该注解的类,会在接到SpringCloud配置核心配置刷新的时候,主动将新的配置更新到该类对应的字段中。
@Slf4j
public class MyFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
Object accessToken = request.getParameter("token");
if(accessToken == null) {
log.warn("token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
}catch (Exception e){}
return null;
}else if(!accessToken.equals("lovin")){
log.warn("token is not correct");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(403);
try {
ctx.getResponse().getWriter().write("token is not correct");
}catch (Exception e){}
return null;
}
log.info("ok");
return null;
}
}
filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
pre:路由之前
routing:路由之时
post: 路由之后
error:发送谬误调用
filterOrder:过滤的程序
shouldFilter:这里能够写逻辑判断,是否要过滤,本文true,永远过滤。
run:过滤器的具体逻辑。可用很简单,包含查sql,nosql去判断该申请到底有没有权限拜访。
叁、启动测试
- 顺次启动eureka的服务端和两个客户端和lovin-feign-client、lovin-ribbon-client,以及新建的lovin-cloud-zuul
- 而后拜访http://localhost:8882/api-feign/getHello和http://localhost:8882/api-ribbon/hello,而后咱们能够带上token拜访来验证过滤器
肆、网络架构
- 咱们能够看到咱们调用的服务不再是像再上一篇文章中的间接拜访对应的服务,而是通过feign的Ribbon的负载平衡的去调用的,而且这里阐明一点,Ribbon的默认机制是轮询。
- 最初的最初是本博客的源码,欢送关注这一套SpringCloud的实际
发表回复