Skip to content

Commit

Permalink
apollo assembly optimization (#5035)
Browse files Browse the repository at this point in the history
* table prefix

* assembly config v0.1

* assembly config v0.2

* assembly db init

* assembly db init fix

* assembly sql

* WebSecurity

* H2Function

* assembly profile

* assembly session

* ddl-auto=none

* DataSource

* doc

* admin assembly profile

* copy assembly sql on build

* merge sql conflict

* sql comment

* sql converter

* sql converter delta

* sql converter clean

* fix assembly sql

* fix assembly sql temp

* fix assembly sql temp v0.2

* fix assembly sql temp v0.3

* fix assembly sql temp v0.4

* fix assembly sql temp v0.5

* assembly doc

* autoGeneratedDeclaration

* rename sql-converter module

* sql-converter h2

* fix database-discovery

* mv sql

* mv sql v0.2

* mv sql v0.3

* multi datasource

* remove table prefix

* update doc

* fix init

* log apollo datasource initialize

* fix apollo datasource initialize

* mv h2 sql

* fix apollo datasource initialize v0.2

* clean import

* fix sql check

* fix sql check v0.2

* merge sql

* merge sql generated

* add sql test temp

* add sql test temp 2

* add sql test

* sql convert

* fix sql-convert

* CHANGES.md

* fix copyright

* clean logger

* fix order

* fix h2 converter

* fix h2 converter v0.2

* fix order v0.2

---------

Co-authored-by: Jason Song <[email protected]>
  • Loading branch information
vdiskg and nobodyiam authored Feb 3, 2024
1 parent 921af84 commit c90c930
Show file tree
Hide file tree
Showing 107 changed files with 7,902 additions and 442 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Apollo 2.3.0
* [Fix circular references on LdapAutoConfiguration](https://github.com/apolloconfig/apollo/pull/5055)
* [Add comment for clusters and UI display](https://github.com/apolloconfig/apollo/pull/5072)
* [Fix the issue that the length of private namespaces are mis-calculated](https://github.com/apolloconfig/apollo/pull/5078)
* [apollo assembly optimization](https://github.com/apolloconfig/apollo/pull/5035)


------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
Expand All @@ -29,7 +30,9 @@
@EnableAspectJAutoProxy
@Configuration
@PropertySource(value = {"classpath:adminservice.properties"})
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {
UserDetailsServiceAutoConfiguration.class,
})
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {ApolloCommonConfig.class,
ApolloBizConfig.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed 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.ctrip.framework.apollo.adminservice;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Profile("assembly")
@Configuration
public class AdminServiceAssemblyConfiguration {

@Order(101)
@Configuration
static class AdminServiceSecurityConfigurer extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.httpBasic();
}
}
}
28 changes: 28 additions & 0 deletions apollo-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/META-INF/sql/profiles</outputDirectory>
<resources>
<resource>
<directory>${project.parent.basedir}/scripts/sql/profiles</directory>
<includes>
<include>h2-default/apolloconfigdb.sql</include>
<include>h2-default/apolloportaldb.sql</include>
<include>mysql-database-not-specified/apolloconfigdb.sql</include>
<include>mysql-database-not-specified/apolloportaldb.sql</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class, ApolloAuditAutoConfiguration.class})
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
ApolloAuditAutoConfiguration.class,
})
public class ApolloApplication {

private static final Logger logger = LoggerFactory.getLogger(ApolloApplication.class);
Expand All @@ -41,39 +47,46 @@ public static void main(String[] args) throws Exception {
/**
* Common
*/
MDC.put("starting_context", "[starting:common] ");
logger.info("commonContext starting...");
ConfigurableApplicationContext commonContext =
new SpringApplicationBuilder(ApolloApplication.class).web(WebApplicationType.NONE).run(args);
logger.info(commonContext.getId() + " isActive: " + commonContext.isActive());
logger.info("commonContext [{}] isActive: {}", commonContext.getId(), commonContext.isActive());

/**
* ConfigService
*/
if (commonContext.getEnvironment().containsProperty("configservice")) {
ConfigurableApplicationContext configContext =
new SpringApplicationBuilder(ConfigServiceApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info(configContext.getId() + " isActive: " + configContext.isActive());
}
MDC.put("starting_context", "[starting:config] ");
logger.info("configContext starting...");
ConfigurableApplicationContext configContext =
new SpringApplicationBuilder(ConfigServiceApplication.class).parent(commonContext)
.profiles("assembly")
.sources(RefreshScope.class).run(args);
logger.info("configContext [{}] isActive: {}", configContext.getId(), configContext.isActive());

/**
* AdminService
*/
if (commonContext.getEnvironment().containsProperty("adminservice")) {
ConfigurableApplicationContext adminContext =
new SpringApplicationBuilder(AdminServiceApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info(adminContext.getId() + " isActive: " + adminContext.isActive());
}
MDC.put("starting_context", "[starting:admin] ");
logger.info("adminContext starting...");
ConfigurableApplicationContext adminContext =
new SpringApplicationBuilder(AdminServiceApplication.class).parent(commonContext)
.profiles("assembly")
.sources(RefreshScope.class).run(args);
logger.info("adminContext [{}] isActive: {}", adminContext.getId(), adminContext.isActive());

/**
* Portal
*/
if (commonContext.getEnvironment().containsProperty("portal")) {
ConfigurableApplicationContext portalContext =
new SpringApplicationBuilder(PortalApplication.class).parent(commonContext)
.sources(RefreshScope.class).run(args);
logger.info(portalContext.getId() + " isActive: " + portalContext.isActive());
}
MDC.put("starting_context", "[starting:portal] ");
logger.info("portalContext starting...");
ConfigurableApplicationContext portalContext =
new SpringApplicationBuilder(PortalApplication.class).parent(commonContext)
.profiles("assembly")
.sources(RefreshScope.class).run(args);
logger.info("portalContext [{}] isActive: {}", portalContext.getId(), portalContext.isActive());

MDC.clear();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright 2024 Apollo Authors
#
# Licensed 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.
#
apollo.eureka.server.enabled=false
eureka.client.enabled=false
spring.cloud.discovery.enabled=false

apollo.service.registry.enabled=true
apollo.service.registry.cluster=default
apollo.service.registry.heartbeatIntervalInSecond=10

apollo.service.discovery.enabled=true
# health check by heartbeat, heartbeat time before 61s ago will be seemed as unhealthy
apollo.service.discovery.healthCheckIntervalInSecond = 61
47 changes: 47 additions & 0 deletions apollo-assembly/src/main/resources/application-github.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# Copyright 2024 Apollo Authors
#
# Licensed 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.
#
# Config DataSource
spring.config-datasource.url=jdbc:h2:mem:~/apollo-config-db;mode=mysql;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;BUILTIN_ALIAS_OVERRIDE=TRUE;DATABASE_TO_UPPER=FALSE
#spring.config-datasource.username=
#spring.config-datasource.password=
spring.sql.config-init.schema-locations=@@repository@@/profiles/@@platform@@@@suffix@@/apolloconfigdb.sql
spring.sql.config-init.mode=embedded
# Portal DataSource
spring.portal-datasource.url=jdbc:h2:mem:~/apollo-portal-db;mode=mysql;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;BUILTIN_ALIAS_OVERRIDE=TRUE;DATABASE_TO_UPPER=FALSE
#spring.portal-datasource.username=
#spring.portal-datasource.password=
spring.sql.portal-init.schema-locations=@@repository@@/profiles/@@platform@@@@suffix@@/apolloportaldb.sql
spring.sql.portal-init.mode=embedded

# Resolve Multi DataSource JMX name conflict
spring.jmx.unique-names=true

# H2 datasource
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.metadata_builder_contributor=com.ctrip.framework.apollo.common.jpa.SqlFunctionsMetadataBuilderContributor
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true

# Sql logging
#logging.level.org.hibernate.SQL=DEBUG

# Default env
apollo.portal.envs=local

# Spring session
spring.session.store-type=none
4 changes: 4 additions & 0 deletions apollo-assembly/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
~
-->
<configuration>
<property name="CONSOLE_LOG_PATTERN"
value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %X{starting_context}%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="FILE_LOG_PATTERN"
value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} %X{starting_context}: %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE"
value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}apollo-assembly.log}" />
Expand Down
25 changes: 22 additions & 3 deletions apollo-assembly/src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
spring.datasource.url = jdbc:h2:mem:~/apolloconfigdb;mode=mysql;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
# Config DataSource
spring.config-datasource.url=jdbc:h2:mem:~/apollo-config-db;mode=mysql;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;BUILTIN_ALIAS_OVERRIDE=TRUE;DATABASE_TO_UPPER=FALSE
#spring.config-datasource.username=
#spring.config-datasource.password=
spring.sql.config-init.schema-locations=@@repository@@/profiles/@@platform@@@@suffix@@/apolloconfigdb.sql
spring.sql.config-init.mode=embedded
# Portal DataSource
spring.portal-datasource.url=jdbc:h2:mem:~/apollo-portal-db;mode=mysql;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;BUILTIN_ALIAS_OVERRIDE=TRUE;DATABASE_TO_UPPER=FALSE
#spring.portal-datasource.username=
#spring.portal-datasource.password=
spring.sql.portal-init.schema-locations=@@repository@@/profiles/@@platform@@@@suffix@@/apolloportaldb.sql
spring.sql.portal-init.mode=embedded

# Resolve Multi DataSource JMX name conflict
spring.jmx.unique-names=true

# H2 datasource
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.metadata_builder_contributor=com.ctrip.framework.apollo.common.jpa.SqlFunctionsMetadataBuilderContributor
spring.h2.console.enabled = true
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
apollo.portal.env= local

# Default env
apollo.portal.envs=local
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed 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.ctrip.framework.apollo.biz;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

@Profile("assembly")
@Configuration
public class ApolloBizAssemblyConfiguration {

@Primary
@ConfigurationProperties(prefix = "spring.config-datasource")
@Bean
public static DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public BizDBPropertySource(final ServerConfigRepository serverConfigRepository,

@PostConstruct
public void runSqlScript() throws Exception {
if (env.acceptsProfiles(Profiles.of("h2"))) {
if (env.acceptsProfiles(Profiles.of("h2")) && !env.acceptsProfiles(Profiles.of("assembly"))) {
Resource resource = new ClassPathResource("jpa/configdb.init.h2.sql");
if (resource.exists()) {
DatabasePopulatorUtils.execute(new ResourceDatabasePopulator(resource), dataSource);
Expand Down
Loading

0 comments on commit c90c930

Please sign in to comment.