forked from dyc87112/SpringCloud-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,038 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
31 changes: 31 additions & 0 deletions
31
spring_cloud_in_action/api-gateway-consul/filter/post/PostFilter.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.didispace.filter.post | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
import com.netflix.zuul.ZuulFilter | ||
|
||
class PostFilter extends ZuulFilter{ | ||
|
||
Logger log = LoggerFactory.getLogger(PostFilter.class); | ||
|
||
@Override | ||
String filterType() { | ||
return "post" | ||
} | ||
|
||
@Override | ||
int filterOrder() { | ||
return 2000 | ||
} | ||
|
||
@Override | ||
boolean shouldFilter() { | ||
return true | ||
} | ||
|
||
@Override | ||
Object run() { | ||
log.info("this is a post filter aaaaaaa"); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
spring_cloud_in_action/api-gateway-consul/filter/pre/PreFilter.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.didispace.filter.pre | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import com.netflix.zuul.ZuulFilter; | ||
import com.netflix.zuul.context.RequestContext; | ||
|
||
class PreFilter extends ZuulFilter { | ||
|
||
Logger log = LoggerFactory.getLogger(PreFilter.class); | ||
|
||
@Override | ||
String filterType() { | ||
return "pre" | ||
} | ||
|
||
@Override | ||
int filterOrder() { | ||
return 1000 | ||
} | ||
|
||
@Override | ||
boolean shouldFilter() { | ||
return true | ||
} | ||
|
||
@Override | ||
Object run() { | ||
log.info("this is a pre filter") | ||
HttpServletRequest request = RequestContext.getCurrentContext().getRequest() | ||
log.info(String.format("send %s request to %s", request.getMethod(), request.getRequestURL().toString())) | ||
return null | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
spring_cloud_in_action/api-gateway-dynamic-filter/filter/post/PostFilter.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.didispace.filter.post | ||
|
||
import com.netflix.zuul.ZuulFilter | ||
import com.netflix.zuul.context.RequestContext | ||
import com.netflix.zuul.http.HttpServletResponseWrapper | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.cloud.netflix.zuul.util.RequestUtils | ||
|
||
import javax.servlet.http.HttpServletResponse | ||
|
||
class PostFilter extends ZuulFilter{ | ||
|
||
Logger log = LoggerFactory.getLogger(PostFilter.class) | ||
|
||
@Override | ||
String filterType() { | ||
return "post" | ||
} | ||
|
||
@Override | ||
int filterOrder() { | ||
return 2000 | ||
} | ||
|
||
@Override | ||
boolean shouldFilter() { | ||
return true | ||
} | ||
|
||
@Override | ||
Object run() { | ||
log.info("debug request : {}", RequestContext.getCurrentContext().getBoolean("debugRequest")) | ||
log.info("this is a post filter: Receive response") | ||
HttpServletResponse response = RequestContext.getCurrentContext().getResponse() | ||
response.getOutputStream().print(", I am zhaiyongchao") | ||
response.flushBuffer() | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
spring_cloud_in_action/api-gateway-dynamic-filter/filter/pre/PreFilter.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.didispace.filter.pre | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import com.netflix.zuul.ZuulFilter; | ||
import com.netflix.zuul.context.RequestContext; | ||
|
||
class PreFilter extends ZuulFilter { | ||
|
||
Logger log = LoggerFactory.getLogger(PreFilter.class) | ||
|
||
@Override | ||
String filterType() { | ||
return "pre" | ||
} | ||
|
||
@Override | ||
int filterOrder() { | ||
return 1000 | ||
} | ||
|
||
@Override | ||
boolean shouldFilter() { | ||
return true | ||
} | ||
|
||
@Override | ||
Object run() { | ||
HttpServletRequest request = RequestContext.getCurrentContext().getRequest() | ||
log.info("this is a pre filter: Send {} request to {}", request.getMethod(), request.getRequestURL().toString()) | ||
return null | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.didispace</groupId> | ||
<artifactId>api-gateway-dynamic-filter</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>api-gateway-dynamic-filter</name> | ||
<description>Spring Cloud project</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.3.7.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-zuul</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-eureka</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy-all</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>Brixton.SR5</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
38 changes: 38 additions & 0 deletions
38
...g_cloud_in_action/api-gateway-dynamic-filter/src/main/java/com/didispace/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.didispace; | ||
|
||
import com.netflix.zuul.FilterFileManager; | ||
import com.netflix.zuul.FilterLoader; | ||
import com.netflix.zuul.groovy.GroovyCompiler; | ||
import com.netflix.zuul.groovy.GroovyFileFilter; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.cloud.client.SpringCloudApplication; | ||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@EnableZuulProxy | ||
@EnableConfigurationProperties({FilterConfiguration.class}) | ||
@SpringCloudApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
new SpringApplicationBuilder(Application.class).web(true).run(args); | ||
} | ||
|
||
@Bean | ||
public FilterLoader filterLoader(FilterConfiguration filterConfiguration) { | ||
FilterLoader filterLoader = FilterLoader.getInstance(); | ||
filterLoader.setCompiler(new GroovyCompiler()); | ||
try { | ||
FilterFileManager.setFilenameFilter(new GroovyFileFilter()); | ||
FilterFileManager.init( | ||
filterConfiguration.getInterval(), | ||
filterConfiguration.getRoot() + "/pre", | ||
filterConfiguration.getRoot() + "/post"); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return filterLoader; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...in_action/api-gateway-dynamic-filter/src/main/java/com/didispace/FilterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.didispace; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* @author 翟永超 | ||
* @create 2016/12/11. | ||
* @blog http://blog.didispace.com | ||
*/ | ||
@ConfigurationProperties("zuul.filter") | ||
public class FilterConfiguration { | ||
|
||
private String root; | ||
private Integer interval; | ||
|
||
public String getRoot() { | ||
return root; | ||
} | ||
|
||
public void setRoot(String root) { | ||
this.root = root; | ||
} | ||
|
||
public Integer getInterval() { | ||
return interval; | ||
} | ||
|
||
public void setInterval(Integer interval) { | ||
this.interval = interval; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
spring_cloud_in_action/api-gateway-dynamic-filter/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
spring.application.name=api-gateway | ||
server.port=5555 | ||
|
||
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ | ||
|
||
zuul.filter.root=filter | ||
zuul.filter.interval=5 | ||
|
||
#zuul.debug.request=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.didispace</groupId> | ||
<artifactId>api-gateway-dynamic-route</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>api-gateway-dynamic-route</name> | ||
<description>Spring Cloud project</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.3.7.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-zuul</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-eureka</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-config</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>Brixton.SR5</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
26 changes: 26 additions & 0 deletions
26
...ng_cloud_in_action/api-gateway-dynamic-route/src/main/java/com/didispace/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.didispace; | ||
|
||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.cloud.client.SpringCloudApplication; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; | ||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@EnableZuulProxy | ||
@SpringCloudApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
new SpringApplicationBuilder(Application.class).web(true).run(args); | ||
} | ||
|
||
@Bean | ||
@RefreshScope | ||
@ConfigurationProperties("zuul") | ||
public ZuulProperties zuulProperties() { | ||
return new ZuulProperties(); | ||
} | ||
|
||
} |
Oops, something went wrong.