diff --git a/api-order-server/src/main/java/com/incarcloud/api/fallback/UserServiceFallback.java b/api-order-server/src/main/java/com/incarcloud/api/fallback/UserServiceFallback.java index 5e53ec1..d157129 100644 --- a/api-order-server/src/main/java/com/incarcloud/api/fallback/UserServiceFallback.java +++ b/api-order-server/src/main/java/com/incarcloud/api/fallback/UserServiceFallback.java @@ -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 ..."); } } diff --git a/api-order-server/src/main/java/com/incarcloud/api/service/impl/OrderServiceImpl.java b/api-order-server/src/main/java/com/incarcloud/api/service/impl/OrderServiceImpl.java index e03322c..e168e9b 100644 --- a/api-order-server/src/main/java/com/incarcloud/api/service/impl/OrderServiceImpl.java +++ b/api-order-server/src/main/java/com/incarcloud/api/service/impl/OrderServiceImpl.java @@ -49,7 +49,7 @@ public List getDetail(@RequestParam("userId") Integer userId) { } @Override - @GetMapping("/user/getCustomTitle") + @GetMapping("/order/getCustomTitle") public String getCustomTitle() { return customTitle; } diff --git a/api-order-server/src/main/resources/bootstrap.yml b/api-order-server/src/main/resources/bootstrap.yml index e3a3d12..475d267 100644 --- a/api-order-server/src/main/resources/bootstrap.yml +++ b/api-order-server/src/main/resources/bootstrap.yml @@ -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: diff --git a/api-order/src/main/java/com/incarcloud/api/service/OrderService.java b/api-order/src/main/java/com/incarcloud/api/service/OrderService.java index 290b09f..c1a1867 100644 --- a/api-order/src/main/java/com/incarcloud/api/service/OrderService.java +++ b/api-order/src/main/java/com/incarcloud/api/service/OrderService.java @@ -17,6 +17,6 @@ public interface OrderService { @GetMapping("/order/getDetail") List getDetail(@RequestParam("userId") Integer userId); - @GetMapping("/user/getCustomTitle") + @GetMapping("/order/getCustomTitle") String getCustomTitle(); } diff --git a/api-user-server/src/main/resources/bootstrap.yml b/api-user-server/src/main/resources/bootstrap.yml index d5afd5a..4d47467 100644 --- a/api-user-server/src/main/resources/bootstrap.yml +++ b/api-user-server/src/main/resources/bootstrap.yml @@ -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: diff --git a/build.gradle b/build.gradle index 8733c27..8673468 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ buildscript { allprojects { group 'com.incarcloud' - version '0.0.4-SNAPSHOT' + version '0.0.5-SNAPSHOT' } subprojects { diff --git a/config-server/src/main/resources/bootstrap.yml b/config-server/src/main/resources/bootstrap.yml index 0e90bf5..281e9a4 100644 --- a/config-server/src/main/resources/bootstrap.yml +++ b/config-server/src/main/resources/bootstrap.yml @@ -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: diff --git a/configstore/gateway-server-dev.yml b/configstore/gateway-server-dev.yml new file mode 100644 index 0000000..f8aa3bb --- /dev/null +++ b/configstore/gateway-server-dev.yml @@ -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 diff --git a/gateway-server/build.gradle b/gateway-server/build.gradle new file mode 100644 index 0000000..9c52cae --- /dev/null +++ b/gateway-server/build.gradle @@ -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}" +} diff --git a/gateway-server/src/main/java/com/incarcloud/AppGateway.java b/gateway-server/src/main/java/com/incarcloud/AppGateway.java new file mode 100644 index 0000000..92563c4 --- /dev/null +++ b/gateway-server/src/main/java/com/incarcloud/AppGateway.java @@ -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); + } +} diff --git a/gateway-server/src/main/java/com/incarcloud/config/ZuulConfig.java b/gateway-server/src/main/java/com/incarcloud/config/ZuulConfig.java new file mode 100644 index 0000000..9d9715a --- /dev/null +++ b/gateway-server/src/main/java/com/incarcloud/config/ZuulConfig.java @@ -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(); + } +} diff --git a/gateway-server/src/main/java/com/incarcloud/filter/TokenFilter.java b/gateway-server/src/main/java/com/incarcloud/filter/TokenFilter.java new file mode 100644 index 0000000..86adbc1 --- /dev/null +++ b/gateway-server/src/main/java/com/incarcloud/filter/TokenFilter.java @@ -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支持的过滤器: + * + *
  • pre-路由前
  • + *
  • route-路由时
  • + *
  • post-路由完毕
  • + *
  • error-发生错误时
  • + *
    + * + * @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; + } +} diff --git a/gateway-server/src/main/resources/bootstrap.yml b/gateway-server/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..b5b7a92 --- /dev/null +++ b/gateway-server/src/main/resources/bootstrap.yml @@ -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 diff --git a/settings.gradle b/settings.gradle index 78a1e6c..fdd69d5 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ include 'config-server' +include 'gateway-server' include 'api-user' include 'api-user-server' include 'api-order'