Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev -> master #14

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 222 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,225 @@ target
2-Dalston版教程示例/trace-2/build/bootstrap.json
2-Dalston版教程示例/trace-2/build/springAppName_IS_UNDEFINED.json
2-Dalston版教程示例/trace-2/build/trace-2.json
### Eclipse template

.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/
cmake-build-release/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

/2-Dalston/eureka-consumer-ribbon/eureka-consumer-ribbon.iml
/2-Dalston/eureka-consumer-ribbon/target/
1-Brixton/Chapter1-1-1/compute-service/.classpath
1-Brixton/Chapter1-1-1/compute-service/.project
1-Brixton/Chapter1-1-1/eureka-server/.classpath
1-Brixton/Chapter1-1-1/eureka-server/.project
1-Brixton/Chapter1-1-2/eureka-feign/.classpath
1-Brixton/Chapter1-1-2/eureka-feign/.project
1-Brixton/Chapter1-1-2/eureka-ribbon/.classpath
1-Brixton/Chapter1-1-2/eureka-ribbon/.project
1-Brixton/Chapter1-1-4/config-client/.classpath
1-Brixton/Chapter1-1-4/config-client/.project
1-Brixton/Chapter1-1-4/config-server/.classpath
1-Brixton/Chapter1-1-4/config-server/.project
1-Brixton/Chapter1-1-5/api-gateway/.classpath
1-Brixton/Chapter1-1-5/api-gateway/.project
1-Brixton/Chapter1-1-5/service-A/.classpath
1-Brixton/Chapter1-1-5/service-A/.project
1-Brixton/Chapter1-1-5/service-B/.classpath
1-Brixton/Chapter1-1-5/service-B/.project
1-Brixton/Chapter1-1-7/config-client-eureka-kafka/.classpath
1-Brixton/Chapter1-1-7/config-client-eureka-kafka/.project
1-Brixton/Chapter1-1-7/config-client-eureka/.classpath
1-Brixton/Chapter1-1-7/config-client-eureka/.project
1-Brixton/Chapter1-1-7/config-server-eureka-kafka/.classpath
1-Brixton/Chapter1-1-7/config-server-eureka-kafka/.project
1-Brixton/Chapter1-1-7/config-server-eureka/.classpath
1-Brixton/Chapter1-1-7/config-server-eureka/.project
2-Dalston/config-server-git/.classpath
2-Dalston/config-server-git/.project
2-Dalston/consul-consumer/.classpath
2-Dalston/consul-consumer/.project
2-Dalston/eureka-consumer-feign-hystrix/.classpath
2-Dalston/eureka-consumer-feign-hystrix/.project
2-Dalston/eureka-consumer-ribbon/.classpath
2-Dalston/eureka-consumer-ribbon/.project
2-Dalston/eureka-feign-api/.classpath
2-Dalston/eureka-feign-api/.project
2-Dalston/eureka-feign-client/.classpath
2-Dalston/eureka-feign-client/.project
2-Dalston/eureka-feign-consumer/.classpath
2-Dalston/eureka-feign-consumer/.project
2-Dalston/hystrix-collapser-provider/.classpath
2-Dalston/hystrix-collapser-provider/.project
spring_cloud_in_action/.project
spring_cloud_in_action/api-gateway-consul/.classpath
spring_cloud_in_action/api-gateway-consul/.project
spring_cloud_in_action/api-gateway-dynamic-filter/.classpath
spring_cloud_in_action/api-gateway-dynamic-filter/.project
spring_cloud_in_action/api-gateway-dynamic-route/.classpath
spring_cloud_in_action/api-gateway-dynamic-route/.project
spring_cloud_in_action/api-gateway-exception/.classpath
spring_cloud_in_action/api-gateway-exception/.project
spring_cloud_in_action/config-client-eureka-rabbit/.classpath
spring_cloud_in_action/config-client-eureka-rabbit/.project
spring_cloud_in_action/config-server-eureka-rabbit/.classpath
spring_cloud_in_action/config-server-eureka-rabbit/.project
spring_cloud_in_action/consul-client-1/.classpath
spring_cloud_in_action/consul-client-1/.project
spring_cloud_in_action/eureka-server-ha/.classpath
spring_cloud_in_action/eureka-server-ha/.project
spring_cloud_in_action/feign-consumer/.classpath
spring_cloud_in_action/feign-consumer/.project
spring_cloud_in_action/hello-service-api/.classpath
spring_cloud_in_action/hello-service-api/.project
spring_cloud_in_action/hello-service/.classpath
spring_cloud_in_action/hello-service/.project
spring_cloud_in_action/rabbitmq-hello/.classpath
spring_cloud_in_action/rabbitmq-hello/.project
spring_cloud_in_action/ribbon-consumer/.classpath
spring_cloud_in_action/ribbon-consumer/.project
spring_cloud_in_action/spring-boot-hello/.classpath
spring_cloud_in_action/spring-boot-hello/.project
spring_cloud_in_action/stream-consumer/.classpath
spring_cloud_in_action/stream-consumer/.project
spring_cloud_in_action/stream-producer/.classpath
spring_cloud_in_action/stream-producer/.project
spring_cloud_in_action/trace-1/.classpath
spring_cloud_in_action/trace-1/.project
spring_cloud_in_action/trace-2/.classpath
spring_cloud_in_action/trace-2/.project
spring_cloud_in_action/trace-stream-1/.classpath
spring_cloud_in_action/trace-stream-1/.project
spring_cloud_in_action/trace-stream-2/.classpath
spring_cloud_in_action/trace-stream-2/.project
spring_cloud_in_action/zipkin-server-stream-mysql/.classpath
spring_cloud_in_action/zipkin-server-stream-mysql/.project
spring_cloud_in_action/zipkin-server-stream/.classpath
spring_cloud_in_action/zipkin-server-stream/.project
spring_cloud_in_action/zipkin-server/.classpath
spring_cloud_in_action/zipkin-server/.project
/build/
build/
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.didispace;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/*网关服务的加入,外部客户端访问我们的系统已经有了统一入口,既然这些校验与具体业务无关,那何不在请求到达的时候就完成校验和过滤,而不是转发后再过滤而导致更长的请求延迟。
同时,通过在网关中完成校验和过滤,微服务应用端就可以去除各种复杂的过滤器和拦截器了,这使得微服务应用的接口开发和测试复杂度也得到了相应的降低。

为了在API网关中实现对客户端请求的校验,我们将需要使用到Spring Cloud Zuul的另外一个核心功能:过滤器。

Zuul允许开发者在API网关上通过定义过滤器来实现对请求的拦截与过滤,实现的方法非常简单,我们只需要继承ZuulFilter抽象类并实现它定义的四个抽象函数就可以完成对请求的拦截和过滤了。

可以根据自己的需要在服务网关上定义一些与业务无关的通用逻辑实现对请求的过滤和拦截,比如:签名校验、权限校验、请求限流等功能。
*/
public class AccessFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(AccessFilter.class);

