F1V3.0-21 微服務舊版本升級

由於我們微服務的諸多好處,很多基於F12.0的項目必將大量遷移,整改成微服務項目,我們該如何進行升級呢?本文將做一個特殊說明,讓你在遷移項目時少走彎路。

1 新建maven項目

命名爲f1-xxxxx(推薦)

1.1 編寫pom文件

本文提供一個通用模板如下所示:

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>f1-xxxx</artifactId>
    <parent>
        <artifactId>f1-parent</artifactId>
        <groupId>com.joinbright.f1</groupId>
        <version>3.0.0-SNAPSHOT</version>
    </parent>
    <dependencies>
        <!-- 開發時熱加載工具 -->
        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency> -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>com.joinbright.f1</groupId>
            <artifactId>f1-starter</artifactId>
        </dependency>
        <!--類型掛接腳本,訪問model中的服務引入這個-->
        <dependency>
            <groupId>com.joinbright.f1</groupId>
            <artifactId>f1-interface-model</artifactId>
        </dependency>
        <dependency>
                <groupId>com.joinbright.f1</groupId>
                <artifactId>f1-starter-listener</artifactId>
        </dependency>
        <!--接入平臺取得授權引入這個-->
        <dependency>
            <groupId>com.joinbright.f1</groupId>
            <artifactId>f1-starter-auth</artifactId>
        </dependency>
        <!--需要訪問permission中的服務引入這個-->
        <dependency>
            <groupId>com.joinbright.f1</groupId>
            <artifactId>f1-interface-permission</artifactId>
        </dependency>
        <!--緩存引入這個-->
        <dependency>
            <groupId>com.joinbright.f1</groupId>
            <artifactId>f1-starter-cache</artifactId>
        </dependency>
        <!-- <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency> -->
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>f1-xxxx</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

1.2 編寫啓動類

創建一個根包 com.jb.xxxx(推介命名) ,在這個下面新建一個啓動類。本文提供一個通用啓動類:

@SpringBootApplication
@EnableDiscoveryClient 
@EnableOAuth2Sso
@EnableFeignClients("com.jb.*.client")
@ComponentScan(basePackages ={"com.jb.xxx"})
@EntityScan(basePackages = {"com.jb.*.model"})
@EnableCaching
public class XXXXXX{

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    @Bean
    public Logger.Level feignLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
    public static void main(String[] args) {
        SpringApplication.run(XXXXXX.class,args);
    }
}

1.3 建立配置文件

在 src/main/resources 下建立如下文件,這裏配置文件內容不做過多說明,大家可以在平臺提供的微服務中找到這些配置,稍作修改即可。

application.properties,
bootstrap.properties,
logback-spring.xml

同時把resource.xml拷貝到 src/main/resources 下

2 移植文件

對於後臺文件的移植可能很簡單,只要你引入正確的平臺jar包,對於報錯信息,更多的是jar包結構改變引起,我們只需要刪除原來import的引入,eclipse會幫助我們自動檢索,只需要點下鼠標即可成功引入。

2.1 service服務的移植

以前的osgi項目我們使用的是切面的形式在service層切入事物,現在我們使用註解的形式切入事物。

@Transactional(value="transactionManager",propagation=Propagation.REQUIRED)

2.2 control的移植

基本平滑移植。

2.3 Model的移植

對於@JsonProperty(“xxxx”)註解的屬性需要

原來的爲

import org.codehaus.jackson.annotate.JsonProperty

改變成:

import com.fasterxml.jackson.annotation.JsonProperty;

對於以前java文件,我們新版的F13.0對一些基礎類的包結構可能做了更改,現在總結如下:

F12.0 F13.0
org.codehaus.jackson.annotate.JsonProperty com.fasterxml.jackson.annotation.JsonProperty
com.jb.f1.kernel.util.ThreadLocalUtils com.jb.util.ThreadLocalUtils
com.jb.f1.kernel.util.classloader.ClassLoaderUtils com.jb.util.ClassLoaderUtils
com.jb.f1.kernel.util.security.MD5 com.jb.util.MD5

3 腳本的移植

平臺不支持模型工具中直接編寫腳本的方式,所以對於舊平臺中模型工具中寫的腳本需要移植到現在的服務中。

3.1 類型腳本移植

引入依賴 f1-interface-model

模型類型中的腳本改爲如下模式:

第一步:

新建一個類繼承自BaseClsScript,如下所示;

@Service("xxxxxx")
@Transactional(value="transactionManager",propagation=Propagation.REQUIRED)
public class SysConfigScript extends BaseClsScript{
}

第二步:

找到對應的腳本:在模型工具中右鍵打開或者直接在 us_sys.tb_model_clsscript表中把腳本內容拷貝出來,粘貼到新建好的類中

第三步:

掛接腳本,在對應的類型鼠標右鍵選擇掛接腳本,把服務ID,服務名稱填上即可。


這裏寫圖片描述

3.2 調度任務的腳本

引入依賴 f1-interface-quartz。

對於調度任務掛接腳本,只是實現的類不同,其他步驟基本一樣。

第一步:新建一個類實現 IJobScript,如下所示;

@Service("quartScript")
@Transactional(value="transactionManager",propagation=Propagation.REQUIRED)
public class QuartScript implements IJobScript{
    @Override
    public void execute(F1JobExecutionContext arg0) {
        System.out.println("調度任務腳本執行");
    }
}

第二步:

找到對應的腳本:在模型工具中右鍵打開或者直接在 us_sys.tb_model_clsscript表中把腳本內容拷貝出來,粘貼到新建好的類中

第三步:

掛接腳本,在對應的任務鼠標右鍵選擇掛接腳本,把服務ID,服務名稱填上即可。


這裏寫圖片描述

第四步:

對於調度任務我們需要配置白名單

security.ignored=/jobScript/execute.do

3.3 工作流腳本

引入f1-interface-workflow。除了實現的接口不同,其他都和模型,調度任務掛接腳本一樣。

流程環節實現:ProcessNodeUniteListener

流程遷移線實現:TransitionFilter

4 自己依賴jar包的升級

對於一些模塊我們可能有自己引入的第三方jar包,可能和平臺版本不兼容,這裏建議對於以前舊的第三方包替換成支持jdk1.8的包。

5 移植前後項目對比

對於基於osgi的項目移植成微服務後項目結構要簡單的多,沒有那麼多配置文件。

移植前項目結構


這裏寫圖片描述

移植後的項目結構


這裏寫圖片描述

發佈了69 篇原創文章 · 獲贊 25 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章