Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.
Open
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
22 changes: 19 additions & 3 deletions samples/springboot-samples/slimming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ biz1 包含两个打包插件,一个常规 springboot 插件, 构建普通 spr
<skipArkExecutable>true</skipArkExecutable>
<outputDirectory>./target</outputDirectory>
<bizName>biz1</bizName>
<!-- packExcludesConfig 模块瘦身配置,文件名自定义,和配置对应即可-->
<!-- 配置文件位置:biz1/conf/ark/rules.txt-->
<packExcludesConfig>rules.txt</packExcludesConfig>
<webContextPath>biz1</webContextPath>
<declaredMode>true</declaredMode>
<!-- 打包、安装和发布 ark biz-->
Expand All @@ -106,6 +103,25 @@ biz1 包含两个打包插件,一个常规 springboot 插件, 构建普通 spr
</build>
```

瘦身配置在 biz1 的 `biz1/src/main/resources/application.properties` 文件中,
```
# 按 jar 名称( artifactId:version )排除,多个 jar 按照逗号分隔,可以使用换行符号
excludes=tracer-core:3.0.10,tracer-core:3.0.11

# 按 artifactId 排除,支持通配符,多个 artifactId 按照逗号分隔,可以使用换行符号
excludeArtifactIds=commons-collections,commons-httpclient,commons-io,commons-lang

# 按 groupId 排除,支持通配符,多个 group 按照逗号分隔,可以使用换行符号
excludeGroupIds=aopalliance*,asm*,cglib*,com.alibaba.common.lang*,com.alibaba.common.resourcebundle*,com.alibaba.tbase*,com.alipay*,com.antcloud.antvip*,com.caucho.hessian*,com.ctc.wstx*,\
com.fasterxml*,com.google.code*,com.google.common*,com.google.gson*,com.google.guava*,com.google.http-client*,com.google.inject*,com.google.protobuf*,com.ibatis*,com.iwallet.biz*,com.lmax*,\
com.taobao.config*,com.taobao.hsf*,com.taobao.notify*,com.taobao.remoting*,com.taobao.tair*,groovy*,io.fury*,io.grpc*,io.mosn.layotto*,io.netty*,io.openmessaging*,io.prometheus*,javax*,javax.el*,\
javax.script*,javax.servlet*,javax.validation*,loccs-bcprov*,log4j*,mysql*,net.sf.acegisecurity*,net.sf.cglib*,netty*,ognl*,org.aopalliance*,org.apache*,org.aspectj*,org.codehaus*,org.codehaus.groovy*,\
org.codehaus.xfire*,org.dom4j*,org.hibernate.validator*,org.junit*,org.mvel2*,org.mybatis*,org.mybatis.spring*,org.mybatis.spring.boot.autoconfigure*,org.projectlombok*,org.quartz*,org.reflections*,\
org.slf4j*,org.springframework*,org.yaml*,xerces*,xml-apis*,xpp3*,jakarta*,ch.qos.logback*,org.latencyutils*,org.hdrhistogram*,io.micrometer*,ch.qos.logback*,com.squareup.okhttp3*,com.squareup.okhttp*,\
net.sf.ehcache*,redis.clients*
```


## 实验任务
### 执行 mvn clean package -DskipTests
可在各 biz1 bundle 的 target 目录里查看到打包生成的 ark-biz jar 包 和 普通 springboot 包, 明显经过模块瘦身的 ark-biz jar 包大小更小
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ logging.level.com.alipay.sofa=DEBUG
logging.level.root=INFO
logging.level.com.alipay.sofa.arklet=INFO
logging.config=classpath:log4j2-spring.xml
excludes=tracer-core:3.0.10,tracer-core:3.0.11
excludeArtifactIds=commons-collections,commons-httpclient,commons-io,commons-lang
excludeGroupIds=aopalliance*,asm*,cglib*,com.alibaba.common.lang*,com.alibaba.common.resourcebundle*,com.alibaba.tbase*,com.alipay*,com.antcloud.antvip*,com.caucho.hessian*,com.ctc.wstx*,\
com.fasterxml*,com.google.code*,com.google.common*,com.google.gson*,com.google.guava*,com.google.http-client*,com.google.inject*,com.google.protobuf*,com.ibatis*,com.iwallet.biz*,com.lmax*,\
com.taobao.config*,com.taobao.hsf*,com.taobao.notify*,com.taobao.remoting*,com.taobao.tair*,groovy*,io.fury*,io.grpc*,io.mosn.layotto*,io.netty*,io.openmessaging*,io.prometheus*,javax*,javax.el*,\
javax.script*,javax.servlet*,javax.validation*,loccs-bcprov*,log4j*,mysql*,net.sf.acegisecurity*,net.sf.cglib*,netty*,ognl*,org.aopalliance*,org.apache*,org.aspectj*,org.codehaus*,org.codehaus.groovy*,\
org.codehaus.xfire*,org.dom4j*,org.hibernate.validator*,org.junit*,org.mvel2*,org.mybatis*,org.mybatis.spring*,org.mybatis.spring.boot.autoconfigure*,org.projectlombok*,org.quartz*,org.reflections*,\
org.slf4j*,org.springframework*,org.yaml*,xerces*,xml-apis*,xpp3*,jakarta*,ch.qos.logback*,org.latencyutils*,org.hdrhistogram*,io.micrometer*,ch.qos.logback*,com.squareup.okhttp3*,com.squareup.okhttp*,\
net.sf.ehcache*,redis.clients*
52 changes: 52 additions & 0 deletions samples/springboot3-samples/config/apollo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 实验内容


1. 不同模块使用不同的 app id, 独立配置
2. 不同模块使用相同 app id,独立配置默认就已经支持

## 实验任务
### 不同模块使用不同的 app id, 独立配置
#### base 代码改造
1. application.properties 里增加如下配置,用来解决 jmx 注册 bean 冲突问题
```properties
spring.jmx.default-domain=${spring.application.name}
```
2. 配置 apollo 服务端地址
```java
// 默认 apollo 使用 Eureka 获取服务地址,由于本地 docker 采用 bridge 网络模式,通过 Eureka 获取到的是虚拟子网服务地址
// 在本地无法直接调用,所以这里直接通过自定义配置 `apollo.configService` 指定为 localhost

