Skip to content

Commit e10ad0c

Browse files
committed
init
1 parent f35305d commit e10ad0c

File tree

1,763 files changed

+69735
-205
lines changed

Some content is hidden

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

1,763 files changed

+69735
-205
lines changed

.gitignore

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,86 @@
1-
# Compiled class file
2-
*.class
1+
# mac
2+
*.DS_Store
3+
# Output Directory
4+
bin/
5+
target/
6+
tmp/
37

4-
# Log file
5-
*.log
8+
# C pre-compile
9+
*.gch
10+
*.pch
11+
12+
# C compile
13+
*.a
14+
*.o
15+
*.ko
16+
*.la
17+
*.lo
18+
*.obj
19+
*.elf
20+
*.so
21+
*.so.*
22+
*.dylib
23+
*.exe
24+
*.lib
25+
*.dll
26+
*.out
27+
*.app
28+
*.hex
629

7-
# BlueJ files
8-
*.ctxt
30+
# Debug files
31+
*.dSYM/
932

10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
33+
# Java
34+
*.class
1235

13-
# Package Files #
36+
# Java Package Files
1437
*.jar
1538
*.war
1639
*.ear
17-
*.zip
18-
*.tar.gz
19-
*.rar
2040

2141
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2242
hs_err_pid*
43+
44+
# Zip Files
45+
*.rar
46+
*.zip
47+
*.7z
48+
*.tar
49+
*.gz
50+
51+
# Ant
52+
#build/
53+
54+
# Compiled Python
55+
__pycache__/
56+
*.py[cod]
57+
*py.class
58+
59+
# Eclipse
60+
.settings/
61+
.classpath
62+
.project
63+
64+
65+
# IntelliJ, based on http://devnet.jetbrains.net/docs/DOC-1186
66+
.idea/
67+
*.iml
68+
69+
70+
# logs and trace
71+
*.log
72+
*.trace
73+
*.dat
74+
75+
# vi swap
76+
*.swp
77+
78+
# Backup Files
79+
*.bak
80+
*.old
81+
82+
# SVN metadata
83+
.svn/
84+
85+
logs/
86+

LICENSE

