springcloud分布式事务处理是个棘手问题,LCN是一个个解决方案。但有个小问题,如果使用的是springboot 2.0版本的话,就会出问题,因为LCN现在还是基于springboot1.5.4的版本,还好LCN是开源项目,可以获取到源码(https://gitee.com/wangliang1991/tx-lcn),稍作改动就可以升级到springboot2.0上了。具体操作如下(只针对springcloud升级):
一、transaction-springcloud项目改动:
1、pom只需改动两个属性及feign的依赖:
<properties>
<spring-cloud.version>2.0.0.RELEASE</spring-cloud.version>
<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring-cloud.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
2、com.codingapi.tx.springcloud.listener.ServerListener改动:
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
改成:
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
相应代码改动:
public class ServerListener implements ApplicationListener<ServletWebServerInitializedEvent> {
@Override
public void onApplicationEvent(ServletWebServerInitializedEvent event) {
logger.info("onApplicationEvent -> onApplicationEvent. "+event.getWebServer());
int serverPort = event.getWebServer().getPort();
二、tx-manager改动:
1、pom改动:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2、com.codingapi.tm.ServletInitializer改动:
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
3、com.codingapi.tm.listener.ApplicationStartListener改动:
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
改成:
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
相应代码改动:
public class ApplicationStartListener implements ApplicationListener<ServletWebServerInitializedEvent> {
@Override
public void onApplicationEvent(ServletWebServerInitializedEvent event) {
int serverPort = event.getWebServer().getPort();
三、tx-client改动:
只需改一下pom:
<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
三、如何应用:
1、先将tx-client、transaction-springcloud、tx-plugins-db三个项目打包。
2、启动tx-manager,要应用LCN的springcloud项目增加依赖,示例如下(我改了一下整个项目的version为4.1.1-hk):
<!-- lcn 分布式事务管理 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>com.github.1991wangliang</groupId>
<artifactId>lorne_core</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-client</artifactId>
<version>4.1.1-hk</version>
<scope>system</scope>
<systemPath>
${project.basedir}/lib/tx-client-4.1.1-hk.jar
</systemPath>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-springcloud</artifactId>
<version>4.1.1-hk</version>
<scope>system</scope>
<systemPath>
${project.basedir}/lib/transaction-springcloud-4.1.1-hk.jar
</systemPath>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId>
<version>4.1.1-hk</version>
<scope>system</scope>
<systemPath>
${project.basedir}/lib/tx-plugins-db-4.1.1-hk.jar
</systemPath>
</dependency>