一、ek2014
1、pom.xml
<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-eureka-server</artifactId>
</dependency>
2、application.properties
# 应用名称
spring.application.name=eureka
server.port=2013
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:2013/eureka/
eureka.server.eviction-interval-timer-in-ms=5
3、Ek2013Application
@EnableEurekaServer
@SpringBootApplication
public class Ek2013Application {
public static void main(String[] args) {
SpringApplication.run(Ek2013Application.class, args);
}
}
二、Provider2033
1、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、application.properties
# 应用名称
spring.application.name=provider2033
# 应用服务 WEB 访问端口
server.port=2033
##eureka
eureka.client.service-url.defaultZone=http://localhost:2013/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
3、Provider2033Application
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class Provider2033Application {
public static void main(String[] args) {
SpringApplication.run(Provider2033Application.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home(){
return "软件老五,端口"+port;
}
}
三、Zull2041
1、pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
2、application.properties
# 应用名称
spring.application.name=zuul2041
server.port=2041
##eureka
eureka.client.service-url.defaultZone=http://localhost:2013/eureka/
eureka.client.registry-fetch-interval-seconds=30
eureka.instance.lease-renewal-interval-in-seconds=15
eureka.instance.lease-expiration-duration-in-seconds=45
#####route api-provider
##http://localhost:2033 ==> http://localhost:2041/provider
zuul.routes.api-provider.path=/provider/**
zuul.routes.api-provider.service-id=provider2033
3、Zull2041Application
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;
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class Zull2041Application {
public static void main(String[] args) {
SpringApplication.run(Zull2041Application.class, args);
}
}
4、MyZullFilter
import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
public class MyZullFilter extends ZuulFilter {
//每秒放多少个token(10个)
private static final RateLimiter RATE_LIMITER=RateLimiter.create(10);
/**
* 过滤类型
* @return
*/
@Override
public String filterType() {
return "pre";
}
/**
* 请求过滤顺序
* @return
*/
@Override
public int filterOrder() {
return 0;
}
/**
* 判断过滤器是否生效
* @return
*/
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
/* if(!RATE_LIMITER.tryAcquire()){
System.out.println("限流.....................");
}*/
//获取token参数
RequestContext rc=RequestContext.getCurrentContext();
if(!RATE_LIMITER.tryAcquire()) {
rc.setSendZuulResponse(false);
rc.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
System.out.println("限流.....................");
}
HttpServletRequest request=rc.getRequest();
String token=request.getParameter("token");
if(token!=null){
return null;
}
//表示不进行路由
rc.setSendZuulResponse(false);
rc.setResponseStatusCode(401);
try {
rc.getResponse().setContentType("text/html;charset=utf-8");
rc.getResponse().getWriter().write("没有传递正确参数,请校验后重试~");
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
四、Jmeter测试
测限流
标签:网关,Zuul,spring,route,springframework,eureka,import,org,cloud From: https://www.cnblogs.com/smallfa/p/16935439.html