Skip to content

Commit

Permalink
修正样例问题,增加代码格式化 (#153)
Browse files Browse the repository at this point in the history
* fix: optimize the discovery inject logic and fix nacos config not working issue

* feat: 删除无用的导入

* feat: 增加actuator到demo中

* fix: add lossless healthcheck for example

* fix: add configuration comments
  • Loading branch information
andrewshan authored Apr 15, 2024
1 parent bf9bbdd commit 7bc4c23
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 131 deletions.
2 changes: 1 addition & 1 deletion polaris-agent-build/conf/polaris-agent.config
Original file line number Diff line number Diff line change
@@ -1 +1 @@
plugins.enable=spring-cloud-2021-plugin,spring-cloud-2020-plugin
plugins.enable=spring-cloud-2023-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ spec:
polarismesh.cn/javaagentVersion: "1.7.0-RC.0"
polarismesh.cn/javaagentFrameworkName: "spring-cloud"
polarismesh.cn/javaagentFrameworkVersion: "2023"
polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}"
polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\", \"spring.cloud.polaris.lossless.healthCheckPath\": \"/actuator/health\"}"
spec:
containers:
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-service-consumer-2023:1.7.0-RC.0-java17
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-2023-consumer:1.7.0-RC.0-java17
imagePullPolicy: Always
name: consumer
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spec:
polarismesh.cn/javaagentVersion: "1.7.0-RC.0"
polarismesh.cn/javaagentFrameworkName: "spring-cloud"
polarismesh.cn/javaagentFrameworkVersion: "2023"
polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}"
polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\", \"spring.cloud.polaris.lossless.healthCheckPath\": \"/actuator/health\"}"
spec:
containers:
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-2023-scg:1.7.0-RC.0-java17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ spec:
polarismesh.cn/javaagentVersion: "1.7.0-RC.0"
polarismesh.cn/javaagentFrameworkName: "spring-cloud"
polarismesh.cn/javaagentFrameworkVersion: "2023"
polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}"
polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\", \"spring.cloud.polaris.lossless.healthCheckPath\": \"/actuator/health\"}"
spec:
containers:
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-service-provider-2023:1.7.0-RC.0-java17
- image: polarismesh/polaris-javaagent-demo-sc-quickstart-2023-provider:1.7.0-RC.0-java17
imagePullPolicy: Always
name: consumer
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,40 @@
# 应用名称(必填)
# the application name for spring cloud, register as the service name in registry center
spring.application.name=application-name
# 配置北极星服务端地址
# polaris server address
spring.cloud.polaris.address=grpc\://127.0.0.1\:8091
# switch for spring cloud polaris all features
spring.cloud.polaris.enabled=true

# 启用 Java Agent 的 Spring Cloud Tencent 注册发现能力
# switch for spring cloud discovery feature
spring.cloud.discovery.enabled=true
# 配置服务注册发现的命名空间信息
# service discovery namespace value
spring.cloud.polaris.discovery.namespace=default
# 启用从北极星
# switch for polaris discovery feature
spring.cloud.polaris.discovery.enabled=true
# switch for polaris discovery(register) feature
spring.cloud.polaris.discovery.register=true

## 是否启用北极星的 LoadBalancer
# switch for polaris loadbalancer feature
spring.cloud.polaris.loadbalancer.enabled=true
#spring.cloud.polaris.loadbalancer.discoveryType=POLARIS
#spring.cloud.polaris.loadbalancer.strategy=random

## 是否启用北极星服务路由能力
spring.cloud.polaris.router.enabled=false
# 是否启用北极星的自定义路由能力
spring.cloud.polaris.router.rule-router.enabled=false
# 是否启用北极星的元数据路由能力
spring.cloud.polaris.router.metadata-router.enabled=false
# 是否启用北极星的就近路由能力
spring.cloud.polaris.router.nearby-router.enabled=false

