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

springCloud中的RestTemplateConfiguration自定义Feign.Builder拦截与开启hystrix冲突 #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions raincat-core/pom.xml
Original file line number Diff line number Diff line change
@@ -100,6 +100,10 @@
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

</dependencies>
<build>
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
import com.raincat.core.compensation.command.TxCompensationCommand;
import com.raincat.core.concurrent.task.BlockTask;
import com.raincat.core.concurrent.task.BlockTaskHelper;
import com.raincat.core.concurrent.threadlocal.TxTransactionLocal;
import com.raincat.core.concurrent.threadpool.TransactionThreadPool;
import com.raincat.core.service.TxManagerMessageService;
import com.raincat.core.service.TxTransactionHandler;
@@ -40,6 +41,8 @@
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledFuture;
@@ -77,10 +80,13 @@ public Object handler(ProceedingJoinPoint point, TxTransactionInfo info) throws
LogUtil.info(LOGGER, "分布式事务参与方,开始执行,事务组id:{}", info::getTxGroupId);
final String taskKey = IdWorkerUtils.getInstance().createTaskKey();
final BlockTask task = BlockTaskHelper.getInstance().getTask(taskKey);
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

transactionThreadPool
.newFixedThreadPool()
.execute(() -> {
TxTransactionLocal.getInstance().setTxGroupId(info.getTxGroupId());
RequestContextHolder.setRequestAttributes(requestAttributes);
final String waitKey = IdWorkerUtils.getInstance().createTaskKey();
final BlockTask waitTask = BlockTaskHelper.getInstance().getTask(waitKey);
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
Original file line number Diff line number Diff line change
@@ -18,7 +18,11 @@
package com.raincat.springcloud.feign;


import com.raincat.common.constant.CommonConstant;
import com.raincat.core.concurrent.threadlocal.TxTransactionLocal;
import feign.Feign;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@@ -28,11 +32,24 @@
*/
@Configuration
public class RestTemplateConfiguration {

/*
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder().requestInterceptor(new RestTemplateInterceptor());
}*/

@Bean("txRequestInterceptor")
public RequestInterceptor txRequestInterceptor() {

return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
template.header(CommonConstant.TX_TRANSACTION_GROUP, TxTransactionLocal.getInstance().getTxGroupId());

}
};
}


}