Skip to content

Commit

Permalink
Merge pull request #6 from aaric/dev-aaric
Browse files Browse the repository at this point in the history
0.0.5-SNAPSHOT Zuul
  • Loading branch information
aaric authored Dec 13, 2019
2 parents f848f68 + fdc1437 commit 896b310
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public class UserServiceFallback implements UserServiceFeign {
@Override
public User getUser(Integer id) {
log.info("currentThreadName: {}", Thread.currentThread().getName());
return null;
return new User(0, "Waiting UserService$getUser ...");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<Order> getDetail(@RequestParam("userId") Integer userId) {
}

@Override
@GetMapping("/user/getCustomTitle")
@GetMapping("/order/getCustomTitle")
public String getCustomTitle() {
return customTitle;
}
Expand Down
2 changes: 1 addition & 1 deletion api-order-server/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ spring:
port: 8500
discovery:
instance-id: ${spring.application.name}_${spring.cloud.client.ip-address}_${server.port}
tags: app=order, version=0.0.4-SNAPSHOT
tags: app=order, version=0.0.5-SNAPSHOT
healthCheckInterval: 15s
prefer-ip-address: true
config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public interface OrderService {
@GetMapping("/order/getDetail")
List<Order> getDetail(@RequestParam("userId") Integer userId);

@GetMapping("/user/getCustomTitle")
@GetMapping("/order/getCustomTitle")
String getCustomTitle();
}
2 changes: 1 addition & 1 deletion api-user-server/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ spring:
port: 8500
discovery:
instance-id: ${spring.application.name}_${spring.cloud.client.ip-address}_${server.port}
tags: app=user, version=0.0.4-SNAPSHOT
tags: app=user, version=0.0.5-SNAPSHOT
healthCheckInterval: 15s
prefer-ip-address: true
config:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {

allprojects {
group 'com.incarcloud'
version '0.0.4-SNAPSHOT'
version '0.0.5-SNAPSHOT'
}

subprojects {
Expand Down
2 changes: 1 addition & 1 deletion config-server/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ spring:
port: 8500
discovery:
instance-id: ${spring.application.name}_${spring.cloud.client.ip-address}_${server.port}
tags: app=config, version=0.0.4-SNAPSHOT
tags: app=config, version=0.0.5-SNAPSHOT
healthCheckInterval: 15s
prefer-ip-address: true
config:
Expand Down
9 changes: 9 additions & 0 deletions configstore/gateway-server-dev.yml
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
7 changes: 7 additions & 0 deletions gateway-server/build.gradle
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 gateway-server/src/main/java/com/incarcloud/AppGateway.java
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 gateway-server/src/main/java/com/incarcloud/config/ZuulConfig.java
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();
}
}
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;
}
}
56 changes: 56 additions & 0 deletions gateway-server/src/main/resources/bootstrap.yml
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
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include 'config-server'
include 'gateway-server'
include 'api-user'
include 'api-user-server'
include 'api-order'
Expand Down

0 comments on commit 896b310

Please sign in to comment.