Skip to content

Commit

Permalink
feat: add configuration center integration (#151)
Browse files Browse the repository at this point in the history
* feat: new add spring-cloud-2023 plugin

* feat: fix rpc enhancement的bean重复加载的问题

* feat: add deployment and dockerfiles for examples

* feat: 解决开启路由后,服务调用报错的问题

* feat: modify build docker scripts

* feat: 更新example镜像文件

* feat: 支持scg的quickstart

* fix: 全链路灰度及路由插件加载问题

* fix: support feign lane router

* fix: 修复就近路由问题

* feat: 支持自动获取地址并注入

* fix: 规范化样例的artifactId

* feat: 统一依赖polaris-all,避免jar冲突

* fix: 删除不必要临时文件

* fix: 修复配置文件路径错误的问题

* fix: add curl into init-container

* feat: add configuration center integration
  • Loading branch information
andrewshan authored Apr 12, 2024
1 parent 53138a5 commit f4476c5
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public static class EchoController {
@Value("${server.port}")
private int port;

@Value("${custom.config:none}")
private String customConfig;

public EchoController(RestTemplate restTemplate, Registration registration) {
this.template = restTemplate;
this.registration = registration;
Expand All @@ -70,6 +73,10 @@ public ResponseEntity<String> rest(@PathVariable String str) {
return new ResponseEntity<>(content, HttpStatus.OK);
}

@GetMapping("/custom/config")
public ResponseEntity<String> getCustomConfig() {
return new ResponseEntity<>(String.valueOf(customConfig), HttpStatus.OK);
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -37,6 +39,7 @@ public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}


@RestController
public static class EchoController {

Expand All @@ -45,6 +48,14 @@ public static class EchoController {
@Value("${server.port}")
private int port;

@Value("${custom.config:none}")
private String customConfig;

@GetMapping("/custom/config")
public ResponseEntity<String> getCustomConfig() {
return new ResponseEntity<>(String.valueOf(customConfig), HttpStatus.OK);
}

public EchoController(Registration registration) {
this.registration = registration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import cn.polarismesh.agent.core.extension.transform.TransformOperations;
import cn.polarismesh.agent.plugin.spring.cloud.common.ClassNames;
import cn.polarismesh.agent.plugin.spring.cloud.common.Constant;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.aware.ApplicationContextAwareInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.aware.BlockingLoadBalancerClientInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.disable.alibaba.DisableSpringCloudAlibabaInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.discovery.DiscoveryInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.discovery.reactive.ReactiveDiscoveryInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.serviceregistry.RegistryInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ApplicationContextAwareInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.BlockingLoadBalancerClientInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.DisableSpringCloudAlibabaInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.discovery.DiscoveryInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.discovery.ReactiveDiscoveryInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.discovery.registry.RegistryInterceptor;

import org.springframework.context.ApplicationContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
import com.tencent.cloud.polaris.extend.nacos.NacosConfigModifier;
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
import com.tencent.cloud.polaris.router.RouterConfigModifier;
import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties;
Expand Down Expand Up @@ -126,7 +127,7 @@ private static void initProperties() {
polarisStatProperties = new PolarisStatProperties();
losslessProperties = new LosslessProperties();
rpcEnhancementReporterProperties = new RpcEnhancementReporterProperties();

polarisCryptoConfigProperties = new PolarisCryptoConfigProperties();
discoveryProperties.setService(Holder.getLocalService());
}

Expand Down Expand Up @@ -155,6 +156,8 @@ public static void init() {
}
discoveryProperties.setNamespace(namespace);

polarisContextProperties.setNamespace(namespace);
polarisContextProperties.setService(discoveryProperties.getService());
bindObject("spring.cloud.consul", consulContextProperties, environment);
bindObject("spring.cloud.nacos.discovery", nacosContextProperties, environment);

Expand All @@ -163,6 +166,7 @@ public static void init() {
bindObject("spring.cloud.polaris.router.nearby-router", nearByRouterProperties, environment);
bindObject("spring.cloud.polaris.router.metadata-router", metadataRouterProperties, environment);

bindObject("spring.cloud.polaris.config.crypto", polarisCryptoConfigProperties, environment);
// 配置中心
bindObject("spring.cloud.polaris.config", polarisConfigProperties, environment);

Expand Down Expand Up @@ -237,17 +241,20 @@ private static void runConfigModifiers(Environment environment) throws IOExcepti
new ModifyAddress(polarisContextProperties),
new DiscoveryConfigModifier(discoveryProperties),
new PolarisDiscoveryConfigModifier(discoveryProperties),
new RouterConfigModifier(nearByRouterProperties),
//new RateLimitConfigModifier(rateLimitProperties),
new StatConfigModifier(polarisStatProperties, environment),
new StatConfigModifier(polarisStatProperties, environment)
//new CircuitBreakerConfigModifier(rpcEnhancementReporterProperties),
new LosslessConfigModifier(losslessProperties)
));
if (consulContextProperties.isEnabled()) {
modifiers.add(new ConsulConfigModifier(consulContextProperties));
}
if (nacosContextProperties.isEnabled()) {
modifiers.add(new NacosConfigModifier(nacosContextProperties));
}
if (losslessProperties.isEnabled()) {
modifiers.add(new LosslessConfigModifier(losslessProperties));
}
if (polarisConfigProperties.isEnabled()) {
modifiers.add(new ConfigurationModifier(polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties));
}
Expand Down Expand Up @@ -323,11 +330,11 @@ public static RpcEnhancementReporterProperties getRpcEnhancementReporterProperti
return rpcEnhancementReporterProperties;
}

static PolarisConfigProperties getPolarisConfigProperties() {
public static PolarisConfigProperties getPolarisConfigProperties() {
return polarisConfigProperties;
}

static PolarisCryptoConfigProperties getPolarisCryptoConfigProperties() {
public static PolarisCryptoConfigProperties getPolarisCryptoConfigProperties() {
return polarisCryptoConfigProperties;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 cn.polarismesh.agent.plugin.spring.cloud.config;

import java.util.function.Supplier;

import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.context.AbstractContextHandler;
import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration;
import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.cloud.context.properties.ConfigurationPropertiesBeans;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

public class ConfigHandler extends AbstractContextHandler {

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (!Holder.getPolarisConfigProperties().isEnabled()) {
return;
}
registerBean(applicationContext, "polarisProperties", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name,
BeanDefinitionBuilder.genericBeanDefinition(PolarisConfigProperties.class, new Supplier<PolarisConfigProperties>() {
@Override
public PolarisConfigProperties get() {
return Holder.getPolarisConfigProperties();
}
}).getBeanDefinition());
});
registerBean(applicationContext, "polarisCryptoConfigProperties", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name,
BeanDefinitionBuilder.genericBeanDefinition(PolarisCryptoConfigProperties.class, new Supplier<PolarisCryptoConfigProperties>() {
@Override
public PolarisCryptoConfigProperties get() {
return Holder.getPolarisCryptoConfigProperties();
}
}).getBeanDefinition());
});
registerBean(applicationContext, "polarisPropertySourceManager", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name,
BeanDefinitionBuilder.genericBeanDefinition(PolarisPropertySourceManager.class, new Supplier<PolarisPropertySourceManager>() {
@Override
public PolarisPropertySourceManager get() {
return new PolarisPropertySourceManager();
}
}).getBeanDefinition());
});
registerBean(applicationContext, "configFileService", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name,
BeanDefinitionBuilder.genericBeanDefinition(ConfigFileService.class, new Supplier<ConfigFileService>() {
@Override
public ConfigFileService get() {
return ConfigFileServiceFactory.createConfigFileService(Holder.getContextManager().getSDKContext());
}
}).getBeanDefinition());
});
registerBean(applicationContext, "polarisConfigFileLocator", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name,
BeanDefinitionBuilder.genericBeanDefinition(PolarisConfigFileLocator.class, new Supplier<PolarisConfigFileLocator>() {
@Override
public PolarisConfigFileLocator get() {
ConfigFileService configFileService = cfgCtx.getBean("configFileService", ConfigFileService.class);
PolarisPropertySourceManager polarisPropertySourceManager = cfgCtx.getBean("polarisPropertySourceManager", PolarisPropertySourceManager.class);
Environment environment = cfgCtx.getBean("environment", Environment.class);
return new PolarisConfigFileLocator(Holder.getPolarisConfigProperties(),
Holder.getPolarisContextProperties(), configFileService,
polarisPropertySourceManager, environment);
}
}).getBeanDefinition());
});
// registerBean(applicationContext, "affectedConfigurationPropertiesRebinder", (ctx, name) -> {
// ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
// DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
// beanFactory.registerBeanDefinition(name,
// BeanDefinitionBuilder.genericBeanDefinition(ConfigurationPropertiesRebinder.class, new Supplier<ConfigurationPropertiesRebinder>() {
// @Override
// public ConfigurationPropertiesRebinder get() {
// ConfigurationPropertiesBeans beans = cfgCtx.getBean(ConfigurationPropertiesBeans.class);
// return new AffectedConfigurationPropertiesRebinder(beans);
// }
// }).getBeanDefinition());
// });

