Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xlorne committed Oct 30, 2017
1 parent dc8f0fa commit c909892
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 64 deletions.
5 changes: 0 additions & 5 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 13 additions & 32 deletions README.md
Expand Up @@ -46,7 +46,7 @@ compensate.db.dbType = mysql
```

4. 添加事务拦截器
```java
```
@Aspect
Expand Down Expand Up @@ -80,28 +80,8 @@ LCN是不控制事务。切面仅用于识别LCN分布式事务的作用。

若使用的是`@FeignClient`的方式,则需要添加`configuration`配置。

方案一:

```java
@FeignClient(value = "demo2",configuration = TransactionRestTemplateConfiguration.class)
public interface Demo2Client {


@RequestMapping("/demo/list")
List<Test> list();


@RequestMapping("/demo/save")
int save();
}

```

直接使用lcn提供的`TransactionRestTemplateConfiguration`类。

方案二:

```java
@FeignClient(value = "demo3",configuration = MyConfiguration.class)
public interface Demo3Client {
Expand All @@ -116,14 +96,13 @@ public interface Demo3Client {
```

```java
```
@Configuration
public class MyConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder().requestInterceptor(new TransactionRestTemplateInterceptor());
public RequestInterceptor requestInterceptor(){
return new TransactionRestTemplateInterceptor();
}
}
Expand All @@ -137,7 +116,7 @@ public class MyConfiguration {

在builder时添加拦截器

```java
```
@Autowired
private RestTemplateBuilder builder;
Expand All @@ -156,11 +135,11 @@ public class MyConfiguration {

若采用的是传统的Http请求那么需要手动在发起请求的header下添加tx-group参数如下:

```java
```
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
String groupId = txTransactionLocal==null?null:txTransactionLocal.getGroupId();
request.addHeader("tx-group",groupId);
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
String groupId = txTransactionLocal==null?null:txTransactionLocal.getGroupId();
request.addHeader("tx-group",groupId);
```

Expand Down Expand Up @@ -192,7 +171,7 @@ ribbon.MaxAutoRetriesNextServer=0
7. 配置LCN代理和补偿连接池


```java
```
@Bean
public DataSource dataSource() {
Expand Down Expand Up @@ -254,7 +233,7 @@ ribbon.MaxAutoRetriesNextServer=0

8. 创建数据库,项目都是依赖相同的数据库,创建一次其他的demo下将不再需要重复创建。mysql数据库,库名称test

```sql
```
USE test;
Expand All @@ -279,6 +258,8 @@ CREATE TABLE `t_test` (
事务的补偿机制是基于java反射的方式重新执行一次需要补偿的业务。因此执行的时候需要获取到业务的service对象,LCN是基于spring的ApplicationContent的getBean方法获取bean的对象的。因此不允许出现重名对象。


2. 目前在Feign下开启断路器以后LCN控制失效,该问题正在处理中。

## 测试说明


Expand Down
@@ -1,7 +1,7 @@
package com.example.demo.client;

import com.example.demo.config.MyConfiguration;
import com.example.demo.entity.Test;
import com.lorne.tx.springcloud.feign.TransactionRestTemplateConfiguration;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

Expand All @@ -10,7 +10,7 @@
/**
* Created by lorne on 2017/6/27.
*/
@FeignClient(value = "demo2",configuration = TransactionRestTemplateConfiguration.class)
@FeignClient(value = "demo2",configuration = MyConfiguration.class)
public interface Demo2Client {


Expand Down
@@ -1,10 +1,9 @@
package com.example.demo.config;

import com.lorne.tx.springcloud.feign.TransactionRestTemplateInterceptor;
import feign.Feign;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
* Created by lorne on 2017/6/28.
Expand All @@ -14,8 +13,7 @@
public class MyConfiguration {

@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder().requestInterceptor(new TransactionRestTemplateInterceptor());
public RequestInterceptor requestInterceptor(){
return new TransactionRestTemplateInterceptor();
}
}
@@ -1,7 +1,7 @@
package com.example.demo.client;

import com.example.demo.config.MyConfiguration;
import com.example.demo.entity.Test;
import com.lorne.tx.springcloud.feign.TransactionRestTemplateConfiguration;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

Expand All @@ -10,7 +10,7 @@
/**
* Created by lorne on 2017/6/27.
*/
@FeignClient(value = "demo4",configuration = TransactionRestTemplateConfiguration.class)
@FeignClient(value = "demo4",configuration = MyConfiguration.class)
public interface Demo4Client {


Expand Down
@@ -1,7 +1,7 @@
package com.example.demo.client;

import com.example.demo.config.MyConfiguration;
import com.example.demo.entity.Test;
import com.lorne.tx.springcloud.feign.TransactionRestTemplateConfiguration;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

Expand All @@ -10,7 +10,7 @@
/**
* Created by lorne on 2017/6/27.
*/
@FeignClient(value = "demo5",configuration = TransactionRestTemplateConfiguration.class)
@FeignClient(value = "demo5",configuration = MyConfiguration.class)
public interface Demo5Client {


Expand Down
@@ -0,0 +1,19 @@
package com.example.demo.config;

import com.lorne.tx.springcloud.feign.TransactionRestTemplateInterceptor;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Created by lorne on 2017/6/28.
*/

@Configuration
public class MyConfiguration {

@Bean
public RequestInterceptor requestInterceptor(){
return new TransactionRestTemplateInterceptor();
}
}
@@ -1,7 +1,7 @@
package com.example.demo.client;

import com.example.demo.config.MyConfiguration;
import com.example.demo.entity.Test;
import com.lorne.tx.springcloud.feign.TransactionRestTemplateConfiguration;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

Expand All @@ -10,7 +10,7 @@
/**
* Created by lorne on 2017/6/27.
*/
@FeignClient(value = "demo2",configuration = TransactionRestTemplateConfiguration.class)
@FeignClient(value = "demo2",configuration = MyConfiguration.class)
public interface Demo2Client {


Expand Down
@@ -1,10 +1,9 @@
package com.example.demo.config;

import com.lorne.tx.springcloud.feign.TransactionRestTemplateInterceptor;
import feign.Feign;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
* Created by lorne on 2017/6/28.
Expand All @@ -14,8 +13,7 @@
public class MyConfiguration {

@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder().requestInterceptor(new TransactionRestTemplateInterceptor());
public RequestInterceptor requestInterceptor(){
return new TransactionRestTemplateInterceptor();
}
}
@@ -1,23 +1,24 @@
package com.example.demo.client;

//import com.example.demo.config.MyConfiguration;
import com.example.demo.entity.Test;
import com.lorne.tx.springcloud.feign.TransactionRestTemplateConfiguration;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
* Created by lorne on 2017/6/27.
*/
@FeignClient(value = "demo2",configuration = TransactionRestTemplateConfiguration.class)
@FeignClient(value = "demo2",fallback = Demo2ClientHystric.class)
public interface Demo2Client {


@RequestMapping("/demo/list")
@RequestMapping(value = "/demo/list",method = RequestMethod.GET)
List<Test> list();


@RequestMapping("/demo/save")
@RequestMapping(value = "/demo/save",method = RequestMethod.GET)
int save();
}
@@ -0,0 +1,26 @@
package com.example.demo.client;

import com.example.demo.entity.Test;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* Created by fangzhipeng on 2017/4/6.
*/
@Component
public class Demo2ClientHystric implements Demo2Client {


@Override
public List<Test> list() {
System.out.println("进入断路器-list。。。");
return null;
}

@Override
public int save() {
System.out.println("进入断路器-save。。。");
return 0;
}
}
@@ -1,7 +1,10 @@
package com.example.demo.config;

import com.lorne.tx.springcloud.feign.TransactionRestTemplateInterceptor;
import feign.Contract;
import feign.Feign;
import feign.RequestInterceptor;
import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
Expand All @@ -13,9 +16,24 @@
@Configuration
public class MyConfiguration {

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

// @Bean
// public Contract feignContract() {
// return new feign.Contract.Default();
// }

// @Bean
// public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
// return new BasicAuthRequestInterceptor("user", "password");
// }

@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder().requestInterceptor(new TransactionRestTemplateInterceptor());
public RequestInterceptor requestInterceptor(){
return new TransactionRestTemplateInterceptor();
}
}
Expand Up @@ -26,6 +26,9 @@ public int getOrder() {

@Around("execution(* com.example.demo.service.impl.*Impl.*(..))")
public Object around(ProceedingJoinPoint point)throws Throwable{
return txManagerInterceptor.around(point);
System.out.println("interceptor-->around-->start");
Object val = txManagerInterceptor.around(point);
System.out.println("interceptor-->around-->end");
return val;
}
}
Expand Up @@ -35,12 +35,14 @@ public List<Test> list() {
@Transactional
public int save() {

System.out.println("service-thread-id:"+Thread.currentThread());

int rs2 = demo2Client.save();


int rs1 = testMapper.save("hello1");

int v = 100/0;
//int v = 100/0;

return rs1+rs2;
}
Expand Down
@@ -1,10 +1,15 @@
#feign.hystrix.enabled=true

spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/test
spring.datasource.username= root
spring.datasource.password=root
spring.datasource.initialize = true
init-db= true

#hystrix.command.default.execution.isolation.strategy=Semaphore


spring.application.name = demo1
server.port = 8081
#${random.int[9000,9999]}
Expand Down

0 comments on commit c909892

Please sign in to comment.