一、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 {
private final Logger logger= (Logger) LoggerFactory.getLogger(FallbackProvider.class);
public static void main(String[] args) {
SpringApplication.run(Provider2033Application.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home(){
logger.info("request tow name");
try {
Thread.sleep(1000000);
}catch (Exception e){
logger.error("hello tow error",e);
}
return "软件老五,端口"+port+" ,this is twn message";
}
}
三、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>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</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 id: api-provider
#####service-id:provider2033
##http://localhost:2033 ==> http://localhost:2041/provider
######1、第一种
##-----zuul.routes.api-provider.path=/provider/**
##-----zuul.routes.api-provider.service-id=provider2033
######2、第二种 简易配置(route id、service-id一样)
####若route id、service-id:一样 provider2033
zuul.routes.provider2033.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);
}
//***********MyZullFilter第一种@Bean启用
//MyZullFilter的@Component 注解掉
@Bean
public MyZullFilter myZullFilter(){
return new MyZullFilter();
}
//***********MyZullFilter第二种的@Component启用
//Zull2041Application的@Bean 注解掉
}
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();
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;
}
}
5、ProducerFallback
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
/***
* 熔断处理
*/
@Component
public class ProducerFallback implements FallbackProvider {
private final Logger logger= (Logger) LoggerFactory.getLogger(FallbackProvider.class);
service-id:provider2033
@Override
public String getRoute() {
return "provider2033";
}
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse(){
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers=new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("The service is unavailable.".getBytes());
}
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
};
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
if(cause!=null&&cause.getCause()!=null){
String reason=cause.getCause().getMessage();
logger.info("Exception{}"+reason);
}
return fallbackResponse();
}
}
6、路由重启
四、Jmeter测试
测限流
标签:网关,Retry,Zuul,springframework,eureka,return,org,import,public From: https://blog.51cto.com/smallfa/5951512