Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions springboot-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<koupleless.runtime.version>1.3.0</koupleless.runtime.version>
<sofa.ark.version>2.2.13</sofa.ark.version>
<koupleless.runtime.version>1.3.1-SNAPSHOT</koupleless.runtime.version>
<sofa.ark.version>2.2.14-SNAPSHOT</sofa.ark.version>
<disruptor.version>3.4.2</disruptor.version>
<os.plugin.version>1.7.1</os.plugin.version>
<protobuf.plugin.version>0.6.1</protobuf.plugin.version>
Expand Down
28 changes: 21 additions & 7 deletions springboot-samples/service/README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ base 为普通 springboot 改造成的基座,改造内容为在 pom 里增加
```

### biz
biz 包含两个模块,分别为 biz1 和 biz2, 都是普通 springboot,修改打包插件方式为 sofaArk biz 模块打包方式,打包为 ark biz jar 包,打包插件配置如下:
biz 包含三个模块,分别为 biz1, biz2, biz3。其中,biz1 和 biz2 都是普通 springboot,biz3 是纯代码片段。修改打包插件方式为 sofaArk biz 模块打包方式,打包为 ark biz jar 包,打包插件配置如下:
```xml
<dependency>
<groupId>com.alipay.sofa.koupleless</groupId>
Expand Down Expand Up @@ -74,7 +74,7 @@ biz 包含两个模块,分别为 biz1 和 biz2, 都是普通 springboot,修
</configuration>
</plugin>
```
注意这里将不同 biz 的web context path 修改成不同的值,以此才能成功在一个 tomcat host 里安装多个 web 应用。
注意这里将 biz1 和 biz2 的web context path 修改成不同的值,以此才能成功在一个 tomcat host 里安装多个 web 应用。


## 实验步骤
Expand All @@ -83,11 +83,11 @@ biz 包含两个模块,分别为 biz1 和 biz2, 都是普通 springboot,修

可以使用 IDEA run 启动基座应用

### 打包模块应用 biz1、biz2
### 打包模块应用 biz1、biz2、biz3

在samples/springboot-samples/service/sample-service-biz 和 samples/springboot-samples/service/sample-service-biz2 目录下分别执行 mvn clean package -Dmaven.test.skip=true 进行模块打包, 打包完成后可在各 bundle 的 target 目录里查看到打包生成的 ark-biz jar 包
在samples/springboot-samples/service/biz1/biz1-bootstrap, samples/springboot-samples/service/biz2/biz2-bootstrap, samples/springboot-samples/service/biz3/biz3-bootstrap 目录下分别执行 mvn clean package -Dmaven.test.skip=true 进行模块打包, 打包完成后可在各 bundle 的 target 目录里查看到打包生成的 ark-biz jar 包

### 安装模块应用 biz1、biz2
### 安装模块应用 biz1、biz2、biz3

#### 执行 curl 命令安装 biz1

Expand All @@ -98,7 +98,7 @@ curl --location --request POST 'localhost:1238/installBiz' \
"bizName": "biz",
"bizVersion": "0.0.1-SNAPSHOT",
// local path should start with file://, alse support remote url which can be downloaded
"bizUrl": "file:///Users/xxxx/xxxx/Code/koupleless/samples/springboot-samples/service/sample-service-biz/biz-bootstrap/target/biz-bootstrap-0.0.1-SNAPSHOT-ark-biz.jar"
"bizUrl": "file:///Users/xxxx/xxxx/Code/koupleless/samples/springboot-samples/service/biz1/biz1-bootstrap/target/biz1-bootstrap-0.0.1-SNAPSHOT-ark-biz.jar"
}'
```

