Skip to content

Commit

Permalink
[SCB-2786]remove EnableServiceComb annotation (#3975)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Oct 17, 2023
1 parent e5bb4f9 commit 793b1c1
Show file tree
Hide file tree
Showing 75 changed files with 199 additions and 469 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* 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 org.apache.servicecomb.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.servicecomb.config.ConfigMapping;
import org.apache.servicecomb.config.YAMLUtil;
import org.apache.servicecomb.config.file.MicroserviceConfigLoader;
import org.apache.servicecomb.foundation.bootstrap.BootStrapService;
import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

/**
* Add microservice.yaml to Environment earlier<br>
* to affect {@link Conditional}<br>
*/
public class ConfigurationSpringBootInitializer implements EnvironmentPostProcessor {
public static final String MICROSERVICE_PROPERTY_SOURCE_NAME = "microservice.yaml";

public static final String MAPPING_PROPERTY_SOURCE_NAME = "mapping.yaml";

private final List<BootStrapService> bootStrapServices = SPIServiceUtils.getSortedService(BootStrapService.class);

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
addMicroserviceDefinitions(environment);
startupBootStrapService(environment);
}

public static void addMicroserviceDefinitions(Environment environment) {
addMicroserviceYAMLToSpring(environment);
addMappingToSpring(environment);
}

private void startupBootStrapService(Environment environment) {
bootStrapServices.forEach(bootStrapService -> bootStrapService.startup(environment));
}

/**
* make springboot have a change to add microservice.yaml source earlier<br>
* to affect {@link Conditional}
* @param environment environment
*/
private static void addMicroserviceYAMLToSpring(Environment environment) {
if (!(environment instanceof ConfigurableEnvironment)) {
return;
}

MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
if (propertySources.contains(MICROSERVICE_PROPERTY_SOURCE_NAME)) {
return;
}

propertySources.addLast(new EnumerablePropertySource<MicroserviceConfigLoader>(MICROSERVICE_PROPERTY_SOURCE_NAME) {
private final Map<String, Object> values = new HashMap<>();

private final String[] propertyNames;

{
MicroserviceConfigLoader loader = new MicroserviceConfigLoader();
loader.loadAndSort();

loader.getConfigModels()
.forEach(configModel -> values.putAll(YAMLUtil.retrieveItems("", configModel.getConfig())));

propertyNames = values.keySet().toArray(new String[0]);
}

@Override
public String[] getPropertyNames() {
return propertyNames;
}

@SuppressWarnings("unchecked")
@Override
public Object getProperty(String name) {
Object value = this.values.get(name);

// spring will not resolve nested placeholder of list, so try to fix the problem
if (value instanceof List) {
value = ((List<Object>) value).stream()
.filter(item -> item instanceof String)
.map(item -> environment.resolvePlaceholders((String) item))
.collect(Collectors.toList());
}
return value;
}
});
}

private static void addMappingToSpring(Environment environment) {
if (!(environment instanceof ConfigurableEnvironment)) {
return;
}

MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
if (propertySources.contains(MAPPING_PROPERTY_SOURCE_NAME)) {
return;
}

Map<String, Object> mappings = ConfigMapping.getConvertedMap(environment);
propertySources.addFirst(new MapPropertySource(MAPPING_PROPERTY_SOURCE_NAME, mappings));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,17 @@
package org.apache.servicecomb.core;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import org.apache.servicecomb.config.ConfigMapping;
import org.apache.servicecomb.config.DynamicPropertiesSource;
import org.apache.servicecomb.config.YAMLUtil;
import org.apache.servicecomb.config.file.MicroserviceConfigLoader;
import org.apache.servicecomb.foundation.bootstrap.BootStrapService;
import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

/**
* Add dynamic configuration, microserive.yaml to spring
Expand All @@ -62,8 +50,6 @@ public static boolean isExternalInit() {
return Boolean.getBoolean(EXTERNAL_INIT);
}

private final List<BootStrapService> bootStrapServices = SPIServiceUtils.getSortedService(BootStrapService.class);

private final List<DynamicPropertiesSource<?>> dynamicPropertiesSources;

public ConfigurationSpringInitializer(List<DynamicPropertiesSource<?>> dynamicPropertiesSources) {
Expand All @@ -83,87 +69,9 @@ public void setEnvironment(Environment environment) {
return;
}

addMicroserviceDefinitions(environment);

startupBootStrapService(environment);

addDynamicConfigurationToSpring(environment);
}

public static void addMicroserviceDefinitions(Environment environment) {
addMicroserviceYAMLToSpring(environment);
addMappingToSpring(environment);
}

private void startupBootStrapService(Environment environment) {
bootStrapServices.forEach(bootStrapService -> bootStrapService.startup(environment));
}

/**
* make springboot have a change to add microservice.yaml source earlier<br>
* to affect {@link Conditional}
* @param environment environment
*/
private static void addMicroserviceYAMLToSpring(Environment environment) {
if (!(environment instanceof ConfigurableEnvironment)) {
return;
}

MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
if (propertySources.contains(MICROSERVICE_PROPERTY_SOURCE_NAME)) {
return;
}

propertySources.addLast(new EnumerablePropertySource<MicroserviceConfigLoader>(MICROSERVICE_PROPERTY_SOURCE_NAME) {
private final Map<String, Object> values = new HashMap<>();

private final String[] propertyNames;

{
MicroserviceConfigLoader loader = new MicroserviceConfigLoader();
loader.loadAndSort();

loader.getConfigModels()
.forEach(configModel -> values.putAll(YAMLUtil.retrieveItems("", configModel.getConfig())));

propertyNames = values.keySet().toArray(new String[0]);
}

@Override
public String[] getPropertyNames() {
return propertyNames;
}

@SuppressWarnings("unchecked")
@Override
public Object getProperty(String name) {
Object value = this.values.get(name);

// spring will not resolve nested placeholder of list, so try to fix the problem
if (value instanceof List) {
value = ((List<Object>) value).stream()
.filter(item -> item instanceof String)
.map(item -> environment.resolvePlaceholders((String) item))
.collect(Collectors.toList());
}
return value;
}
});
}

private static void addMappingToSpring(Environment environment) {
if (!(environment instanceof ConfigurableEnvironment)) {
return;
}

MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
if (propertySources.contains(MAPPING_PROPERTY_SOURCE_NAME)) {
return;
}

Map<String, Object> mappings = ConfigMapping.getConvertedMap(environment);
propertySources.addFirst(new MapPropertySource(MAPPING_PROPERTY_SOURCE_NAME, mappings));
}

private void addDynamicConfigurationToSpring(Environment environment) {
if (!(environment instanceof ConfigurableEnvironment)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ public void onApplicationEvent(ApplicationEvent event) {
((AbstractApplicationContext) applicationContext).registerShutdownHook();
}

//SCBEngine init first, hence we do not need worry that when other beans need use the
//producer microserviceMeta, the SCBEngine is not inited.
// String serviceName = RegistryUtils.getMicroservice().getServiceName();
// SCBEngine.getInstance().setProducerMicroserviceMeta(new MicroserviceMeta(serviceName).setConsumer(false));
// SCBEngine.getInstance().setProducerProviderManager(applicationContext.getBean(ProducerProviderManager.class));
// SCBEngine.getInstance().setConsumerProviderManager(applicationContext.getBean(ConsumerProviderManager.class));
// SCBEngine.getInstance().setTransportManager(applicationContext.getBean(TransportManager.class));
scbEngine.setPriorityPropertyManager(applicationContext.getBean(PriorityPropertyManager.class));
scbEngine.setFilterChainsManager(applicationContext.getBean(FilterChainsManager.class));
scbEngine.getConsumerProviderManager().getConsumerProviderList()
Expand Down
23 changes: 2 additions & 21 deletions core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.apache.servicecomb.core.provider.producer.ProducerProviderManager;
import org.apache.servicecomb.core.registry.discovery.SwaggerLoader;
import org.apache.servicecomb.core.transport.TransportManager;
import org.apache.servicecomb.foundation.common.VendorExtensions;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
import org.apache.servicecomb.foundation.common.event.EventManager;
import org.apache.servicecomb.foundation.vertx.VertxUtils;
Expand Down Expand Up @@ -109,12 +108,8 @@ public class SCBEngine {

private final SwaggerEnvironment swaggerEnvironment = new SwaggerEnvironment();

private final VendorExtensions vendorExtensions = new VendorExtensions();

private SwaggerLoader swaggerLoader;

private Thread shutdownHook;

private RegistrationManager registrationManager;

private MicroserviceProperties microserviceProperties;
Expand Down Expand Up @@ -210,10 +205,6 @@ public ApplicationContext getApplicationContext() {
return applicationContext;
}

public VendorExtensions getVendorExtensions() {
return vendorExtensions;
}

public String getAppId() {
return this.microserviceProperties.getApplication();
}
Expand Down Expand Up @@ -317,12 +308,14 @@ protected void safeTriggerEvent(EventType eventType) {

@AllowConcurrentEvents
@Subscribe
@SuppressWarnings("unused")
public void onInvocationStart(InvocationStartEvent event) {
invocationStartedCounter.incrementAndGet();
}

@AllowConcurrentEvents
@Subscribe
@SuppressWarnings("unused")
public void onInvocationFinish(InvocationFinishEvent event) {
invocationFinishedCounter.incrementAndGet();
}
Expand Down Expand Up @@ -400,9 +393,6 @@ private void doRun() throws Exception {
status = SCBStatus.UP;
triggerEvent(EventType.AFTER_REGISTRY);

shutdownHook = new Thread(this::destroyForShutdownHook);
Runtime.getRuntime().addShutdownHook(shutdownHook);

// Keep this message for tests cases work.
LOGGER.warn("ServiceComb is ready.");
}
Expand All @@ -416,11 +406,6 @@ private void createProducerMicroserviceMeta() {
producerMicroserviceMeta.setMicroserviceVersionsMeta(new MicroserviceVersionsMeta(this));
}

public void destroyForShutdownHook() {
shutdownHook = null;
destroy();
}

/**
* not allow throw any exception
* even some step throw exception, must catch it and go on, otherwise shutdown process will be broken.
Expand All @@ -435,10 +420,6 @@ public synchronized void destroy() {
}

private void doDestroy() {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}

//Step 0: turn down the status of this instance in service center,
// so that the consumers can remove this instance record in advance
turnDownInstanceStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#
org.springframework.boot.env.EnvironmentPostProcessor=\
org.apache.servicecomb.springboot.starter.ConfigurationSpringBootInitializer
org.apache.servicecomb.core.ConfigurationSpringBootInitializer
4 changes: 0 additions & 4 deletions coverage-reports/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,6 @@
</dependency>

<!-- spring boot -->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-spring-boot-starter-servlet</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
import org.apache.servicecomb.springboot.starter.EnableServiceComb;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
Expand All @@ -36,7 +35,6 @@
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableServiceComb
public class CrossappClient {
@RpcReference(microserviceName = "appServer:appService", schemaId = "helloworld")
private static HelloWorld helloWorld;
Expand Down
2 changes: 1 addition & 1 deletion demo/demo-crossapp/crossapp-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-pojo</artifactId>
<artifactId>provider-jaxrs</artifactId>
</dependency>
</dependencies>

Expand Down
Loading

0 comments on commit 793b1c1

Please sign in to comment.