## 是否启用北极星的服务限流能力
# switch for polaris router feature
spring.cloud.polaris.router.enabled=true
# switch for polaris router(rule-router) feature
spring.cloud.polaris.router.rule-router.enabled=true
# switch for polaris router(metadata-router) feature
spring.cloud.polaris.router.metadata-router.enabled=true
# switch for polaris router(nearby-router) feature
spring.cloud.polaris.router.nearby-router.enabled=true
# switch for polaris ratelimit feature
spring.cloud.polaris.ratelimit.enabled=false
# 设置触发限流时的提示信息
# spring.cloud.polaris.ratelimit.rejectRequestTips=""
# 设置触发限流时的响应码
# rejectHttpCode for polaris ratelimit, will be returned as limited
spring.cloud.polaris.ratelimit.rejectHttpCode=429
# 设置限流匀速排队最大排队时间
# maxQueuingTime for polaris ratelimit
spring.cloud.polaris.ratelimit.maxQueuingTime=1000

## RPC 调用增强
spring.cloud.tencent.rpc-enhancement.enabled=false
# 开启 RPC 调用结果上报
spring.cloud.tencent.rpc-enhancement.reporter.enabled=false

# 配置北极星监控指标上报
# switch for rpc-enhancement feature
spring.cloud.tencent.rpc-enhancement.enabled=true
# switch for rpc-enhancement reporter feature
spring.cloud.tencent.rpc-enhancement.reporter.enabled=true
# switch for polaris stat feature
spring.cloud.polaris.stat.enabled=false
# 指标上报监听端口
# port for polaris stat
spring.cloud.polaris.stat.port=0
# 指标上报暴露的 http path
# path for polaris stat
spring.cloud.polaris.stat.path=/metrics


## 开启双注册双发现到 nacos(默认情况不开启)
#spring.cloud.nacos.enabled=false
## 设置 nacos 服务注册中心的地址
#spring.cloud.nacos.discovery.server-addr=127.0.0.1\:8848
## 设置 nacos 的账户信息
#spring.cloud.nacos.username=nacos
#spring.cloud.nacos.password=nacos
## 是否开启从 nacos 拉取服务实例信息
#spring.cloud.nacos.discovery.enabled=false
## 是否开启将服务注册到 nacos
#spring.cloud.nacos.discovery.register-enabled=false
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void addPolarisTransformers(TransformOperations operations) {
operations.transform(ClassNames.APPLICATION_CONTEXT_AWARE, ApplicationContextAwareTransform.class);

// EnvironmentPostProcessor 处理
operations.transform(ClassNames.ENVIRONMENT_POST_PROCESSOR, ConfigurationInjectTransform.class);
// operations.transform(ClassNames.ENVIRONMENT_POST_PROCESSOR, ConfigurationInjectTransform.class);

}

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.context;
package cn.polarismesh.agent.plugin.spring.cloud.base;

import java.util.function.BiConsumer;

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

package cn.polarismesh.agent.plugin.spring.cloud.context;
package cn.polarismesh.agent.plugin.spring.cloud.base;

import java.util.function.Supplier;

import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.EnvironmentChangeEventListener;
import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.ServiceRuleManager;
import com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancerAutoConfiguration;
import com.tencent.cloud.polaris.registry.PolarisAutoServiceRegistration;
import com.tencent.cloud.polaris.registry.PolarisRegistration;
import com.tencent.cloud.polaris.registry.PolarisServiceRegistry;
import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration;
import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties;
import com.tencent.polaris.assembly.api.AssemblyAPI;
import com.tencent.polaris.client.api.SDKContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.function.Supplier;

import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.context.AbstractContextHandler;
import cn.polarismesh.agent.plugin.spring.cloud.base.AbstractContextHandler;
import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.function.Supplier;

