springcloud LCN 分散式事務升級到 springboot 2.0
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
<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign
<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>