-
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.
Merge pull request #6 from aaric/dev-aaric
0.0.5-SNAPSHOT Zuul
- Loading branch information
Showing
14 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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 @@ | ||
# Zuul settings | ||
zuul: | ||
routes: | ||
api-user-server: | ||
path: /api/usergroup/** | ||
serviceId: api-user-server | ||
api-order-server: | ||
path: /api/ordergroup/** | ||
serviceId: api-order-server |
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,7 @@ | ||
apply plugin: 'org.springframework.boot' | ||
|
||
dependencies { | ||
compile "org.springframework.cloud:spring-cloud-starter-consul-discovery:${springCloudVersion}" | ||
compile "org.springframework.cloud:spring-cloud-config-client:${springCloudVersion}" | ||
compile "org.springframework.cloud:spring-cloud-starter-netflix-zuul:${springCloudVersion}" | ||
} |
25 changes: 25 additions & 0 deletions
25
gateway-server/src/main/java/com/incarcloud/AppGateway.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,25 @@ | ||
package com.incarcloud; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; | ||
|
||
/** | ||
* API网关启动类 | ||
* | ||
* @author Aaric, created on 2019-12-11T10:50. | ||
* @version 0.0.5-SNAPSHOT | ||
*/ | ||
@EnableZuulProxy | ||
@SpringBootApplication | ||
public class AppGateway { | ||
|
||
/** | ||
* Main | ||
* | ||
* @param args 命令行参数 | ||
*/ | ||
public static void main(String[] args) { | ||
SpringApplication.run(AppGateway.class, args); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
gateway-server/src/main/java/com/incarcloud/config/ZuulConfig.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,20 @@ | ||
package com.incarcloud.config; | ||
|
||
import com.incarcloud.filter.TokenFilter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* ZuulConfig | ||
* | ||
* @author Aaric, created on 2019-12-13T10:32. | ||
* @version 0.0.5-SNAPSHOT | ||
*/ | ||
@Configuration | ||
public class ZuulConfig { | ||
|
||
@Bean | ||
public TokenFilter tokenFilter() { | ||
return new TokenFilter(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
gateway-server/src/main/java/com/incarcloud/filter/TokenFilter.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,77 @@ | ||
package com.incarcloud.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.apache.commons.lang.StringUtils; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* TokenFilter | ||
* | ||
* @author Aaric, created on 2019-12-13T09:12. | ||
* @version 0.0.5-SNAPSHOT | ||
*/ | ||
@Slf4j | ||
public class TokenFilter extends ZuulFilter { | ||
|
||
/** | ||
* Zuul支持的过滤器: | ||
* <u> | ||
* <li>pre-路由前</li> | ||
* <li>route-路由时</li> | ||
* <li>post-路由完毕</li> | ||
* <li>error-发生错误时</li> | ||
* </u> | ||
* | ||
* @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 { | ||
RequestContext context = RequestContext.getCurrentContext(); | ||
HttpServletRequest request = context.getRequest(); | ||
HttpServletResponse response = context.getResponse(); | ||
|
||
// 获取token字符串 | ||
String token = request.getParameter("token"); | ||
log.info("token: {}", token); | ||
|
||
// 简单验证token字符串 | ||
if (StringUtils.isEmpty(token)) { | ||
context.setSendZuulResponse(false); | ||
context.setResponseStatusCode(401); | ||
response.setHeader("Content-Type", "text/html;charset=UTF-8"); | ||
context.setResponseBody("token is required."); | ||
} | ||
|
||
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,56 @@ | ||
# Tomcat settings | ||
server: | ||
port: ${TOMCAT_SERVER_PORT:81} | ||
|
||
# Management settings | ||
management: | ||
endpoints: | ||
web: | ||
exposure: | ||
include: "*" | ||
|
||
# Logging settings | ||
logging: | ||
level: | ||
root: WARN | ||
org: | ||
springframework: | ||
security: INFO | ||
web: ERROR | ||
hibernate: INFO | ||
com: | ||
incarcloud: DEBUG | ||
file: | ||
path: ${LOGGING_FILE_PATH:./} | ||
name: gateway.log | ||
max-size: ${LOGGING_FILE_MAX_SIZE:20MB} | ||
|
||
# Spring settings | ||
spring: | ||
application: | ||
name: gateway-server | ||
cloud: | ||
consul: | ||
host: 10.0.11.21 | ||
port: 8500 | ||
discovery: | ||
register: false | ||
instance-id: ${spring.application.name}_${spring.cloud.client.ip-address}_${server.port} | ||
tags: app=order, version=0.0.5-SNAPSHOT | ||
healthCheckInterval: 15s | ||
prefer-ip-address: true | ||
config: | ||
profile: dev | ||
discovery: | ||
enabled: true | ||
serviceId: config-server | ||
|
||
# Zuul settings | ||
#zuul: | ||
# routes: | ||
# api-user-server: | ||
# path: /api/usergroup/** | ||
# serviceId: api-user-server | ||
# api-order-server: | ||
# path: /api/ordergroup/** | ||
# serviceId: api-order-server |
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