import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.context.AbstractContextHandler;
import cn.polarismesh.agent.plugin.spring.cloud.base.AbstractContextHandler;
import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import cn.polarismesh.agent.core.common.logger.CommonLogger;
import cn.polarismesh.agent.core.common.logger.StdoutCommonLoggerFactory;
import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.config.ConfigHandler;
import cn.polarismesh.agent.plugin.spring.cloud.context.BaseBeanHandler;
import cn.polarismesh.agent.plugin.spring.cloud.base.BaseBeanHandler;
import cn.polarismesh.agent.plugin.spring.cloud.discovery.DiscoveryHandler;
import cn.polarismesh.agent.plugin.spring.cloud.metadata.MetadataHandler;
import cn.polarismesh.agent.plugin.spring.cloud.router.RouterHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ private void disableSpringCloudAlibabaAbility(ConfigurableEnvironment environmen
String disableCheck = "__disable__check__";
Properties properties = new Properties();
properties.setProperty("spring.cloud.polaris.config.import-check.enabled", "false");
// 设置 spring.cloud.sentinel.enabled 为 false
environment.getPropertySources().addFirst(new PropertiesPropertySource(disableCheck, properties));

LOGGER.info("[PolarisAgent] disable spring cloud alibaba all ability");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package cn.polarismesh.agent.plugin.spring.cloud.metadata;

import cn.polarismesh.agent.plugin.spring.cloud.context.AbstractContextHandler;
import cn.polarismesh.agent.plugin.spring.cloud.base.AbstractContextHandler;
import com.tencent.cloud.metadata.config.MetadataTransferAutoConfiguration;

import org.springframework.beans.BeansException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@

import cn.polarismesh.agent.core.common.utils.ClassUtils;
import cn.polarismesh.agent.plugin.spring.cloud.common.Holder;
import cn.polarismesh.agent.plugin.spring.cloud.context.AbstractContextHandler;
import cn.polarismesh.agent.plugin.spring.cloud.base.AbstractContextHandler;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.router.PolarisRouterServiceInstanceListSupplier;
import com.tencent.cloud.polaris.router.config.FeignAutoConfiguration;
import com.tencent.cloud.polaris.router.config.LoadBalancerConfiguration;
import com.tencent.cloud.polaris.router.config.RouterAutoConfiguration;
import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
Expand Down Expand Up @@ -80,15 +79,15 @@ public PolarisRuleBasedRouterProperties get() {
}
}).getBeanDefinition());
});
registerBean(applicationContext, "routerAutoConfiguration", (ctx, name) -> {
registerBean(applicationContext, "routerAutoConfiguration", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name,
BeanDefinitionBuilder.genericBeanDefinition(RouterAutoConfiguration.class).getBeanDefinition());
});
if (null != ClassUtils.getClazz("feign.RequestInterceptor",
Thread.currentThread().getContextClassLoader())) {
registerBean(applicationContext, "feignAutoConfiguration", (ctx, name) -> {
registerBean(applicationContext, "feignAutoConfiguration", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name,
Expand Down Expand Up @@ -124,9 +123,10 @@ public ServiceInstanceListSupplier get() {
});
}
}
String blockingEnable = applicationContext.getEnvironment().getProperty("spring.cloud.discovery.blocking.enabled");
String blockingEnable = applicationContext.getEnvironment()
.getProperty("spring.cloud.discovery.blocking.enabled");
if (null == blockingEnable || Boolean.parseBoolean(blockingEnable)) {
registerBean(applicationContext, "polarisRouterDiscoveryClientServiceInstanceListSupplier", (ctx, name) -> {
registerBean(applicationContext, "polarisRouterDiscoveryClientServiceInstanceListSupplier", (ctx, name) -> {
ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx;
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory();
beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition(ServiceInstanceListSupplier.class, new Supplier<ServiceInstanceListSupplier>() {
Expand All @@ -137,14 +137,15 @@ public ServiceInstanceListSupplier get() {
Map<String, RouterResponseInterceptor> responseInterceptors = applicationContext.getBeansOfType(RouterResponseInterceptor.class);
InstanceTransformer instanceTransformer = applicationContext.getBean("instanceTransformer", InstanceTransformer.class);
return new PolarisRouterServiceInstanceListSupplier(
ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().build((ConfigurableApplicationContext) applicationContext),
ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
.build((ConfigurableApplicationContext) applicationContext),
polarisSDKContextManager.getRouterAPI(),
new ArrayList<>(requestInterceptors.values()),
new ArrayList<>(responseInterceptors.values()),
instanceTransformer);
}
}).getBeanDefinition());
});
});

}
// registerBean(applicationContext, "loadBalancerConfiguration", (ctx, name) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package cn.polarismesh.agent.plugin.spring.cloud.rpc;

import cn.polarismesh.agent.plugin.spring.cloud.context.AbstractContextHandler;
import cn.polarismesh.agent.plugin.spring.cloud.base.AbstractContextHandler;
import com.tencent.cloud.plugin.discovery.adapter.config.NacosDiscoveryAdapterAutoConfiguration;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration;
import org.slf4j.Logger;
Expand Down

0 comments on commit 7bc4c23

Please sign in to comment.