Skip to content

Commit

Permalink
Support sofa-rpc provider register blacklist/whitelist (#1254)
Browse files Browse the repository at this point in the history
Co-authored-by: 致节 <hzj266771@antgroup.com>
  • Loading branch information
HzjNeverStop and 致节 authored Oct 10, 2023
1 parent 0104a65 commit 76e7def
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.util.ObjectUtils;

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

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

private List<String> providerRegisterWhiteList;

private List<String> providerRegisterBlackList;

public boolean isEnableAutoPublish() {
return enableAutoPublish;
}
Expand Down Expand Up @@ -920,6 +925,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
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseFilter;
import java.util.List;
import java.util.Map;

import static com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA;
Expand Down Expand Up @@ -368,6 +369,20 @@ void customRegistryConfigContainer() {
});
}

@Test
void customProviderConfigContainer() {
this.contextRunner.withPropertyValues(
"sofa.boot.rpc.providerRegisterWhiteList=a,b,c",
"sofa.boot.rpc.providerRegisterBlackList=e,f,g").
run(context -> {
ProviderConfigContainer providerConfigContainer = context.getBean(ProviderConfigContainer.class);
List<String> whiteList = providerConfigContainer.getProviderRegisterWhiteList();
assertThat(whiteList).containsExactly("a", "b", "c");
List<String> blackList = providerConfigContainer.getProviderRegisterBlackList();
assertThat(blackList).containsExactly("e", "f", "g");
});
}

@Configuration(proxyBeanMethods = false)
static class CustomProviderConfigContainerConfiguration {
@Bean
Expand Down
Original file line number Diff line number Diff line change
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;
}
}
Original file line number Diff line number Diff line change
@@ -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.smoke.tests.rpc.provider;

import com.alipay.sofa.rpc.core.exception.SofaRouteException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.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 = "sofa.boot.rpc.providerRegisterBlackList=com.alipay.sofa.smoke.tests.rpc.boot.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("Cannot get the service address of service [com.alipay.sofa.smoke.tests.rpc.boot.bean.SampleFacade:1.0:uniqueId]");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.rpc.provider;

import com.alipay.sofa.rpc.boot.container.ProviderConfigContainer;
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 com.alipay.sofa.smoke.tests.rpc.boot.RpcSofaBootApplication;
import com.alipay.sofa.smoke.tests.rpc.boot.bean.SampleFacade;
import com.alipay.sofa.smoke.tests.rpc.boot.bean.SampleFacadeImpl;
import org.junit.jupiter.api.AfterEach;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
* @author huzijie
* @version ProviderConfigContainerTests.java, v 0.1 2023年09月27日 11:14 AM huzijie Exp $
*/
@SpringBootTest(classes = RpcSofaBootApplication.class, properties = "sofa.boot.actuator.health.skipAll=true", 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;

@AfterEach
private 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();
}
}

}
Original file line number Diff line number Diff line change
@@ -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.smoke.tests.rpc.provider;

import com.alipay.sofa.rpc.core.exception.SofaRouteException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.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 = "sofa.boot.rpc.providerRegisterWhiteList=com.alipay.sofa.smoke.tests.rpc.boot.bean.SampleFacade:uniqueId")
public class WhiteListProviderConfigContainerTests extends ProviderConfigContainerTestBase {

@Test
public void checkProviderExported() {
Assertions.assertThatThrownBy(() -> sampleFacadeA.sayHi("Sofa")).isInstanceOf(SofaRouteException.class).
hasMessageContaining("Cannot get the service address of service [com.alipay.sofa.smoke.tests.rpc.boot.bean.SampleFacade:1.0]");
Assertions.assertThat(sampleFacadeB.sayHi("Sofa")).isEqualTo("hi Sofa!");
}
}

0 comments on commit 76e7def

Please sign in to comment.