Commit d3a30303 by yong.huang

add zuul demo

parent 890de97f
......@@ -21,6 +21,7 @@
<module>demo-sdk</module>
<module>demo-service</module>
<module>eureka-server</module>
<module>zuul-gateway</module>
</modules>
......
<?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">
<parent>
<artifactId>spring-demo-parent</artifactId>
<groupId>com.freemud.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zuul-gateway</artifactId>
<dependencies>
<!-- 服务网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- 重试机制 ,必须配,否则重试不生效 -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.freemud.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Auther: yong.huang
* @Date: 2018/11/19 11:03
* @Description:
* 调用后端服务时先进行校验
*/
public class AccessFilter extends ZuulFilter {
private static Logger logger = LoggerFactory.getLogger(AccessFilter.class);
private final static String[] notNeedAccessURL=new String[]{"/dictionary/getAllasMap"};
/**
* 过滤器具体逻辑实现
* @return
*/
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
HttpServletResponse response = ctx.getResponse();
String tokenId = request.getHeader("tokenId");
ctx.setResponseStatusCode(200);
ctx.set("isSuccess", true);
return null;
}
/**
* 返回一个字符串代表过滤器的类型
* pre:可以在请求被路由之前调用
* route:在路由请求时候被调用
* post:在route和error过滤器之后被调用
* error:处理请求时发生错误时被调用
* @return
*/
@Override
public String filterType() {
return "pre";
}
/**
* 优先级为0,数字越大,优先级越低
* @return
*/
@Override
public int filterOrder() {
return 0;
}
/**
* 过滤指定url
* @return
*/
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
for(String notNeedAccess : notNeedAccessURL){
if(request.getRequestURI().contains(notNeedAccess)){
return false;
}
}
return true;
}
}
package com.freemud.gatewaydemo;
import com.freemud.filter.AccessFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
/**
* @Auther: yong.huang
* @Date: 2018/11/19 10:52
* @Description:
*/
@EnableZuulProxy
@SpringBootApplication
@EnableFeignClients
public class GatewayApplication {
public static void main(String[] args){
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public AccessFilter preAccessFilter(){
return new AccessFilter();
}
}
package com.freemud.gatewaydemo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* All rights Reserved, Designed By www.freemud.cn
*
* @version V1.0
* @Title: Swagger2
* @Package com.freemud.order
* @Description: Swagger2配置类
* @author: junliang.li
* @date: 2017/5/9 15:01
* @Copyright: 2017 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目的
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Value("${swagger_enable:}")
String swaggerEnable;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable("true".equals(this.swaggerEnable))
.select()
.apis(RequestHandlerSelectors.basePackage("com.freemud.springbootdemo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Demo RESTful APIs")
.description("demo APIs")
.termsOfServiceUrl("http://www.freemud.cn/")
.version("1.0")
.build();
}
}
spring:
application:
name: api-gateway
server:
port: 9080
#log
logging:
config: classpath:logback.xml
swagger_enable: true
eureka:
instance:
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
preferIpAddress: true
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
retryable: true #重试机制,默认重试3次
routes:
api-system:
path: /dict/**
serviceId: dictionary
stripPrefix: true
host:
maxPerRouteConnections: 50
socket-timeout-millis: 60000
sensitiveHeaders:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 60000
ribbon:
ConnectTimeout: 3000
ReadTimeout: 50000
MaxAutoRetries: 1
MaxAutoRetriesNextServer: 2
OkToRetryOnAllOperations: false
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--<springProperty scope="context"-->
<!--name="springAppName"-->
<!--source="spring.application.name"/>-->
<!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%d %p (%file:%line\)- %m%n</pattern>
</encoder>
</appender>
<appender name = "STDOUT_tid" class= "ch.qos.logback.core.ConsoleAppender" >
<layout class = "ch.qos.logback.classic.PatternLayout">
<!--<Pattern >%d{HH:mm} %-5level %logger{36} - [springAppName:${springAppName:-}, TxId : %X{X-B3-TraceId:-} , SpanId : %X{X-B3-SpanId:-}] %msg%n</Pattern >-->
<Pattern >%d{HH:mm} %-5level %logger{36} - [spring-demo,%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %msg%n</Pattern >
</layout >
</appender >
<root level="info">
<appender-ref ref="STDOUT_tid"/>
</root>
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment