Skip to content

Commit

Permalink
Merge v3.19.0 (#1182)
Browse files Browse the repository at this point in the history
* avoid npe (#1177)

Co-authored-by: 致节 <hzj266771@antgroup.com>
(cherry picked from commit 7e5fd1e)

* Add annotation sofa service bean (#1176)
(cherry picked from commit 123b3eb)

* straight application listener order (#1179)
(cherry picked from commit bb45ec7)

---------

Co-authored-by: Dando <jialindeng7@163.com>
  • Loading branch information
HzjNeverStop and crazysaltfish authored Jun 5, 2023
1 parent 50ee113 commit 6b77bb7
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.sofa.boot.constant.SofaBootConstants;
import com.alipay.sofa.boot.util.ApplicationListenerOrderConstants;
import com.alipay.sofa.boot.util.SofaBootEnvUtils;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.properties.bind.Bindable;
Expand Down Expand Up @@ -102,6 +103,6 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {

@Override
public int getOrder() {
return HIGHEST_PRECEDENCE + 30;
return ApplicationListenerOrderConstants.SOFA_TRACER_CONFIGURATION_LISTENER_ORDER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.runtime.api.annotation;

import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation used to create a SOFA service, which will also be created as a bean.
* Sample usage:
*
* <pre>
*
* &#064;SofaServiceBean(uniqueId = &quot;aop&quot;)
* public class SampleServiceImpl implements SampleService {
*
* &#064;Override
* public String say() {
* return &quot;sampleService&quot;;
* }
* }
* </pre>
*
* @author xunfang 23/5/23
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@SofaService
@Component
public @interface SofaServiceBean {
@AliasFor(annotation = Component.class)
String value() default "";

@AliasFor(annotation = SofaService.class)
Class<?> interfaceType() default void.class;

@AliasFor(annotation = SofaService.class)
String uniqueId() default "";

@AliasFor(annotation = SofaService.class)
SofaServiceBinding[] bindings() default { @SofaServiceBinding };
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ public void unregister(ComponentInfo componentInfo) throws ServiceRuntimeExcepti
ComponentType componentType = componentName.getType();

Map<ComponentName, ComponentInfo> typesRi = resolvedRegistry.get(componentType);
typesRi.remove(componentName);
if (typesRi != null) {
typesRi.remove(componentName);
}
}

componentInfo.unregister();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.boot.listener;

import com.alipay.sofa.boot.constant.SofaBootConstants;
import com.alipay.sofa.boot.util.ApplicationListenerOrderConstants;
import com.alipay.sofa.common.config.SofaConfigs;
import com.alipay.sofa.common.config.source.AbstractConfigSource;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
Expand Down Expand Up @@ -68,6 +69,6 @@ public boolean hasKey(String key) {

@Override
public int getOrder() {
return LOWEST_PRECEDENCE;
return ApplicationListenerOrderConstants.SOFA_CONFIG_SOURCE_SUPPORT_LISTENER_ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alipay.sofa.boot.constant.SofaBootConstants;
import com.alipay.sofa.boot.util.SofaBootEnvUtils;
import com.alipay.sofa.common.log.env.LogEnvUtils;
import com.alipay.sofa.boot.util.ApplicationListenerOrderConstants;
import org.springframework.boot.Banner;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
Expand Down Expand Up @@ -83,7 +84,7 @@ private void assemblyRequireProperties(ConfigurableEnvironment environment) {

@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;
return ApplicationListenerOrderConstants.SOFA_BOOTSTRAP_RUN_LISTENER_ORDER;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package com.alipay.sofa.boot.logging;

import com.alipay.sofa.boot.util.ApplicationListenerOrderConstants;
import com.alipay.sofa.boot.util.SofaBootEnvUtils;
import com.alipay.sofa.common.log.CommonLoggingConfigurations;
import com.alipay.sofa.common.log.Constants;
import com.alipay.sofa.common.log.env.LogEnvUtils;
import com.alipay.sofa.common.thread.SofaThreadPoolConstants;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
Expand All @@ -42,7 +42,7 @@
*/
public class LogEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {

public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER + 1;
public static final int ORDER = ApplicationListenerOrderConstants.LOG_ENVIRONMENT_PREPARING_LISTENER_ORDER;

/**
* support use config to disable sofa common thread pool monitor.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.boot.util;

import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;

/**
* ApplicationListener 顺序常量
*
* @author xunfang
* @version ApplicationListenerOrderConstants.java, v 0.1 2023/6/1
*/
public class ApplicationListenerOrderConstants {
/**
* 必须最先执行
*/
public static final int SOFA_BOOTSTRAP_RUN_LISTENER_ORDER = Ordered.HIGHEST_PRECEDENCE;

/**
* 必须在其他会触发 sofa-common-tools 日志上下文初始化的组件之前
*/
public static final int LOG_ENVIRONMENT_PREPARING_LISTENER_ORDER = Ordered.HIGHEST_PRECEDENCE + 12;

/**
* 必须在 LogEnvironmentPreparingListener 之后, 且 LoggingApplicationListener 之前
*/
public static final int SOFA_CONFIG_SOURCE_SUPPORT_LISTENER_ORDER = LOG_ENVIRONMENT_PREPARING_LISTENER_ORDER + 3;

public static final int SOFA_TRACER_CONFIGURATION_LISTENER_ORDER = Ordered.HIGHEST_PRECEDENCE + 50;

static {
Assert.isTrue(
SOFA_CONFIG_SOURCE_SUPPORT_LISTENER_ORDER < LoggingApplicationListener.DEFAULT_ORDER,
"SofaConfigSourceSupportListener must init after LogEnvironmentPostProcessor");
}
}
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.smoke.tests.runtime.impl;

import com.alipay.sofa.runtime.api.annotation.SofaServiceBean;
import com.alipay.sofa.smoke.tests.runtime.service.SampleService;

/**
* SofaServiceBeanService
*
* @author xunfang
* @version SofaServiceBeanService.java, v 0.1 2023/5/23
*/
@SofaServiceBean(value = "sofaServiceBeanService", uniqueId = "sofaServiceBeanService")
public class SofaServiceBeanService implements SampleService {
@Override
public String service() {
return "sofaServiceBeanService";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.smoke.tests.runtime.spring;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.smoke.tests.runtime.RuntimeSofaBootApplication;
import com.alipay.sofa.smoke.tests.runtime.service.SampleService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;

/**
* SofaServiceBeanAnnotationTests
*
* @author xunfang
* @version SofaServiceBeanAnnotationTests.java, v 0.1 2023/5/23
*/
@SpringBootTest(classes = RuntimeSofaBootApplication.class)
public class SofaServiceBeanAnnotationTests {

@SofaReference(uniqueId = "sofaServiceBeanService")
private SampleService sofaServiceBeanService;

@Test
public void testSofaServiceBean() {
assertThat(sofaServiceBeanService).isNotNull();
assertThat("sofaServiceBeanService").isEqualTo(sofaServiceBeanService.service());
}

}

0 comments on commit 6b77bb7

Please sign in to comment.