Expand All @@ -111,10 +111,24 @@ curl --location --request POST 'localhost:1238/installBiz' \
"bizName": "biz2",
"bizVersion": "0.0.1-SNAPSHOT",
// local path should start with file://, alse support remote url which can be downloaded
"bizUrl": "file:///Users/xxxx/xxxx/Code/koupleless/samples/springboot-samples/service/sample-service-biz2/biz2-bootstrap/target/biz2-bootstrap-0.0.1-SNAPSHOT-ark-biz.jar"
"bizUrl": "file:///Users/xxxx/xxxx/Code/koupleless/samples/springboot-samples/service/biz2/biz2-bootstrap/target/biz2-bootstrap-0.0.1-SNAPSHOT-ark-biz.jar"
}'
```

#### 执行 curl 命令安装 biz3

```shell
curl --location --request POST 'localhost:1238/installBiz' \
--header 'Content-Type: application/json' \
--data '{
"bizName": "biz3",
"bizVersion": "0.0.1-SNAPSHOT",
// local path should start with file://, alse support remote url which can be downloaded
"bizUrl": "file:///Users/xxxx/xxxx/Code/koupleless/samples/springboot-samples/service/biz3/biz3-bootstrap/target/biz3-bootstrap-0.0.1-SNAPSHOT-ark-biz.jar"
}'
```


### 发起请求验证

#### 验证基座调用模块
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
public class AppServiceImpl implements AppService {
@Override
public String getAppName() {
return null;
return "baseApp";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Externalize the application name.

Returning a hardcoded string is a valid fix to avoid potential null pointer exceptions. The code changes are approved.

However, hardcoding the application name reduces flexibility and maintainability. Consider externalizing the application name to a configuration file (e.g., application.properties) or an environment variable.

For example, you can define the application name in application.properties:

spring.application.name=baseApp

Then, inject the application name using the @Value annotation:

+import org.springframework.beans.factory.annotation.Value;
 
 @Service
 public class AppServiceImpl implements AppService {
+    @Value("${spring.application.name}")
+    private String appName;
+
     @Override
     public String getAppName() {
-        return "baseApp";
+        return appName;
     }
 }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.base.rest;

import com.alipay.sofa.base.facade.AppService;
import com.alipay.sofa.biz.facade.Param;
import com.alipay.sofa.biz.facade.Provider;
import com.alipay.sofa.biz.facade.Result;
Expand All @@ -42,6 +43,9 @@ public class SampleController {
@AutowiredFromBiz(bizName = "biz1", name = "teacherProvider")
private Provider teacherProvider;

@AutowiredFromBiz(bizName = "biz3", bizVersion = "0.0.1-SNAPSHOT", name = "biz3AppServiceImpl")
private AppService biz3AppServiceImpl;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String hello() {
Result tmp = studentProvider.provide(new Param());
Expand Down Expand Up @@ -79,6 +83,17 @@ public String hello() {
System.out.println(result2.getClass());
}

System.out.println(biz3AppServiceImpl.getAppName());

AppService biz3OtherAppServiceImpl = SpringServiceFinder.getModuleService("biz3", "0.0.1-SNAPSHOT",
"biz3OtherAppServiceImpl", AppService.class);
System.out.println(biz3OtherAppServiceImpl.getAppName());

Map<String, AppService> appServiceMap = SpringServiceFinder.listModuleServices("biz3",
"0.0.1-SNAPSHOT", AppService.class);
for (AppService appService:appServiceMap.values()){
System.out.println(appService.getAppName());
}
return "hello to ark master biz";
}
}
79 changes: 79 additions & 0 deletions springboot-samples/service/biz3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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>
<parent>
<groupId>com.alipay.sofa.service</groupId>
<artifactId>cross-biz-calling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>

<artifactId>biz3</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.alipay.sofa.service</groupId>
<artifactId>base-facade</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.alipay.sofa.koupleless</groupId>
<artifactId>koupleless-app-starter</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<classifier>lib</classifier>
<!-- Ensure other necessary configuration here -->
</configuration>
</execution>
</executions>
</plugin>
<!-- 修改打包插件为 sofa-ark biz 打包插件,打包成 ark biz jar -->
<plugin>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-maven-plugin</artifactId>
<version>${sofa.ark.version}</version>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<skipArkExecutable>true</skipArkExecutable>
<outputDirectory>target</outputDirectory>
<bizName>biz3</bizName>
<!-- 单host下需更换 web context path -->
<declaredMode>true</declaredMode>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.biz3;

import com.alipay.sofa.biz3.service.Biz3AppServiceImpl;
import com.alipay.sofa.biz3.service.Biz3OtherAppServiceImpl;
import com.alipay.sofa.koupleless.common.MainApplication;

/**
* @author lianglipeng.llp@alibaba-inc.com
* @version $Id: Biz3Application.java, v 0.1 2024年09月11日 16:01 立蓬 Exp $
*/
public class Biz3Application {
public static void main(String[] args) {
// 初始化模块的实例容器
MainApplication.init();

// 注册实例到模块容器中
MainApplication.register("biz3AppServiceImpl", new Biz3AppServiceImpl());
MainApplication.register("biz3OtherAppServiceImpl", new Biz3OtherAppServiceImpl());

System.out.println("Biz3 start!");
System.out.println("Biz classLoader: " + Biz3Application.class.getClassLoader());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.biz3.service;

import com.alipay.sofa.base.facade.AppService;
import com.alipay.sofa.koupleless.common.api.SpringServiceFinder;

/**
* @author lianglipeng.llp@alibaba-inc.com
* @version $Id: Biz3AppServiceImpl.java, v 0.1 2024年09月11日 16:06 立蓬 Exp $
*/
public class Biz3AppServiceImpl implements AppService {
// 获取基座的bean
private AppService baseAppService = SpringServiceFinder.getBaseService(AppService.class);

public String getAppName() {
return "biz3AppServiceImpl in the base: " + baseAppService.getAppName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.biz3.service;

import com.alipay.sofa.base.facade.AppService;
import com.alipay.sofa.koupleless.common.api.SpringServiceFinder;

/**
* @author lianglipeng.llp@alibaba-inc.com
* @version $Id: Biz3OtherAppServiceImpl.java, v 0.1 2024年09月11日 16:25 立蓬 Exp $
*/
public class Biz3OtherAppServiceImpl implements AppService {
// 获取基座的bean
private AppService baseAppService = SpringServiceFinder.getBaseService(AppService.class);

@Override
public String getAppName() {
return "biz3OtherAppServiceImpl in the base: " + baseAppService.getAppName();
}
}
1 change: 1 addition & 0 deletions springboot-samples/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<module>biz1</module>
<module>biz2</module>
<module>cross-biz-calling-integration-test</module>
<module>biz3</module>
</modules>
</project>