Skip to content

Commit

Permalink
Cherrypick 1254 (#1260)
Browse files Browse the repository at this point in the history
* init interface class canonical name to avoid multi reflection

* Support sofa-rpc provider register blacklist/whitelist (#1254)

---------

Co-authored-by: 致节 <hzj266771@antgroup.com>
  • Loading branch information
HzjNeverStop and 致节 committed Oct 23, 2023
1 parent ced1622 commit 3b803af
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 13 deletions.
Expand Up @@ -77,8 +77,13 @@
public class SofaRpcAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ProviderConfigContainer providerConfigContainer() {
return new ProviderConfigContainer();
public ProviderConfigContainer providerConfigContainer(SofaBootRpcProperties sofaBootRpcProperties) {
ProviderConfigContainer providerConfigContainer = new ProviderConfigContainer();
providerConfigContainer.setProviderRegisterWhiteList(sofaBootRpcProperties
.getProviderRegisterWhiteList());
providerConfigContainer.setProviderRegisterBlackList(sofaBootRpcProperties
.getProviderRegisterBlackList());
return providerConfigContainer;
}

@Bean
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.springframework.util.StringUtils;

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

/**
Expand Down Expand Up @@ -341,6 +342,10 @@ public class SofaBootRpcProperties implements EnvironmentAware {
*/
private String dynamicConfig;

private List<String> providerRegisterWhiteList;

private List<String> providerRegisterBlackList;

public String getAftRegulationEffective() {
return StringUtils.isEmpty(aftRegulationEffective) ? getDotString(new Object() {
}.getClass().getEnclosingMethod().getName()) : aftRegulationEffective;
Expand Down Expand Up @@ -938,6 +943,22 @@ public void setBoltProcessInIoThread(Boolean boltProcessInIoThread) {
this.boltProcessInIoThread = boltProcessInIoThread;
}

public List<String> getProviderRegisterWhiteList() {
return providerRegisterWhiteList;
}

public void setProviderRegisterWhiteList(List<String> providerRegisterWhiteList) {
this.providerRegisterWhiteList = providerRegisterWhiteList;
}

public List<String> getProviderRegisterBlackList() {
return providerRegisterBlackList;
}

public void setProviderRegisterBlackList(List<String> providerRegisterBlackList) {
this.providerRegisterBlackList = providerRegisterBlackList;
}

@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
Expand Down
Expand Up @@ -16,14 +16,6 @@
*/
package com.alipay.sofa.rpc.boot.container;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.slf4j.Logger;
import org.springframework.util.StringUtils;

import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants;
import com.alipay.sofa.rpc.boot.log.SofaBootRpcLoggerFactory;
import com.alipay.sofa.rpc.boot.runtime.binding.RpcBinding;
Expand All @@ -33,6 +25,14 @@
import com.alipay.sofa.rpc.registry.Registry;
import com.alipay.sofa.rpc.registry.RegistryFactory;
import com.alipay.sofa.runtime.spi.binding.Contract;
import org.slf4j.Logger;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* ProviderConfig持有者.维护编程界面级别的RPC组件。
Expand All @@ -48,6 +48,10 @@ public class ProviderConfigContainer {
*/
private boolean allowPublish = false;

private List<String> providerRegisterWhiteList;

private List<String> providerRegisterBlackList;

/**
* ProviderConfig 缓存
*/
Expand All @@ -73,6 +77,23 @@ public void addProviderConfig(String key, ProviderConfig providerConfig) {
}
}

private boolean allowProviderRegister(ProviderConfig providerConfig) {
if (CollectionUtils.isEmpty(providerRegisterWhiteList)
&& CollectionUtils.isEmpty(providerRegisterBlackList)) {
return true;
}
String uniqueName = createUniqueNameByProvider(providerConfig);
if (!CollectionUtils.isEmpty(providerRegisterBlackList)
&& providerRegisterBlackList.contains(uniqueName)) {
return false;
}
if (!CollectionUtils.isEmpty(providerRegisterWhiteList)
&& !providerRegisterWhiteList.contains(uniqueName)) {
return false;
}
return true;
}

/**
* 获取 ProviderConfig
*
Expand Down Expand Up @@ -110,7 +131,11 @@ public void publishAllProviderConfig() {
ServerConfig serverConfig = (ServerConfig) providerConfig.getServer().get(0);
if (!serverConfig.getProtocol().equalsIgnoreCase(
SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
providerConfig.setRegister(true);
if (allowProviderRegister(providerConfig)) {
providerConfig.setRegister(true);
} else {
LOGGER.info("Provider will not register: [{}]", providerConfig.buildKey());
}

List<RegistryConfig> registrys = providerConfig.getRegistry();
for (RegistryConfig registryConfig : registrys) {
Expand Down Expand Up @@ -206,4 +231,30 @@ public String createUniqueName(Contract contract, RpcBinding binding) {
.append(uniqueId).append(protocol).toString();
}

/**
* Create UniqueName by interfaceId and uniqueId
*/
private String createUniqueNameByProvider(ProviderConfig providerConfig) {
String uniqueId = "";
if (StringUtils.hasText(providerConfig.getUniqueId())) {
uniqueId = ":" + providerConfig.getUniqueId();
}
return providerConfig.getInterfaceId() + uniqueId;
}

public void setProviderRegisterWhiteList(List<String> providerRegisterWhiteList) {
this.providerRegisterWhiteList = providerRegisterWhiteList;
}

public void setProviderRegisterBlackList(List<String> providerRegisterBlackList) {
this.providerRegisterBlackList = providerRegisterBlackList;
}

public List<String> getProviderRegisterWhiteList() {
return providerRegisterWhiteList;
}

public List<String> getProviderRegisterBlackList() {
return providerRegisterBlackList;
}
}
Expand Up @@ -72,8 +72,14 @@
@EnableConfigurationProperties(SofaBootRpcProperties.class)
public class SofaRpcTestAutoConfiguration {
@Bean
public ProviderConfigContainer providerConfigContainer() {
return new ProviderConfigContainer();
@ConditionalOnMissingBean
public ProviderConfigContainer providerConfigContainer(SofaBootRpcProperties sofaBootRpcProperties) {
ProviderConfigContainer providerConfigContainer = new ProviderConfigContainer();
providerConfigContainer.setProviderRegisterWhiteList(sofaBootRpcProperties
.getProviderRegisterWhiteList());
providerConfigContainer.setProviderRegisterBlackList(sofaBootRpcProperties
.getProviderRegisterBlackList());
return providerConfigContainer;
}

@Bean
Expand Down
@@ -0,0 +1,37 @@
/*
* 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.rpc.boot.test.provider;

import com.alipay.sofa.rpc.core.exception.SofaRouteException;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;

/**
* @author huzijie
* @version BlackListProviderConfigContainerTests.java, v 0.1 2023年09月27日 11:26 AM huzijie Exp $
*/
@TestPropertySource(properties = "com.alipay.sofa.rpc.providerRegisterBlackList=com.alipay.sofa.rpc.boot.test.bean.SampleFacade:uniqueId")
public class BlackListProviderConfigContainerTests extends ProviderConfigContainerTestBase {

@Test
public void checkProviderExported() {
Assertions.assertThat(sampleFacadeA.sayHi("Sofa")).isEqualTo("hi Sofa!");
Assertions.assertThatThrownBy(() -> sampleFacadeB.sayHi("Sofa")).isInstanceOf(SofaRouteException.class).
hasMessageContaining("RPC-020060001");
}
}
@@ -0,0 +1,76 @@
/*
* 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.rpc.boot.test.provider;

import com.alipay.sofa.rpc.boot.container.ProviderConfigContainer;
import com.alipay.sofa.rpc.boot.test.bean.SampleFacade;
import com.alipay.sofa.rpc.boot.test.bean.SampleFacadeImpl;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import org.junit.After;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;

/**
* @author huzijie
* @version ProviderConfigContainerTests.java, v 0.1 2023年09月27日 11:14 AM huzijie Exp $
*/
@SpringBootApplication
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@Import(ProviderConfigContainerTestBase.RpcPublishConfiguration.class)
public class ProviderConfigContainerTestBase {

@SofaReference(jvmFirst = false, binding = @SofaReferenceBinding(bindingType = "bolt"))
protected SampleFacade sampleFacadeA;

@SofaReference(jvmFirst = false, binding = @SofaReferenceBinding(bindingType = "bolt"), uniqueId = "uniqueId")
protected SampleFacade sampleFacadeB;

@Autowired
private ProviderConfigContainer providerConfigContainer;

@After
public void clearExported() {
providerConfigContainer.unExportAllProviderConfig();
}

@Configuration
static class RpcPublishConfiguration {

@SofaService(bindings = { @SofaServiceBinding(bindingType = "bolt") })
@Bean
public SampleFacade providerA() {
return new SampleFacadeImpl();
}

@SofaService(bindings = { @SofaServiceBinding(bindingType = "bolt") }, uniqueId = "uniqueId")
@Bean
public SampleFacade providerB() {
return new SampleFacadeImpl();
}
}

}
@@ -0,0 +1,37 @@
/*
* 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.rpc.boot.test.provider;

import com.alipay.sofa.rpc.core.exception.SofaRouteException;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;

/**
* @author huzijie
* @version WhiteListProviderConfigContainerTests.java, v 0.1 2023年09月27日 11:27 AM huzijie Exp $
*/
@TestPropertySource(properties = "com.alipay.sofa.rpc.providerRegisterWhiteList=com.alipay.sofa.rpc.boot.test.bean.SampleFacade:uniqueId")
public class WhiteListProviderConfigContainerTests extends ProviderConfigContainerTestBase {

@Test
public void checkProviderExported() {
Assertions.assertThatThrownBy(() -> sampleFacadeA.sayHi("Sofa")).isInstanceOf(SofaRouteException.class).
hasMessageContaining("RPC-020060001");
Assertions.assertThat(sampleFacadeB.sayHi("Sofa")).isEqualTo("hi Sofa!");
}
}

0 comments on commit 3b803af

Please sign in to comment.