System.setProperty("apollo.configService", "http://localhost:8080");
System.setProperty("apollo.config-service", "http://localhost:8080");
System.setProperty("env", "DEV");
```
3. pom 里引入 apollo 依赖

4. 基座和模块代码里都添加 apollo 多应用治理类 `com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer`
引入覆盖 apollo 原有逻辑的治理类,与原生的类实现的区别在于如下一行
![diff.png](imgs/diff.png)

5. 基座和模块里都添加 apollo 配置文件 `/META-INF/app.properties`
由于注释了 initializeSystemProperty 方法,导致无法通过 application.properties 对 apollo 进行配置, 初始化 `app.id`。所以模块里需要使用 `/META-INF/app.properties` 进行配置。
![init.png](imgs/init.png)

#### 实验步骤
1. cd 进入 config 目录,执行如下命令启动 apollo 服务端
```shell
docker-compose up
```
2. 登录 apollo 管理后台 `localhost:8080`,创建 app id 为 `biz1` 的项目和 key=data.name 的配置,创建 app id 为 `biz2` 的项目和 key=data.name 的配置,具体查看 https://www.apolloconfig.com/#/zh/deployment/quick-start

![apps.png](imgs/apps.png)

![app-data-name.png](imgs/app-data-name.png)

3. 执行 `mvn clean package -DskipTests`,然后启动基座
4. 进入 apollo 目录,执行 `arkctl deploy biz1/target/biz1-apollo-0.0.1-SNAPSHOT-ark-biz.jar`, 安装 biz1 模块
5. 进入 apollo 目录,执行 `arkctl deploy biz2/target/biz2-apollo-0.0.1-SNAPSHOT-ark-biz.jar`, 安装 biz2 模块
6. 执行 `curl http://localhost:8081/biz1/getValue` 获取到 biz1 的配置值,修改 biz1 的 data.name,再次执行 `curl http://localhost:8081/biz1/getValue` 能获取到新的 biz1 的配置值
7. 执行 `curl http://localhost:8081/biz1/getValue` 获取到 biz2 的配置值,修改 biz2 的 data.name,再次执行 `curl http://localhost:8081/biz2/getValue` 能获取到新的 biz2 的配置值,也不会影响 biz1 或基座的配置值