Lines changed: 181 additions & 191 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1-
# spring-cloud-code
2-
《重新定义Spring Cloud实战》实体书对应源码
1+
基于Spring Cloud的F版:
2+
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
3+
4+
## 1.工程规范
5+
6+
* xxx-zuul-server Zuul Server
7+
* xxx-eureka-server Eureka Server
8+
* xxx-config-server Spring Cloud Config Server
9+
* xxx-service-provider 服务提供者
10+
* xxx-service-consumer 服务消费者
11+
* xxx-config-client
12+
13+
## 2.包名规范
14+
* 基础包名:cn.springcloud.book
15+
* `<url>http://maven.apache.org</url>`统一修改为`<url>http://springcloud.cn</url>`
16+
17+
## 3.开发者
18+
* 许进(http://xujin.org)
19+
* 钟尊发(http://zhongzunfa.com)
20+
* 叶志远(http://blog.csdn.net/rickiyeat)
21+
* 方志鹏(http://blog.csdn.net/forezp)
22+
* 谭朝红
23+
24+
25+
26+

ch10-1/ch10-1-common/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>cn.springcloud.book</groupId>
7+
<artifactId>ch10-1</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<name>ch10-1-common</name>
12+
<artifactId>ch10-1-common</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.cloud</groupId>
17+
<artifactId>spring-cloud-starter-openfeign</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>com.alibaba</groupId>
21+
<artifactId>fastjson</artifactId>
22+
<version>1.2.4</version>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-maven-plugin</artifactId>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
35+
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cn.springcloud.book.common.config;
2+
3+
import feign.Feign;
4+
5+
import javax.annotation.PostConstruct;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
9+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.web.client.RestTemplate;
13+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
14+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
15+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
16+
import org.springframework.web.servlet.mvc.Controller;
17+
18+
import com.netflix.hystrix.strategy.HystrixPlugins;
19+
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
20+
21+
import cn.springcloud.book.common.context.SpringCloudHystrixConcurrencyStrategy;
22+
import cn.springcloud.book.common.intercepter.FeignUserContextInterceptor;
23+
import cn.springcloud.book.common.intercepter.RestTemplateUserContextInterceptor;
24+
import cn.springcloud.book.common.intercepter.UserContextInterceptor;
25+
26+
@Configuration
27+
@EnableWebMvc
28+
public class CommonConfiguration extends WebMvcConfigurerAdapter{
29+
/**
30+
* 请求拦截器
31+
*/
32+
@Override
33+
public void addInterceptors(InterceptorRegistry registry) {
34+
registry.addInterceptor(new UserContextInterceptor());
35+
}
36+
37+
/**
38+
* 创建Feign请求拦截器,在发送请求前设置认证的用户上下文信息
39+
*/
40+
@Bean
41+
@ConditionalOnClass(Feign.class)
42+
public FeignUserContextInterceptor feignTokenInterceptor() {
43+
return new FeignUserContextInterceptor();
44+
}
45+
46+
/**
47+
* RestTemplate拦截器
48+
* @return
49+
*/
50+
@LoadBalanced
51+
@Bean
52+
public RestTemplate restTemplate() {
53+
RestTemplate restTemplate = new RestTemplate();
54+
restTemplate.getInterceptors().add(new RestTemplateUserContextInterceptor());
55+
return restTemplate;
56+
}
57+
58+
@Bean
59+
public SpringCloudHystrixConcurrencyStrategy springCloudHystrixConcurrencyStrategy() {
60+
return new SpringCloudHystrixConcurrencyStrategy();
61+
}
62+
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.springcloud.book.common.context;
2+
import java.util.concurrent.Callable;
3+
4+
import org.springframework.web.context.request.RequestAttributes;
5+
import org.springframework.web.context.request.RequestContextHolder;
6+
7+
public class HystrixThreadCallable<S> implements Callable<S>{
8+
private final RequestAttributes requestAttributes;
9+
private final Callable<S> delegate;
10+
private String params;
11+
12+
public HystrixThreadCallable(Callable<S> callable, RequestAttributes requestAttributes,String params) {
13+
this.delegate = callable;
14+
this.requestAttributes = requestAttributes;
15+
this.params = params;
16+
}
17+
18+
@Override
19+
public S call() throws Exception {
20+
try {
21+
RequestContextHolder.setRequestAttributes(requestAttributes);
22+
HystrixThreadLocal.threadLocal.set(params);
23+
return delegate.call();
24+
} finally {
25+
RequestContextHolder.resetRequestAttributes();
26+
HystrixThreadLocal.threadLocal.remove();
27+
}
28+
}
29+
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cn.springcloud.book.common.context;
2+
3+
public class HystrixThreadLocal {
4+
public static ThreadLocal<String> threadLocal = new ThreadLocal<>();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cn.springcloud.book.common.context;
2+
3+
import java.util.concurrent.BlockingQueue;
4+
import java.util.concurrent.Callable;
5+
import java.util.concurrent.ThreadPoolExecutor;
6+
import java.util.concurrent.TimeUnit;
7+
8+
import org.springframework.web.context.request.RequestContextHolder;
9+
10+
import com.netflix.hystrix.HystrixThreadPoolKey;
11+
import com.netflix.hystrix.HystrixThreadPoolProperties;
12+
import com.netflix.hystrix.strategy.HystrixPlugins;
13+
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
14+
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariable;
15+
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle;
16+
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
17+
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
18+
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
19+
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
20+
import com.netflix.hystrix.strategy.properties.HystrixProperty;
21+
22+
public class SpringCloudHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
23+
private HystrixConcurrencyStrategy delegateHystrixConcurrencyStrategy;
24+
25+
@Override
26+
public <T> Callable<T> wrapCallable(Callable<T> callable) {
27+
return new HystrixThreadCallable<>(callable, RequestContextHolder.getRequestAttributes(),HystrixThreadLocal.threadLocal.get());
28+
}
29+
30+
public SpringCloudHystrixConcurrencyStrategy() {
31+
init();
32+
}
33+
34+
private void init() {
35+
try {
36+
this.delegateHystrixConcurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
37+
if (this.delegateHystrixConcurrencyStrategy instanceof SpringCloudHystrixConcurrencyStrategy) {
38+
return;
39+
}
40+
41+
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();
42+
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
43+
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
44+
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();
45+
46+
HystrixPlugins.reset();
47+
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
48+
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
49+
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
50+
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
51+
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
52+
}
53+
catch (Exception e) {
54+
throw e;
55+
}
56+
}
57+
58+
59+
@Override
60+
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
61+
HystrixProperty<Integer> corePoolSize,
62+
HystrixProperty<Integer> maximumPoolSize,
63+
HystrixProperty<Integer> keepAliveTime, TimeUnit unit,
64+
BlockingQueue<Runnable> workQueue) {
65+
return this.delegateHystrixConcurrencyStrategy.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize,
66+
keepAliveTime, unit, workQueue);
67+
}
68+
69+
@Override
70+
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
71+
HystrixThreadPoolProperties threadPoolProperties) {
72+
return this.delegateHystrixConcurrencyStrategy.getThreadPool(threadPoolKey, threadPoolProperties);
73+
}
74+
75+
@Override
76+
public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
77+
return this.delegateHystrixConcurrencyStrategy.getBlockingQueue(maxQueueSize);
78+
}
79+
80+
@Override
81+
public <T> HystrixRequestVariable<T> getRequestVariable(
82+
HystrixRequestVariableLifecycle<T> rv) {
83+
return this.delegateHystrixConcurrencyStrategy.getRequestVariable(rv);
84+
}
85+
86+
}

0 commit comments

Comments
 (0)