registerBean(applicationContext, "polarisConfigAutoConfiguration", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name,
BeanDefinitionBuilder.genericBeanDefinition(PolarisConfigAutoConfiguration.class).getBeanDefinition());
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License.
*/

package cn.polarismesh.agent.plugin.spring.cloud.interceptor.aware.handler;
package cn.polarismesh.agent.plugin.spring.cloud.context;

import java.util.function.BiConsumer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
* specific language governing permissions and limitations under the License.
*/

package cn.polarismesh.agent.plugin.spring.cloud.interceptor.aware.handler;
package cn.polarismesh.agent.plugin.spring.cloud.context;

import java.util.function.Supplier;

import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.metadata.config.MetadataTransferAutoConfiguration;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.ServiceRuleManager;
import com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration;
Expand Down Expand Up @@ -84,8 +83,6 @@ public PolarisContextProperties get() {
beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition(
PolarisLoadBalancerAutoConfiguration.class).getBeanDefinition());
});


}

private void registerPolarisSDKContextManager(ApplicationContext context) {
Expand All @@ -100,6 +97,5 @@ public PolarisSDKContextManager get() {
}
}).getBeanDefinition());
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
* specific language governing permissions and limitations under the License.
*/

package cn.polarismesh.agent.plugin.spring.cloud.interceptor.discovery;
package cn.polarismesh.agent.plugin.spring.cloud.discovery;

