未验证 提交 37595767 编写于 作者: L lorne 提交者: GitHub

Merge pull request #169 from yizhishang/springcloud-2.0.4

Springcloud 2.0.4
......@@ -30,11 +30,11 @@
## 目录说明
transaction-dubbo LCN dubbo rpc框架扩展支持
extensions feign扩展配置类
transaction-springcloud LCN springcloud rpc框架扩展支持
springcloud-lcn-demo demo案例
transaction-motan LCN motan rpc框架扩展支持
transaction-springcloud LCN springcloud rpc框架扩展支持
tx-client 是LCN核心tx模块端控制框架
......@@ -48,7 +48,6 @@ tx-plugins-db 是LCN 对关系型数据库的插件支持
分布式事务发起方:
```
@Override
@TxTransaction(isStart=true)
@Transactional
......@@ -61,13 +60,10 @@ tx-plugins-db 是LCN 对关系型数据库的插件支持
int v = 100/0;
return true;
}
```
分布式事务被调用方(test2Service的业务实现类)
```
@Override
@Transactional
@TxTransaction
......@@ -76,7 +72,6 @@ tx-plugins-db 是LCN 对关系型数据库的插件支持
testDao.save();
return true;
}
```
如上代码执行完成以后两个模块都将回滚事务。
......@@ -119,27 +114,12 @@ tx-plugins-db 是LCN 对关系型数据库的插件支持
<version>${lcn.last.version}</version>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId>
<version>${lcn.last.version}</version>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-dubbo</artifactId>
<version>${lcn.last.version}</version>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-motan</artifactId>
<version>${lcn.last.version}</version>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-springcloud</artifactId>
......@@ -148,19 +128,6 @@ tx-plugins-db 是LCN 对关系型数据库的插件支持
```
依赖gradle等形式,见中心库
[http://mvnrepository.com/search?q=codingapi](http://mvnrepository.com/search?q=codingapi)
## demo演示教程
每个demo下有区分为 jdbc/hibernate/mybatis不同框架的版本demo
[springcloud版本](https://github.com/codingapi/springcloud-lcn-demo)
[dubbo版本](https://github.com/codingapi/dubbo-lcn-demo)
[motan版本](https://gitee.com/zfvipCase/motan-lcn-demo)
技术交流群:554855843
[springcloud版本](https://github.com/yizhishang/tx-lcn/tree/springcloud-2.0.4/springcloud-lcn-demo)
......@@ -5,7 +5,7 @@
<groupId>com.codingapi</groupId>
<artifactId>tx-lcn</artifactId>
<version>4.1.0</version>
<version>4.2.0</version>
<packaging>pom</packaging>
<name>tx-lcn</name>
......@@ -16,64 +16,34 @@
<module>tx-client</module>
<module>tx-manager</module>
<module>transaction-dubbo</module>
<module>transaction-springcloud</module>
<module>transaction-motan</module>
<module>tx-plugins-db</module>
<module>springcloud-lcn-demo</module>
</modules>
<properties>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compile.source>1.7</maven.compile.source>
<maven.compile.target>1.7</maven.compile.target>
<java.version>1.7</java.version>
<maven.compile.source>1.8</maven.compile.source>
<maven.compile.target>1.8</maven.compile.target>
<java.version>1.8</java.version>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<lcn.last.version>4.1.0</lcn.last.version>
</properties>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<name>lorne</name>
<email>1991wangliang@gmail.com</email>
<roles>
<role>developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
<scm>
<connection>scm:git:https://github.com/codingapi/tx-lcn.git</connection>
<developerConnection>scm:git:https://github.com/codingapi/tx-lcn.git</developerConnection>
<url>https://github.com/codingapi/tx-lcn</url>
<tag>v${project.version}</tag>
</scm>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<name>Maven Central Staging Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<org.aspectj.version>1.8.4</org.aspectj.version>
<guava.version>19.0</guava.version>
<hessian.version>4.0.38</hessian.version>
<protostuff.version>1.1.3</protostuff.version>
<kryo.version>4.0.0</kryo.version>
<org.slf4j-version>1.7.7</org.slf4j-version>
<lcn.last.version>4.2.0</lcn.last.version>
<spring-cloud.version>2.0.4.RELEASE</spring-cloud.version>
<org.springframework-version>5.0.8.RELEASE</org.springframework-version>
<spring-cloud-starter-openfeign.version>2.0.0.RELEASE</spring-cloud-starter-openfeign.version>
</properties>
<build>
<plugins>
......@@ -101,7 +71,6 @@
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
......@@ -113,37 +82,6 @@
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!--手动打包时,将下面的plugin注释掉-->
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-gpg-plugin</artifactId>-->
<!--<version>1.6</version>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>sign-artifacts</id>-->
<!--<phase>verify</phase>-->
<!--<goals>-->
<!--<goal>sign</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
</plugins>
</build>
......
>1.maven引入
````xml
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-springcloud</artifactId>
<version>${lcn.last.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId>
<version>${lcn.last.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
````
>2.application.properties文件配置eureka地址
```properties
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
```
>3.发起方和参与方的业务方法添加@Transactional注解和@TxTransaction注解
>4.发起方中的涉及分布式事务的方法要添加@TxTransaction(isStart = true)注解
>5.tx-manager启动前要配置redis
>6.springboot启动类添加注解配置@EnableFeignClients(basePackages = {"com.codingapi.tx"})
注意事项:避免事务发起方和调用方操作一张表的数据,会导致锁表
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codingapi</groupId>
<artifactId>springcloud-lcn-demo</artifactId>
<version>4.2.0</version>
<packaging>pom</packaging>
<name>springcloud-lcn-demo</name>
<modules>
<module>springcloud-mybatis-demo1</module>
<module>springcloud-mybatis-demo2</module>
<module>springcloud-mybatis-demo3</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compile.source>1.8</maven.compile.source>
<maven.compile.target>1.8</maven.compile.target>
<java.version>1.8</java.version>
<lcn.last.version>4.2.0</lcn.last.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-springcloud</artifactId>
<version>${lcn.last.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId>
<version>${lcn.last.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compile.source}</source>
<target>${maven.compile.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springcloud-mybatis-demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-mybatis-demo1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.codingapi</groupId>
<artifactId>springcloud-lcn-demo</artifactId>
<version>4.2.0</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.19</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
</dependencies>
</project>
package com.example.demo;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import javax.sql.DataSource;
@Configuration
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.codingapi.tx", "com.example.demo"})
public class MybatisDemo1Application {
public static void main(String[] args) {
SpringApplication.run(MybatisDemo1Application.class, args);
}
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
dataSource.setInitialSize(10);
dataSource.setMaxActive(50);
dataSource.setMinIdle(0);
dataSource.setMaxWait(60000);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(false);
dataSource.setTestWhileIdle(true);
dataSource.setPoolPreparedStatements(false);
return dataSource;
}
}
package com.example.demo.client;
import com.example.demo.entity.Test;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* Created by lorne on 2017/6/27.
*/
@FeignClient(value = "demo2", fallback = Demo2ClientHystric.class)
public interface Demo2Client {
@RequestMapping(value = "/demo/list", method = RequestMethod.GET)
List<Test> list();
@RequestMapping(value = "/demo/save", method = RequestMethod.GET)
int save(@RequestParam(value = "id") String id, @RequestParam(value = "name") String name);
}
package com.example.demo.client;
import com.example.demo.entity.Test;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class Demo2ClientHystric implements Demo2Client {
@Override
public List<Test> list() {
System.out.println("进入断路器-list。。。");
throw new RuntimeException("list 保存失败.");
}
@Override
public int save(String id, String name) {
System.out.println("进入断路器-save。。。");
throw new RuntimeException("save 保存失败.");
}
}
package com.example.demo.client;
import com.example.demo.entity.Test;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* Created by lorne on 2017/6/27.
*/
@FeignClient(value = "demo3", fallback = Demo3ClientHystric.class)
public interface Demo3Client {
@RequestMapping(value = "/demo/save", method = RequestMethod.GET)
int save();
}