Skip to content

Commit 512b09f

Browse files
committed
《Spring Cloud实战》示例更新
1 parent 0645a53 commit 512b09f

File tree

76 files changed

+2463
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+2463
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
### 《Spring Cloud源码分析》系列博文
3131

3232
- [Spring Cloud源码分析(一)Eureka](http://blog.didispace.com/springcloud-sourcecode-eureka/)
33+
- [Spring Cloud源码分析(二)Ribbon](http://blog.didispace.com/springcloud-sourcecode-ribbon/)
3334

3435
### 《Spring Cloud实战》配套示例
3536

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>api-gateway-consul</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>api-gateway-consul</name>
12+
<description>Spring Cloud project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.7.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
28+
<dependency>
29+
<groupId>org.springframework.cloud</groupId>
30+
<artifactId>spring-cloud-starter-zuul</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.cloud</groupId>
35+
<artifactId>spring-cloud-starter-consul-all</artifactId>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<groupId>org.springframework.cloud</groupId>
44+
<artifactId>spring-cloud-dependencies</artifactId>
45+
<version>Brixton.SR5</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.didispace;
2+
3+
import com.didispace.filter.AccessFilter;
4+
import org.springframework.boot.builder.SpringApplicationBuilder;
5+
import org.springframework.cloud.client.SpringCloudApplication;
6+
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@EnableZuulProxy
10+
@SpringCloudApplication
11+
public class Application {
12+
13+
public static void main(String[] args) {
14+
new SpringApplicationBuilder(Application.class).web(true).run(args);
15+
}
16+
17+
// @Bean
18+
// public AccessFilter accessFilter() {
19+
// return new AccessFilter();
20+
// }
21+
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.didispace.filter;
2+
3+
import com.netflix.loadbalancer.IPing;
4+
import com.netflix.zuul.ZuulFilter;
5+
import com.netflix.zuul.context.RequestContext;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.context.annotation.Bean;
9+
10+
import javax.servlet.http.HttpServletRequest;
11+
12+
public class AccessFilter extends ZuulFilter {
13+
14+
private static Logger log = LoggerFactory.getLogger(AccessFilter.class);
15+
16+
@Override
17+
public String filterType() {
18+
return "pre";
19+
}
20+
21+
@Override
22+
public int filterOrder() {
23+
return 0;
24+
}
25+
26+
@Override
27+
public boolean shouldFilter() {
28+
return true;
29+
}
30+
31+
@Override
32+
public Object run() {
33+
RequestContext ctx = RequestContext.getCurrentContext();
34+
HttpServletRequest request = ctx.getRequest();
35+
36+
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
37+
38+
Object accessToken = request.getParameter("accessToken");
39+
if(accessToken == null) {
40+
log.warn("access token is empty");
41+
ctx.setSendZuulResponse(false);
42+
ctx.setResponseStatusCode(401);
43+
return null;
44+
}
45+
log.info("access token ok");
46+
return null;
47+
}
48+
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring.application.name=api-gateway-consul
2+
server.port=5555
3+
4+
# routes to serviceId
5+
zuul.routes.api-a.path=/consul/**
6+
zuul.routes.api-a.serviceId=consul-client-1
7+
8+
# routes to url
9+
zuul.routes.api-a-url.path=/api-a-url/**
10+
zuul.routes.api-a-url.url=http://localhost:8080/
11+
12+
spring.cloud.consul.host=localhost
13+
spring.cloud.consul.port=8500

spring_cloud_in_action/api-gateway/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
<groupId>org.springframework.cloud</groupId>
3535
<artifactId>spring-cloud-starter-eureka</artifactId>
3636
</dependency>
37-
3837
</dependencies>
3938

4039
<dependencyManagement>

spring_cloud_in_action/api-gateway/src/main/java/com/didispace/Application.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.boot.builder.SpringApplicationBuilder;
55
import org.springframework.cloud.client.SpringCloudApplication;
66
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
7+
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
78
import org.springframework.context.annotation.Bean;
89

910
@EnableZuulProxy
@@ -19,4 +20,11 @@ public AccessFilter accessFilter() {
1920
return new AccessFilter();
2021
}
2122

23+
@Bean
24+
public PatternServiceRouteMapper serviceRouteMapper() {
25+
return new PatternServiceRouteMapper(
26+
"(?<name>^.+)-(?<version>v.+$)",
27+
"${version}/${name}");
28+
}
29+
2230
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
7+
@RestController
8+
public class HelloController {
9+
10+
@RequestMapping("/local/hello")
11+
public String hello() {
12+
return "Hello World Local";
13+
}
14+
15+
}

spring_cloud_in_action/api-gateway/src/main/java/com/didispace/filter/AccessFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Object run() {
3131
RequestContext ctx = RequestContext.getCurrentContext();
3232
HttpServletRequest request = ctx.getRequest();
3333

34-
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
34+
log.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString());
3535

3636
Object accessToken = request.getParameter("accessToken");
3737
if(accessToken == null) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.didispace.filter;
2+
3+
import com.netflix.zuul.ZuulFilter;
4+
import com.netflix.zuul.context.RequestContext;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.stereotype.Component;
8+
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
/**
12+
* 捕获为处理的异常统一做一些处理,让`SendErrorFilter`可以正确的返回异常信息
13+
*/
14+
//@Component
15+
public class ErrorFilter extends ZuulFilter {
16+
17+
Logger log = LoggerFactory.getLogger(ErrorFilter.class);
18+
19+
@Override
20+
public String filterType() {
21+
return "error";
22+
}
23+
24+
@Override
25+
public int filterOrder() {
26+
return 10;
27+
}
28+
29+
@Override
30+
public boolean shouldFilter() {
31+
return true;
32+
}
33+
34+
@Override
35+
public Object run() {
36+
RequestContext ctx = RequestContext.getCurrentContext();
37+
Throwable throwable = RequestContext.getCurrentContext().getThrowable();
38+
log.error("this is a ErrorFilter : {}", throwable.getCause().getMessage());
39+
ctx.set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
40+
ctx.set("error.exception", throwable.getCause());
41+
return null;
42+
}
43+
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.didispace.filter;
2+
3+
import com.netflix.zuul.ZuulFilter;
4+
import com.netflix.zuul.context.RequestContext;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.stereotype.Component;
8+
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
@Component
12+
public class ThrowExceptionFilter extends ZuulFilter {
13+
14+
private static Logger log = LoggerFactory.getLogger(ThrowExceptionFilter.class);
15+
16+
@Override
17+
public String filterType() {
18+
return "pre";
19+
}
20+
21+
@Override
22+
public int filterOrder() {
23+
return 0;
24+
}
25+
26+
@Override
27+
public boolean shouldFilter() {
28+
return true;
29+
}
30+
31+
@Override
32+
public Object run() {
33+
log.info("This is a pre filter, it will throw a RuntimeException");
34+
RequestContext ctx = RequestContext.getCurrentContext();
35+
try {
36+
doSomething();
37+
} catch (Exception e) {
38+
ctx.set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
39+
ctx.set("error.exception", e);
40+
// ctx.set("error.message", "有一些错误发生");
41+
}
42+
return null;
43+
}
44+
45+
private void doSomething() {
46+
throw new RuntimeException("Exist some errors...");
47+
}
48+
49+
}
Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,75 @@
11
spring.application.name=api-gateway
22
server.port=5555
33

4+
# eureka
5+
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
6+
7+
# routes to url
8+
zuul.routes.api-a-url.path=/api-a-url/**
9+
zuul.routes.api-a-url.url=http://localhost:8001/
10+
11+
# ribbon with out eureka
12+
#zuul.routes.api-d.path=/ddd/**
13+
#zuul.routes.api-d.serviceId=hello
14+
#ribbon.eureka.enabled=false
15+
#hello.ribbon.listOfServers=http://localhost:8001/,http://localhost:8002/
16+
417
# routes to serviceId
5-
zuul.routes.api-a.path=/api-a/**
18+
zuul.routes.api-a.path=/api/a/**
619
zuul.routes.api-a.serviceId=hello-service
720

821
zuul.routes.api-b.path=/api-b/**
9-
zuul.routes.api-b.serviceId=feign-consumer
22+
zuul.routes.api-b.serviceId=hello-service
23+
#zuul.routes.api-b.strip-prefix=true
1024

11-
# routes to url
12-
zuul.routes.api-a-url.path=/api-a-url/**
13-
zuul.routes.api-a-url.url=http://localhost:8080/
25+
zuul.routes.api-c.path=/ddd/**
26+
zuul.routes.api-c.serviceId=hello-service
27+
28+
#zuul.routes.api-d.path=/ddd/hello
29+
#zuul.routes.api-d.serviceId=hello-service2
30+
31+
# routes to serviceId£¬'hello-service' is service-name, value is path
32+
#zuul.routes.hello-service=/eee/**
33+
34+
# routes to local forward
35+
zuul.routes.api-b-url.path=/api-b-url/**
36+
zuul.routes.api-b-url.url=forward:/local
37+
38+
# route connection
39+
zuul.host.max-per-route-connections=20
40+
zuul.host.max-total-connections=200
41+
42+
# grobal prefix
43+
#zuul.prefix=/api
44+
45+
# grobal strip prefix, default is true(but it has some bugs, when routes path contains zuul.prefix's value)
46+
#zuul.strip-prefix=false
47+
48+
# router strip prefix
49+
#zuul.routes.api-b.strip-prefix=false
50+
51+
# Set of service names not to consider for proxying automatically.
52+
# By default all services in the discovery client will be proxied.
53+
# For example, set zuul.ignored-services=*, then only routes configed by zuul.routes.* will be proxied.
54+
# And default service proxy will disabled.
55+
#zuul.ignored-services=*
56+
57+
# ignored path;
58+
# o.s.c.n.z.f.pre.PreDecorationFilter : No route found for uri: /xxx/yyy/zzz
59+
#zuul.ignored-patterns=/**/hello/**
60+
61+
# timeout setting
62+
#ribbon.ConnectTimeout=3000
63+
#ribbon.ReadTimeout=1000
64+
65+
# fileupload setting
66+
#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
67+
#ribbon.ConnectTimeout=3000
68+
#ribbon.ReadTimeout=60000
69+
70+
# Disable Zuul Filters
71+
# zuul.<SimpleClassName>.<filterType>.disable=true
72+
zuul.AccessFilter.pre.disable=true
1473

15-
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
74+
# ZuulServlet path
75+
#zuul.servlet-path=/zuul

0 commit comments

Comments
 (0)