/*filterType:过滤器的类型,它决定过滤器在请求的哪个生命周期中执行。
这里定义为pre,代表会在请求被路由之前执行。*/
@Override
public String filterType() {
return "pre";
}

/*filterOrder:过滤器的执行顺序。
当请求在一个阶段中存在多个过滤器时,需要根据该方法返回的值来依次执行。*/
@Override
public int filterOrder() {
return 0;
}

/*shouldFilter:判断该过滤器是否需要被执行。
这里我们直接返回了true,因此该过滤器对所有请求都会生效。
实际运用中我们可以利用该函数来指定过滤器的有效范围。*/
@Override
public boolean shouldFilter() {
return true;
}

/*run:过滤器的具体逻辑。
这里我们通过ctx.setSendZuulResponse(false)令zuul过滤该请求,不对其进行路由,
然后通过ctx.setResponseStatusCode(401)设置了其返回的错误码,当然我们也可以进一步优化我们的返回,
比如,通过ctx.setResponseBody(body)对返回body内容进行编辑等。*/
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();

log.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString());

Object accessToken = request.getParameter("accessToken");
if (accessToken == null) {
log.warn("access token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
return null;
}
log.info("access token ok");
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;

@EnableZuulProxy
@SpringCloudApplication
Expand All @@ -12,4 +13,10 @@ public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}

/*创建具体的Bean启动该过滤器*/
@Bean
public AccessFilter accessFilter() {
return new AccessFilter();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</properties>

<dependencies>
<!--引入服务网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
Expand Down
25 changes: 25 additions & 0 deletions 2-Dalston/api-gateway/src/main/java/com/didispace/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.didispace;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/*使用@EnableZuulProxy注解开启Zuul的功能*/
@EnableZuulProxy
@SpringCloudApplication
public class Application {

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
/*
服务网关是微服务架构中一个不可或缺的部分。

通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由、均衡负载功能之外,它还具备了权限控制等功能。

Spring Cloud Netflix中的Zuul就担任了这样的一个角色,
为微服务架构提供了前门保护的作用,
同时将权限控制这些较重的非业务逻辑内容迁移到服务路由层面,
使得服务集群主体能够具备更高的可复用性和可测试性。
*/
35 changes: 35 additions & 0 deletions 2-Dalston/api-gateway/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
spring:
application:
name: api-gateway

server:
port: 1101

zuul:
routes:
# single-instance 单实例配置:通过一组zuul.routes.<route>.path与zuul.routes.<route>.url参数对的方式配置
api-a:
path: /api-a/**
url: http://localhost:2001/
# multi-instance 多实例配置:通过一组zuul.routes.<route>.path与zuul.routes.<route>.serviceId参数对的方式配置
# <route>可以指定为任意的路由名称

api-b:
path: /api-b/**
serviceId: api-b

api-b:
ribbon:
listOfServers: http://localhost:2001/, http://localhost:2002/
# 由于存在多个实例,API网关在进行路由转发时需要实现负载均衡策略,于是这里需要Spring Cloud Ribbon的配合,Spring Cloud Zuul中自带了对Ribbon的依赖。


# 除了使用path与serviceId映射的配置方式之外,还有一种更简洁的配置方式:zuul.routes.<serviceId>=<path>,其中<serviceId>用来指定路由的具体服务名,<path>用来配置匹配的请求表达式。

# 没有配置任何实例地址的情况下,外部请求经过API网关的时候,它是如何被解析并转发到服务具体实例的呢?
# 在Spring Cloud Netflix中,Zuul巧妙的整合了Eureka来实现面向服务的路由。
# 实际上,我们可以直接将API网关也看做是Eureka服务治理下的一个普通微服务应用。
# 它除了会将自己注册到Eureka服务注册中心上之外,也会从注册中心获取所有服务以及它们的实例清单。
# 所以,在Eureka的帮助下,API网关服务本身就已经维护了系统中所有serviceId与实例地址的映射关系。
# 当有外部请求到达API网关的时候,根据请求的URL路径找到最佳匹配的path规则,API网关就可以知道要将该请求路由到哪个具体的serviceId上去。
# 由于在API网关中已经知道serviceId对应服务实例的地址清单,那么只需要通过Ribbon的负载均衡策略,直接在这些清单中选择一个具体的实例进行转发就能完成路由工作了。
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

//@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public class ConfigClientApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
new SpringApplicationBuilder(ConfigClientApplication.class).web(true).run(args);
}

}
Loading