import cn.polarismesh.agent.core.common.utils.ReflectionUtils;
import cn.polarismesh.agent.plugin.spring.cloud.common.DiscoveryUtils;
import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.BaseInterceptor;
import com.tencent.polaris.api.utils.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
* specific language governing permissions and limitations under the License.
*/

package cn.polarismesh.agent.plugin.spring.cloud.common;
package cn.polarismesh.agent.plugin.spring.cloud.discovery;

import java.util.Objects;

import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* specific language governing permissions and limitations under the License.
*/

package cn.polarismesh.agent.plugin.spring.cloud.interceptor.discovery;
package cn.polarismesh.agent.plugin.spring.cloud.discovery;

import cn.polarismesh.agent.plugin.spring.cloud.common.DiscoveryUtils;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
* specific language governing permissions and limitations under the License.
*/

package cn.polarismesh.agent.plugin.spring.cloud.interceptor.discovery.reactive;
package cn.polarismesh.agent.plugin.spring.cloud.discovery;

import cn.polarismesh.agent.core.common.utils.ReflectionUtils;
import cn.polarismesh.agent.plugin.spring.cloud.common.DiscoveryUtils;
import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.BaseInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.discovery.PolarisServiceDiscovery;
import com.tencent.polaris.api.utils.StringUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License.
*/

package cn.polarismesh.agent.plugin.spring.cloud.interceptor.serviceregistry;
package cn.polarismesh.agent.plugin.spring.cloud.discovery.registry;

import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* specific language governing permissions and limitations under the License.
*/

package cn.polarismesh.agent.plugin.spring.cloud.interceptor.serviceregistry;
package cn.polarismesh.agent.plugin.spring.cloud.discovery.registry;


import java.util.Collections;

import cn.polarismesh.agent.core.common.utils.ReflectionUtils;
import cn.polarismesh.agent.plugin.spring.cloud.common.DiscoveryUtils;
import cn.polarismesh.agent.plugin.spring.cloud.discovery.DiscoveryUtils;
import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.BaseInterceptor;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
Expand Down
Loading

0 comments on commit f4476c5

Please sign in to comment.