### 不同模块使用相同 app id,独立配置默认就已经支持
模块统一使用[自动排包能力](https://sofaserverless.gitee.io/docs/tutorials/module-development/module-slimming/#%E4%B8%80%E9%94%AE%E8%87%AA%E5%8A%A8%E7%98%A6%E8%BA%AB),在 rules.txt 文件里确保有这个配置 `excludeGroupIds=com.ctrip.framework.apollo*`,将 apollo client 委托给基座加载即可达到效果。
注意 application.properties 里增加 `spring.jmx.default-domain=${spring.application.name}`
89 changes: 89 additions & 0 deletions samples/springboot3-samples/config/apollo/base/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alipay.sofa.config</groupId>
<artifactId>apollo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<artifactId>base-apollo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- 务必将次依赖放在构建 pom 的第一个依赖引入, 并且设置 type= pom,
原理请参考这里 https://sofaserverless.gitee.io/docs/contribution-guidelines/runtime/multi-app-padater/ -->
<!-- must add this dependency as the first one in the pom for building jar, and set type=pom,
check the reason from here https://sofaserverless.gitee.io/docs/contribution-guidelines/runtime/multi-app-padater/ -->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-base-starter</artifactId>
<version>${sofa.serverless.runtime.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>web-ark-plugin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>2.2.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>6.0.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- <configuration>-->
<!-- <image>-->
<!-- <builder>paketobuildpacks/builder-jammy-base:latest</builder>-->
<!-- </image>-->
<!-- </configuration>-->
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alipay.sofa.config.apollo;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@EnableConfigurationProperties
@EnableApolloConfig
@SpringBootApplication
public class ApolloApplication {

public static void main(String[] args) {
// 默认 apollo 使用 Eureka 获取服务地址,由于本地 docker 采用 bridge 网络模式,通过 Eureka 获取到的是虚拟子网服务地址
// 在本地无法直接调用,所以这里直接通过自定义配置 `apollo.configService` 指定为 localhost

System.setProperty("apollo.configService", "http://localhost:8080");
System.setProperty("apollo.config-service", "http://localhost:8080");
System.setProperty("env", "DEV");

SpringApplication.run(ApolloApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alipay.sofa.config.apollo.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@ConfigurationProperties(prefix = "data")
@Service
@Data
@RefreshScope
@Slf4j
public class DataConfig implements InitializingBean {

private String name;

@Override
public void afterPropertiesSet() {
log.info("DataConfig: {}", this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alipay.sofa.config.apollo.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component;

/**
* @Author: ZYL
* @Date: 2021/8/17 13:48
* @Description: Apollo 配置热更新
*/
@Slf4j
@Component
public class PropertiesRefresher {

@Autowired
private RefreshScope refreshScope;

@ApolloConfigChangeListener
public void changeListener(ConfigChangeEvent changeEvent) {
changeEvent.changedKeys().stream().forEach(changeKey -> {
ConfigChange configChange = changeEvent.getChange(changeKey);
log.info("Apollo base config change, propertyName:[{}], oldValue:{}, newValue:{}", configChange.getPropertyName(), configChange.getOldValue(), configChange.getNewValue());
});
refreshScope.refreshAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alipay.sofa.config.apollo.rest;

import com.alipay.sofa.config.apollo.config.DataConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SimpleController {

@Autowired
private DataConfig dataConfig;

@GetMapping("/getValue")
public String getValue() {
return dataConfig.getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
app.id=SampleApp
apollo.bootstrap.enabled=true
apollo.bootstrap.eagerLoad.enabled=true
apollo.bootstrap.namespaces=application
apollo.autoUpdateInjectedSpringProperties=true

# ?? ???? docker ?? bridge ?????? Eureka ?????? apollo ?????
# ????????????????????????? meta ????????????????
# ?????? System.setProperty("apollo.configService", "http://localhost:8080")????????????
apollo.meta=http://localhost:8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spring.application.name=base-apollo
server.port=8082

app.id=SampleApp
apollo.bootstrap.enabled=true
apollo.bootstrap.eagerLoad.enabled=true
apollo.bootstrap.namespaces=application
apollo.autoUpdateInjectedSpringProperties=true



data.name=timeout
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.alipay.sofa.config.apollo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ApolloApplicationTests {

@Test
void contextLoads() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ excludeArtifactIds=commons-lang
excludeGroupIds=aopalliance*
excludeGroupIds=asm*
excludeGroupIds=cglib*
excludeGroupIds=org.springframework*
excludeGroupIds=com.alibaba.common.lang*
excludeGroupIds=com.alibaba.common.resourcebundle*
excludeGroupIds=com.alibaba.tbase*
Expand All @@ -13,10 +14,11 @@ excludeGroupIds=com.antcloud.antvip*
excludeGroupIds=com.caucho.hessian*
excludeGroupIds=com.ctc.wstx*
excludeGroupIds=com.fasterxml*
excludeGroupIds=com.google.code*
excludeGroupIds=com.google.common*
excludeGroupIds=com.google.gson*
excludeGroupIds=com.google.code*
excludeGroupIds=com.google.guava*
excludeGroupIds=com.google.j2objc*
excludeGroupIds=com.google.http-client*
excludeGroupIds=com.google.inject*
excludeGroupIds=com.google.protobuf*
Expand Down Expand Up @@ -64,18 +66,26 @@ excludeGroupIds=org.projectlombok*
excludeGroupIds=org.quartz*
excludeGroupIds=org.reflections*
excludeGroupIds=org.slf4j*
excludeGroupIds=org.springframework*
excludeGroupIds=org.yaml*
excludeGroupIds=xerces*
excludeGroupIds=xml-apis*
excludeGroupIds=xpp3*
excludeGroupIds=jakarta*
excludeGroupIds=ch.qos.logback*
excludeGroupIds=org.latencyutils*
excludeGroupIds=org.hdrhistogram*
excludeGroupIds=io.micrometer*
excludeGroupIds=io.micrometer*
excludeGroupIds=ch.qos.logback*
excludeGroupIds=com.squareup.okhttp3*
excludeGroupIds=com.squareup.okhttp*
excludeGroupIds=net.sf.ehcache*
excludeGroupIds=redis.clients*
excludeGroupIds=net.bytebuddy*
excludeGroupIds=com.taobao.text*
excludeGroupIds=net.java.dev.jna*
excludeGroupIds=com.google.errorprone*
excludeGroupIds=com.github.oshi*
excludeGroupIds=org.checkerframework*


excludeArtifactIds=fastjson
Loading