use of com.alipay.sofa.rpc.registry.Registry in project sofa-rpc by sofastack.
the class DefaultProviderBootstrap method register.
/**
* 注册服务
*/
protected void register() {
if (providerConfig.isRegister()) {
List<RegistryConfig> registryConfigs = providerConfig.getRegistry();
if (registryConfigs != null) {
for (RegistryConfig registryConfig : registryConfigs) {
Registry registry = RegistryFactory.getRegistry(registryConfig);
registry.init();
registry.start();
try {
registry.register(providerConfig);
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
String appName = providerConfig.getAppName();
if (LOGGER.isWarnEnabled(appName)) {
LOGGER.errorWithApp(appName, LogCodes.getLog(LogCodes.ERROR_REGISTER_TO_REGISTRY, registryConfig.getId()), e);
}
}
}
}
}
}
use of com.alipay.sofa.rpc.registry.Registry in project sofa-boot by alipay.
the class RpcBindingAdapter method outBinding.
/**
* out binding, out binding means provide service
*
* @param contract binding contract
* @param binding binding object
* @param target binding target
* @param sofaRuntimeContext sofa runtime context
* @return binding result
*/
@Override
public Object outBinding(Object contract, RpcBinding binding, Object target, SofaRuntimeContext sofaRuntimeContext) {
ApplicationContext applicationContext = sofaRuntimeContext.getSofaRuntimeManager().getRootApplicationContext();
ProviderConfigContainer providerConfigContainer = applicationContext.getBean(ProviderConfigContainer.class);
ProcessorContainer processorContainer = applicationContext.getBean(ProcessorContainer.class);
String uniqueName = providerConfigContainer.createUniqueName((Contract) contract, binding);
ProviderConfig providerConfig = providerConfigContainer.getProviderConfig(uniqueName);
processorContainer.processorProvider(providerConfig);
if (providerConfig == null) {
throw new ServiceRuntimeException(LogCodes.getLog(LogCodes.INFO_SERVICE_METADATA_IS_NULL, uniqueName));
}
try {
providerConfig.export();
} catch (Exception e) {
throw new ServiceRuntimeException(LogCodes.getLog(LogCodes.ERROR_PROXY_PUBLISH_FAIL), e);
}
if (providerConfigContainer.isAllowPublish()) {
providerConfig.setRegister(true);
List<RegistryConfig> registrys = providerConfig.getRegistry();
for (RegistryConfig registryConfig : registrys) {
Registry registry = RegistryFactory.getRegistry(registryConfig);
registry.init();
registry.start();
registry.register(providerConfig);
}
}
return Boolean.TRUE;
}
use of com.alipay.sofa.rpc.registry.Registry in project sofa-boot by alipay.
the class ProviderConfigContainer method publishAllProviderConfig.
/**
* 发布所有 ProviderConfig 元数据信息到注册中心
*/
public void publishAllProviderConfig() {
for (ProviderConfig providerConfig : getAllProviderConfig()) {
ServerConfig serverConfig = (ServerConfig) providerConfig.getServer().get(0);
if (!serverConfig.getProtocol().equalsIgnoreCase(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
providerConfig.setRegister(true);
List<RegistryConfig> registrys = providerConfig.getRegistry();
for (RegistryConfig registryConfig : registrys) {
Registry registry = RegistryFactory.getRegistry(registryConfig);
registry.init();
registry.start();
registry.register(providerConfig);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("service published. interfaceId[" + providerConfig.getInterfaceId() + "]; protocol[" + serverConfig.getProtocol() + "]");
}
}
}
}
}
use of com.alipay.sofa.rpc.registry.Registry in project sofa-rpc by sofastack.
the class DefaultConsumerBootstrap method subscribeFromRegistries.
/**
* Subscribe provider list from all registries, the providers will be merged.
*
* @return Provider group list
*/
protected List<ProviderGroup> subscribeFromRegistries() {
List<ProviderGroup> result = new ArrayList<ProviderGroup>();
List<RegistryConfig> registryConfigs = consumerConfig.getRegistry();
if (CommonUtils.isEmpty(registryConfigs)) {
return result;
}
// 是否等待结果
int addressWaitTime = consumerConfig.getAddressWait();
int maxAddressWaitTime = SofaConfigs.getIntegerValue(consumerConfig.getAppName(), SofaOptions.CONFIG_MAX_ADDRESS_WAIT_TIME, SofaOptions.MAX_ADDRESS_WAIT_TIME);
addressWaitTime = addressWaitTime < 0 ? maxAddressWaitTime : Math.min(addressWaitTime, maxAddressWaitTime);
ProviderInfoListener listener = consumerConfig.getProviderInfoListener();
respondRegistries = addressWaitTime == 0 ? null : new CountDownLatch(registryConfigs.size());
// 从注册中心订阅 {groupName: ProviderGroup}
Map<String, ProviderGroup> tmpProviderInfoList = new HashMap<String, ProviderGroup>();
for (RegistryConfig registryConfig : registryConfigs) {
Registry registry = RegistryFactory.getRegistry(registryConfig);
registry.init();
registry.start();
try {
List<ProviderGroup> current;
try {
if (respondRegistries != null) {
consumerConfig.setProviderInfoListener(new WrapperClusterProviderInfoListener(listener, respondRegistries));
}
current = registry.subscribe(consumerConfig);
} finally {
if (respondRegistries != null) {
consumerConfig.setProviderInfoListener(listener);
}
}
if (current == null) {
// 未同步返回结果
continue;
} else {
if (respondRegistries != null) {
respondRegistries.countDown();
}
}
for (ProviderGroup group : current) {
// 当前注册中心的
String groupName = group.getName();
if (!group.isEmpty()) {
ProviderGroup oldGroup = tmpProviderInfoList.get(groupName);
if (oldGroup != null) {
oldGroup.addAll(group.getProviderInfos());
} else {
tmpProviderInfoList.put(groupName, group);
}
}
}
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
String appName = consumerConfig.getAppName();
if (LOGGER.isWarnEnabled(appName)) {
LOGGER.warnWithApp(appName, LogCodes.getLog(LogCodes.ERROR_SUBSCRIBE_FROM_REGISTRY, registryConfig.getId()), e);
}
}
}
if (respondRegistries != null) {
try {
respondRegistries.await(addressWaitTime, TimeUnit.MILLISECONDS);
} catch (Exception ignore) {
// NOPMD
}
}
return new ArrayList<ProviderGroup>(tmpProviderInfoList.values());
}
use of com.alipay.sofa.rpc.registry.Registry in project sofa-rpc by sofastack.
the class RpcRuntimeContext method destroy.
/**
* 销毁方法
*
* @param active 是否主动销毁
*/
private static void destroy(boolean active) {
// TODO 检查是否有其它需要释放的资源
RpcRunningState.setShuttingDown(true);
for (Destroyable.DestroyHook destroyHook : DESTROY_HOOKS) {
destroyHook.preDestroy();
}
List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
for (ProviderBootstrap bootstrap : EXPORTED_PROVIDER_CONFIGS) {
providerConfigs.add(bootstrap.getProviderConfig());
}
// 先反注册服务端
List<Registry> registries = RegistryFactory.getRegistries();
if (CommonUtils.isNotEmpty(registries) && CommonUtils.isNotEmpty(providerConfigs)) {
for (Registry registry : registries) {
registry.batchUnRegister(providerConfigs);
}
}
// 关闭启动的端口
ServerFactory.destroyAll();
// 关闭发布的服务
for (ProviderBootstrap bootstrap : EXPORTED_PROVIDER_CONFIGS) {
bootstrap.unExport();
}
// 关闭调用的服务
for (ConsumerBootstrap bootstrap : REFERRED_CONSUMER_CONFIGS) {
ConsumerConfig config = bootstrap.getConsumerConfig();
if (!CommonUtils.isFalse(config.getParameter(RpcConstants.HIDDEN_KEY_DESTROY))) {
// 除非不让主动unrefer
bootstrap.unRefer();
}
}
// 关闭注册中心
RegistryFactory.destroyAll();
// 关闭客户端的一些公共资源
ClientTransportFactory.closeAll();
// 卸载模块
if (!RpcRunningState.isUnitTestMode()) {
ModuleFactory.uninstallModules();
}
// 卸载钩子
for (Destroyable.DestroyHook destroyHook : DESTROY_HOOKS) {
destroyHook.postDestroy();
}
// 清理缓存
RpcCacheManager.clearAll();
RpcRunningState.setShuttingDown(false);
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("SOFA RPC Framework has been release all resources {}...", active ? "actively " : "");
}
}
Aggregations