From 0aa8b49c5bd1ec3eb60ae6bafe4c11aaf574387b Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Sat, 13 Apr 2024 23:29:23 +0800 Subject: [PATCH 01/24] fix: optimize the discovery inject logic and fix nacos config not working issue --- polaris-agent-build/bin/start.sh | 5 +- .../service-a/deployment.yaml | 8 +- .../gray-release-examples/service-a/pom.xml | 4 + .../src/main/resources/application.properties | 5 - .../src/main/resources/application.yaml | 30 ++++ .../service-b/deployment.yaml | 8 +- .../gray-release-examples/service-b/pom.xml | 4 + .../src/main/resources/application.properties | 5 - .../src/main/resources/application.yaml | 30 ++++ .../service-c/deployment.yaml | 8 +- .../gray-release-examples/service-c/pom.xml | 4 + .../src/main/resources/application.properties | 5 - .../src/main/resources/application.yaml | 30 ++++ .../service-gateway/deployment.yaml | 16 +- .../service-gateway/pom.xml | 4 + .../src/main/resources/application.properties | 5 - .../src/main/resources/application.yaml | 36 +++++ .../consumer/deployment.yaml | 4 +- .../quickstart-examples/consumer/pom.xml | 4 + .../cloud/cloud/ConsumerApplication.java | 2 + .../src/main/resources/application.properties | 5 - .../src/main/resources/application.yaml | 30 ++++ .../gateway/deployment.yaml | 4 +- .../quickstart-examples/gateway/pom.xml | 4 + .../src/main/resources/application.properties | 5 - .../src/main/resources/application.yaml | 36 +++++ .../provider/deployment.yaml | 4 +- .../quickstart-examples/provider/pom.xml | 4 + .../cloud/cloud/ProviderApplication.java | 2 + .../src/main/resources/application.properties | 7 - .../src/main/resources/application.yaml | 32 ++++ .../spring-cloud-2023-plugin/pom.xml | 16 -- .../agent/plugin/spring/cloud/MainPlugin.java | 94 +----------- .../plugin/spring/cloud/common/Holder.java | 8 + .../spring/cloud/config/ConfigHandler.java | 16 -- .../spring/cloud/context/BaseBeanHandler.java | 12 ++ .../cloud/discovery/DiscoveryHandler.java | 122 +++++++++++++++ .../cloud/discovery/DiscoveryInterceptor.java | 82 ---------- .../cloud/discovery/DiscoveryUtils.java | 56 ------- .../discovery/PolarisServiceDiscovery.java | 86 ----------- .../ReactiveDiscoveryInterceptor.java | 83 ---------- .../LosslessProxyServiceRegistry.java | 76 --------- .../registry/RegistryInterceptor.java | 144 ------------------ .../ApplicationContextAwareInterceptor.java | 13 +- ...BlockingLoadBalancerClientInterceptor.java | 23 --- ...va => ConfigurationInjectInterceptor.java} | 28 ++-- .../EnvironmentChangeEventListener.java | 48 ++++++ .../spring/cloud/router/RouterHandler.java | 70 ++++++++- pom.xml | 2 +- 49 files changed, 573 insertions(+), 756 deletions(-) delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/src/main/resources/application.properties create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/src/main/resources/application.yaml delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/src/main/resources/application.properties create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/src/main/resources/application.yaml delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/src/main/resources/application.properties create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/src/main/resources/application.yaml delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/src/main/resources/application.properties create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/resources/application.properties create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/resources/application.yaml delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/src/main/resources/application.properties create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/src/main/resources/application.yaml delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/resources/application.properties create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/resources/application.yaml create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryInterceptor.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryUtils.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/PolarisServiceDiscovery.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/ReactiveDiscoveryInterceptor.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/registry/LosslessProxyServiceRegistry.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/registry/RegistryInterceptor.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/BlockingLoadBalancerClientInterceptor.java rename polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/{DisableSpringCloudAlibabaInterceptor.java => ConfigurationInjectInterceptor.java} (71%) create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/EnvironmentChangeEventListener.java diff --git a/polaris-agent-build/bin/start.sh b/polaris-agent-build/bin/start.sh index 8fe61095..c18cfe36 100644 --- a/polaris-agent-build/bin/start.sh +++ b/polaris-agent-build/bin/start.sh @@ -35,6 +35,7 @@ custom_plugin_properties=${JAVA_AGENT_PLUGIN_CONF} echo "${custom_plugin_properties}" > ${polaris_agent_dir_name}/conf/plugin/${java_agent_config_dir}/application.properties # 第三步,将地域信息拉取并设置进配置文件 +# 腾讯云不能拿到大区,因此腾讯云上的region对应的是北极星的zone,zone对应北极星的campus target_config_file=${polaris_agent_dir_name}/conf/plugin/${java_agent_config_dir}/application.properties echo "start to fetch region, target config file ${target_config_file}" @@ -42,7 +43,7 @@ region="$(curl -s --connect-timeout 10 -m 10 http://metadata.tencentyun.com/late region_code=$? echo "region is ${region}, return code is ${region_code}" if [ ${region_code} -eq 0 ] && [ -n ${region} ]; then - sed -i "s/spring.cloud.tencent.metadata.content.region=\"\"/spring.cloud.tencent.metadata.content.region=${region}/g" ${target_config_file} + sed -i "s/spring.cloud.tencent.metadata.content.region=\"\"/spring.cloud.tencent.metadata.content.zone=${region}/g" ${target_config_file} fi echo "start to fetch zone" @@ -50,5 +51,5 @@ zone="$(curl -s --connect-timeout 10 -m 10 http://metadata.tencentyun.com/latest zone_code=$? echo "region is ${zone}, return code is ${zone_code}" if [ ${zone_code} -eq 0 ] && [ -n ${zone} ]; then - sed -i "s/spring.cloud.tencent.metadata.content.zone=\"\"/spring.cloud.tencent.metadata.content.zone=${zone}/g" ${target_config_file} + sed -i "s/spring.cloud.tencent.metadata.content.zone=\"\"/spring.cloud.tencent.metadata.content.campus=${zone}/g" ${target_config_file} fi \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/deployment.yaml index fed15d02..a8b7a25d 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/deployment.yaml @@ -17,13 +17,13 @@ spec: app: service-a-2023 annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.6.1" + 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\"}" + polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-quickstart-2023-consumer:1.6.1-java17 + - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-a:1.7.0-RC.0-java17 imagePullPolicy: Always name: consumer resources: @@ -35,7 +35,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dserver.port=65203 -jar main.jar + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/pom.xml index 69ddbacc..8e762915 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/pom.xml @@ -47,6 +47,10 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + org.springframework.cloud diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/src/main/resources/application.properties deleted file mode 100644 index ad043be8..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -spring.application.name=service-a-2023 -server.port=65203 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.username=nacos -spring.cloud.nacos.password=UKt([$gc \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/src/main/resources/application.yaml new file mode 100644 index 00000000..0c6a759f --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-a/src/main/resources/application.yaml @@ -0,0 +1,30 @@ +server: + port: 65203 + +spring: + application: + name: service-a-2023 + cloud: + nacos: + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: true + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:service-a-2023.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/deployment.yaml index 4c56d293..ab008070 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/deployment.yaml @@ -17,13 +17,13 @@ spec: app: service-b-2023 annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.6.1" + 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\"}" + polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-quickstart-2023-consumer:1.6.1-java17 + - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-b:1.7.0-RC.0-java17 imagePullPolicy: Always name: consumer resources: @@ -35,7 +35,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dserver.port=65202 -jar main.jar + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/pom.xml index 1e5452d2..0df49bcd 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/pom.xml @@ -47,6 +47,10 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + org.springframework.cloud diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/src/main/resources/application.properties deleted file mode 100644 index 1471400f..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -spring.application.name=service-b-2023 -server.port=65202 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.username=nacos -spring.cloud.nacos.password=UKt([$gc \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/src/main/resources/application.yaml new file mode 100644 index 00000000..d05a01b0 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-b/src/main/resources/application.yaml @@ -0,0 +1,30 @@ +server: + port: 65202 + +spring: + application: + name: service-b-2023 + cloud: + nacos: + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: true + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:service-b-2023.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/deployment.yaml index 60877dcf..d4a3dd56 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/deployment.yaml @@ -17,13 +17,13 @@ spec: app: service-c-2023 annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.6.1" + 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\"}" + polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-quickstart-2023-consumer:1.6.1-java17 + - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-c:1.7.0-RC.0-java17 imagePullPolicy: Always name: consumer resources: @@ -35,7 +35,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dserver.port=65201 -jar main.jar + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/pom.xml index b8df9e26..2914e4d7 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/pom.xml @@ -47,6 +47,10 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + org.springframework.cloud diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/src/main/resources/application.properties deleted file mode 100644 index 78271746..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -spring.application.name=service-c-2023 -server.port=65201 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.username=nacos -spring.cloud.nacos.password=UKt([$gc diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/src/main/resources/application.yaml new file mode 100644 index 00000000..75708285 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-c/src/main/resources/application.yaml @@ -0,0 +1,30 @@ +server: + port: 65201 + +spring: + application: + name: service-c-2023 + cloud: + nacos: + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: true + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:service-c-2023.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/deployment.yaml index 9cd40c13..a9062581 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/deployment.yaml @@ -3,27 +3,27 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - app: service-gateway-2023 - name: service-gateway-2023 + app: sc-gateway-2023 + name: sc-gateway-2023 namespace: default spec: replicas: 1 selector: matchLabels: - app: service-gateway-2023 + app: sc-gateway-2023 template: metadata: labels: - app: service-gateway-2023 + app: sc-gateway-2023 annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.6.1" + 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\"}" + polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-quickstart-2023-consumer:1.6.1-java17 + - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-gateway:1.7.0-RC.0-java17 imagePullPolicy: Always name: consumer resources: @@ -35,7 +35,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dserver.port=65200 -jar main.jar + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/pom.xml index c8457503..168a8e41 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/pom.xml @@ -41,6 +41,10 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + org.springframework.cloud spring-cloud-starter-loadbalancer diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/src/main/resources/application.properties deleted file mode 100644 index 802fd6d0..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -server.port=65200 -spring.application.name=service-gateway-2023 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.discovery.enabled=true -spring.cloud.gateway.discovery.locator.enabled=true diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml new file mode 100644 index 00000000..e91d90ae --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml @@ -0,0 +1,36 @@ +server: + port: 65200 + +spring: + application: + name: sc-gateway-2023 + cloud: + gateway: + discovery: + locator: + enabled: true + nacos: + username: 'nacos' + password: 'nacos' + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: true + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:sc-gateway-2023.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/deployment.yaml index 6387ede7..f63d0a19 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/deployment.yaml @@ -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\"}" + polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}" spec: containers: - image: polarismesh/polaris-javaagent-demo-sc-quickstart-service-consumer-2023:1.7.0-RC.0-java17 @@ -35,7 +35,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dserver.port=65101 -jar main.jar + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/pom.xml index 24be57cc..08bb97ab 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/pom.xml @@ -47,6 +47,10 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + org.springframework.cloud diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java index 312962ae..50057e02 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java @@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -47,6 +48,7 @@ public RestTemplate restTemplate() { } @RestController + @RefreshScope public static class EchoController { private Registration registration; diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/resources/application.properties deleted file mode 100644 index a8ee6ba6..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -server.port=65101 -spring.application.name=service-consumer-2023 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.username=nacos -spring.cloud.nacos.password=UKt([$gc diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/resources/application.yaml new file mode 100644 index 00000000..f9e0d222 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/src/main/resources/application.yaml @@ -0,0 +1,30 @@ +server: + port: 65101 + +spring: + application: + name: service-consumer-2023 + cloud: + nacos: + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: true + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:service-consumer-2023.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/deployment.yaml index a7a2cfd3..bf18dc10 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/deployment.yaml @@ -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\"}" + polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}" spec: containers: - image: polarismesh/polaris-javaagent-demo-sc-quickstart-2023-scg:1.7.0-RC.0-java17 @@ -35,7 +35,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dserver.port=65101 -jar main.jar + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/pom.xml index a14e539f..7077475a 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/pom.xml @@ -43,6 +43,10 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + org.springframework.cloud diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/src/main/resources/application.properties deleted file mode 100644 index 496ea4af..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -server.port=65102 -spring.application.name=service-gateway-2023 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.discovery.enabled=true -spring.cloud.gateway.discovery.locator.enabled=true diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/src/main/resources/application.yaml new file mode 100644 index 00000000..3c792f59 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/src/main/resources/application.yaml @@ -0,0 +1,36 @@ +server: + port: 65102 + +spring: + application: + name: service-gateway-2023 + cloud: + gateway: + discovery: + locator: + enabled: true + nacos: + username: 'nacos' + password: 'nacos' + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: true + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:service-gateway-2023.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/deployment.yaml index 211c9364..042215fd 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/deployment.yaml @@ -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\"}" + polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.namespace\": \"default\", \"spring.cloud.polaris.router.nearby-router.matchLevel\": \"campus\"}" spec: containers: - image: polarismesh/polaris-javaagent-demo-sc-quickstart-service-provider-2023:1.7.0-RC.0-java17 @@ -35,7 +35,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dserver.port=65100 -jar main.jar + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/pom.xml index baa40384..12b87e59 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/pom.xml @@ -47,6 +47,10 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java index f966a634..2ea649a4 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java @@ -23,6 +23,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -41,6 +42,7 @@ public static void main(String[] args) { @RestController + @RefreshScope public static class EchoController { private Registration registration; diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/resources/application.properties deleted file mode 100644 index d66ea37d..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/resources/application.properties +++ /dev/null @@ -1,7 +0,0 @@ -server.port=65100 -spring.application.name=service-provider-2023 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.discovery.enabled=true -spring.cloud.nacos.username=nacos -spring.cloud.nacos.password=UKt([$gc - diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/resources/application.yaml new file mode 100644 index 00000000..88ba3011 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/src/main/resources/application.yaml @@ -0,0 +1,32 @@ +server: + port: 65100 + +spring: + application: + name: service-provider-2023 + cloud: + nacos: + username: 'nacos' + password: 'nacos' + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: true + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:service-provider-2023.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml index cc5ab22b..e96f4b72 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml @@ -77,22 +77,6 @@ polaris-agent-core-common ${project.version} - - - - - - - - - - - - - - - - org.slf4j slf4j-api diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java index 3363d186..b8a08dbe 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java @@ -30,11 +30,7 @@ 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.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 cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationInjectInterceptor; import org.springframework.context.ApplicationContext; @@ -56,78 +52,12 @@ public void init(PluginContext context) { */ private void addPolarisTransformers(TransformOperations operations) { - operations.transform(ClassNames.SERVICE_REGISTRATION, SpringCloudRegistryTransform.class); - operations.transform(ClassNames.DISCOVERY_CLIENT, SpringCloudDiscoveryTransform.class); - operations.transform(ClassNames.REACTIVE_DISCOVERY_CLIENT, SpringCloudReactiveDiscoveryTransform.class); - - // 北极星路由执行 - // operations.transform(ClassNames.ROUTER, SpringCloudTrafficRouterTransform.class); - - // 流量标签信息收集 -// operations.transform(ClassNames.REACTIVE_WEB_FILTER, ReactiveWebFilterTransform.class); -// operations.transform(ClassNames.SERVLET_WEB_FILTER, ServletWebFilterTransform.class); - - // 请求发起时需要收集流量标签信息 -// operations.transform(ClassNames.REST_TEMPLATE, RestTemplateTransform.class); -// operations.transform(ClassNames.FEIGN_TEMPLATE, FeignTransform.class); - // 在 agent 中注入 Spring 的 ApplicationContext operations.transform(ClassNames.APPLICATION_CONTEXT_AWARE, ApplicationContextAwareTransform.class); // EnvironmentPostProcessor 处理 - operations.transform(ClassNames.ENVIRONMENT_POST_PROCESSOR, DisableSpringCloudAlibabaTransform.class); - - operations.transform(ClassNames.BLOCKING_LOADBALANCER_CLIENT, BlockingLoadbalancerClientTransform.class); - - } - - /** - * SpringCloud 注册拦截 - */ - public static class SpringCloudRegistryTransform implements TransformCallback { - - @Override - public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, - Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { - - InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); - InstrumentMethod registerMethod = target.getDeclaredMethod("start"); if (registerMethod != null) { - registerMethod.addInterceptor(RegistryInterceptor.class); - } return target.toBytecode(); - } - } - - /** - * Spring Cloud 服务发现拦截 - */ - public static class SpringCloudDiscoveryTransform implements TransformCallback { - - @Override - public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, - Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { - - InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); - InstrumentMethod constructMethod = target.getConstructor("java.util.List"); if (constructMethod != null) { - constructMethod.addInterceptor(DiscoveryInterceptor.class); - } + operations.transform(ClassNames.ENVIRONMENT_POST_PROCESSOR, ConfigurationInjectTransform.class); - return target.toBytecode(); - } - } - - public static class SpringCloudReactiveDiscoveryTransform implements TransformCallback { - - @Override - public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, - Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { - - InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); - InstrumentMethod constructMethod = target.getConstructor("java.util.List"); if (constructMethod != null) { - constructMethod.addInterceptor(ReactiveDiscoveryInterceptor.class); - } - - return target.toBytecode(); - } } /** @@ -149,7 +79,7 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, } } - public static class DisableSpringCloudAlibabaTransform implements TransformCallback { + public static class ConfigurationInjectTransform implements TransformCallback { @Override public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, @@ -157,23 +87,7 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); InstrumentMethod constructMethod = target.getDeclaredMethod("onApplicationEnvironmentPreparedEvent", "org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent"); if (constructMethod != null) { - constructMethod.addInterceptor(DisableSpringCloudAlibabaInterceptor.class); - } - - return target.toBytecode(); - } - } - - public static class BlockingLoadbalancerClientTransform implements TransformCallback { - - @Override - public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, - Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { - InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); - InstrumentMethod constructMethod = target.getDeclaredMethod("reconstructURI", "org.springframework.cloud" - + ".client.ServiceInstance", "java.net.URI"); - if (constructMethod != null) { - constructMethod.addInterceptor(BlockingLoadBalancerClientInterceptor.class); + constructMethod.addInterceptor(ConfigurationInjectInterceptor.class); } return target.toBytecode(); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java index 46aa74b5..0af70db1 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java @@ -36,6 +36,7 @@ import com.tencent.cloud.polaris.PolarisDiscoveryConfigModifier; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.config.ConfigurationModifier; +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.cloud.polaris.context.ModifyAddress; @@ -114,6 +115,8 @@ public class Holder { private static Environment environment; + private static PolarisPropertySourceManager polarisPropertySourceManager; + private static void initProperties() { polarisContextProperties = new PolarisContextProperties(); discoveryProperties = new PolarisDiscoveryProperties(); @@ -129,6 +132,7 @@ private static void initProperties() { rpcEnhancementReporterProperties = new RpcEnhancementReporterProperties(); polarisCryptoConfigProperties = new PolarisCryptoConfigProperties(); discoveryProperties.setService(Holder.getLocalService()); + polarisPropertySourceManager = new PolarisPropertySourceManager(); } public static void init() { @@ -349,4 +353,8 @@ public static boolean isAllowDiscovery() { public static LosslessProperties getLosslessProperties() { return losslessProperties; } + + public static PolarisPropertySourceManager getPolarisPropertySourceManager() { + return polarisPropertySourceManager; + } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java index b2ace481..68c5e904 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java @@ -22,7 +22,6 @@ 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; @@ -33,8 +32,6 @@ 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; @@ -106,19 +103,6 @@ public PolarisConfigFileLocator get() { } }).getBeanDefinition()); }); -// registerBean(applicationContext, "affectedConfigurationPropertiesRebinder", (ctx, name) -> { -// ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; -// DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); -// beanFactory.registerBeanDefinition(name, -// BeanDefinitionBuilder.genericBeanDefinition(ConfigurationPropertiesRebinder.class, new Supplier() { -// @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(); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/BaseBeanHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/BaseBeanHandler.java index 726ac312..13e4adcd 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/BaseBeanHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/BaseBeanHandler.java @@ -20,12 +20,24 @@ 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; diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java new file mode 100644 index 00000000..4f4e30e0 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java @@ -0,0 +1,122 @@ +package cn.polarismesh.agent.plugin.spring.cloud.discovery; + +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.plugin.lossless.config.LosslessAutoConfiguration; +import com.tencent.cloud.polaris.PolarisDiscoveryProperties; +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; +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.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; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; + +public class DiscoveryHandler extends AbstractContextHandler { + + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + String discoveryEnable = applicationContext.getEnvironment().getProperty("spring.cloud.discovery.enabled"); + if (null == discoveryEnable || Boolean.parseBoolean(discoveryEnable)) { + registerBean(applicationContext, "polarisDiscoveryProperties", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, + BeanDefinitionBuilder.genericBeanDefinition(PolarisDiscoveryProperties.class, new Supplier() { + @Override + public PolarisDiscoveryProperties get() { + return Holder.getDiscoveryProperties(); + } + }).getBeanDefinition()); + }); + registerBean(applicationContext, "polarisDiscoveryHandler", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, + BeanDefinitionBuilder.genericBeanDefinition(PolarisDiscoveryHandler.class, new Supplier() { + @Override + public PolarisDiscoveryHandler get() { + PolarisDiscoveryProperties polarisDiscoveryProperties = (PolarisDiscoveryProperties) ctx.getBean("polarisDiscoveryProperties"); + PolarisSDKContextManager polarisSDKContextManager = (PolarisSDKContextManager) ctx.getBean("polarisSDKContextManager"); + return new PolarisDiscoveryHandler(polarisDiscoveryProperties, polarisSDKContextManager); + } + }).getBeanDefinition()); + }); + registerBean(applicationContext, "polarisStatProperties", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, + BeanDefinitionBuilder.genericBeanDefinition(PolarisStatProperties.class, new Supplier() { + @Override + public PolarisStatProperties get() { + return Holder.getPolarisStatProperties(); + } + }).getBeanDefinition()); + }); + registerBean(applicationContext, "polarisDiscoveryAutoConfiguration", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( + PolarisDiscoveryAutoConfiguration.class).getBeanDefinition()); + }); + registerBean(applicationContext, "sdkContext", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, + BeanDefinitionBuilder.genericBeanDefinition(SDKContext.class, new Supplier() { + @Override + public SDKContext get() { + return Holder.getContextManager().getSDKContext(); + } + }).getBeanDefinition()); + }); + registerBean(applicationContext, "assemblyAPI", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, + BeanDefinitionBuilder.genericBeanDefinition(AssemblyAPI.class, new Supplier() { + @Override + public AssemblyAPI get() { + return Holder.getContextManager().getAssemblyAPI(); + } + }).getBeanDefinition()); + }); + registerBean(applicationContext, "polarisRegistration", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( + PolarisRegistration.class).setPrimary(true).getBeanDefinition()); + }); + registerBean(applicationContext, "polarisServiceRegistry", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( + PolarisServiceRegistry.class).setPrimary(true).getBeanDefinition()); + }); + registerBean(applicationContext, "polarisAutoServiceRegistration", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( + PolarisAutoServiceRegistration.class).setPrimary(true).getBeanDefinition()); + }); + registerBean(applicationContext, "losslessAutoConfiguration", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( + LosslessAutoConfiguration.class).setPrimary(true).getBeanDefinition()); + }); + + } + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryInterceptor.java deleted file mode 100644 index 1d331770..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryInterceptor.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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.discovery; - -import cn.polarismesh.agent.core.common.utils.ReflectionUtils; -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; -import org.springframework.cloud.client.ServiceInstance; -import org.springframework.cloud.client.discovery.DiscoveryClient; - -import java.util.ArrayList; -import java.util.List; - -/** - * Spring Cloud Nacos/Eureka/Consul 服务发现拦截器 - * - * @author zhuyuhan - */ -public class DiscoveryInterceptor extends BaseInterceptor { - - @Override - public void onAfter(Object target, Object[] args, Object result, Throwable throwable) { - if (!Holder.isAllowDiscovery()) { - return; - } - - PolarisServiceDiscovery discovery = new PolarisServiceDiscovery(Holder.getNacosContextProperties(), Holder.getDiscoveryProperties(), - DiscoveryUtils.buildDiscoveryHandler()); - - ReflectionUtils.doWithFields(target.getClass(), field -> { - ReflectionUtils.makeAccessible(field); - List discoveryClients = (List) ReflectionUtils.getField(field, target); - List wraps = new ArrayList<>(); - discoveryClients.forEach(discoveryClient -> wraps.add(new ProxyDiscoveryClient(discovery))); - ReflectionUtils.setField(field, target, wraps); - }, field -> StringUtils.equals(field.getName(), "discoveryClients")); - } - - private static class ProxyDiscoveryClient implements DiscoveryClient { - - public final String description = "Spring Cloud Tencent Polaris Discovery Client."; - - private final PolarisServiceDiscovery discovery; - - private ProxyDiscoveryClient(PolarisServiceDiscovery discovery) { - this.discovery = discovery; - } - - @Override - public String description() { - return description; - } - - @Override - public List getInstances(String serviceId) { - return discovery.getInstances(serviceId); - } - - @Override - public List getServices() { - return discovery.getServices(); - } - } - -} - diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryUtils.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryUtils.java deleted file mode 100644 index c006444d..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryUtils.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.discovery; - -import java.util.Objects; - -import cn.polarismesh.agent.plugin.spring.cloud.common.Holder; -import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; - -/** - * @author liaochuntao - */ -public class DiscoveryUtils { - - private static final String NACOS_DEFAULT_GROUP = "DEFAULT_GROUP"; - - public static PolarisDiscoveryHandler buildDiscoveryHandler() { - return new PolarisDiscoveryHandler( - Holder.getDiscoveryProperties(), Holder.getContextManager() - ); - } - - public static String rewriteServiceId(String serviceId) { - boolean enableNacosRegister = Holder.getNacosContextProperties().isRegisterEnabled(); - boolean enableNacosDiscovery = Holder.getNacosContextProperties().isDiscoveryEnabled(); - boolean enablePolaris = Holder.getDiscoveryProperties().isEnabled(); - String nacosGroupName = Holder.getNacosContextProperties().getGroup(); - if (Holder.getNacosContextProperties().isEnabled()) { - boolean rewrite = enableNacosRegister && enablePolaris; - if (enableNacosDiscovery || enablePolaris) { - rewrite = true; - } - if (rewrite && !Objects.equals(nacosGroupName, NACOS_DEFAULT_GROUP) ) { - serviceId = nacosGroupName + "__" + serviceId; - } - } - return serviceId; - } - - -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/PolarisServiceDiscovery.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/PolarisServiceDiscovery.java deleted file mode 100644 index 352e3e3e..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/PolarisServiceDiscovery.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.discovery; - -import com.tencent.cloud.common.pojo.PolarisServiceInstance; -import com.tencent.cloud.polaris.PolarisDiscoveryProperties; -import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; -import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; -import com.tencent.polaris.api.exception.PolarisException; -import com.tencent.polaris.api.pojo.Instance; -import com.tencent.polaris.api.pojo.ServiceInfo; -import com.tencent.polaris.api.pojo.ServiceInstances; -import com.tencent.polaris.api.rpc.InstancesResponse; -import org.springframework.cloud.client.ServiceInstance; -import org.springframework.util.CollectionUtils; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -public class PolarisServiceDiscovery { - - private final NacosContextProperties nacosContextProperties; - - private final PolarisDiscoveryProperties polarisDiscoveryProperties; - - private final PolarisDiscoveryHandler polarisDiscoveryHandler; - - public PolarisServiceDiscovery( - NacosContextProperties nacosContextProperties, - PolarisDiscoveryProperties polarisDiscoveryProperties, - PolarisDiscoveryHandler polarisDiscoveryHandler) { - this.nacosContextProperties = nacosContextProperties; - this.polarisDiscoveryProperties = polarisDiscoveryProperties; - this.polarisDiscoveryHandler = polarisDiscoveryHandler; - } - - /** - * Return all instances for the given service. - * - * @param serviceId id of service - * @return list of instances - * @throws PolarisException polarisException - */ - public List getInstances(String serviceId) throws PolarisException { - serviceId = DiscoveryUtils.rewriteServiceId(serviceId); - List instances = new ArrayList<>(); - InstancesResponse filteredInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId); - ServiceInstances serviceInstances = filteredInstances.toServiceInstances(); - for (Instance instance : serviceInstances.getInstances()) { - instances.add(new PolarisServiceInstance(instance)); - } - return instances; - } - - /** - * Return the names of all services. - * - * @return list of service names - * @throws PolarisException polarisException - */ - public List getServices() throws PolarisException { - if (CollectionUtils.isEmpty(polarisDiscoveryHandler.getServices().getServices())) { - return Collections.emptyList(); - } - return polarisDiscoveryHandler.getServices().getServices().stream() - .map(ServiceInfo::getService).collect(Collectors.toList()); - } - -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/ReactiveDiscoveryInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/ReactiveDiscoveryInterceptor.java deleted file mode 100644 index 74bd413a..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/ReactiveDiscoveryInterceptor.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.discovery; - -import cn.polarismesh.agent.core.common.utils.ReflectionUtils; -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; -import org.springframework.cloud.client.ServiceInstance; -import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; -import reactor.core.publisher.Flux; - -import java.util.ArrayList; -import java.util.List; - -/** - * Spring Cloud Nacos/Eureka/Consul 服务发现拦截器 - * - * @author zhuyuhan - */ -public class ReactiveDiscoveryInterceptor extends BaseInterceptor { - - @Override - public void onAfter(Object target, Object[] args, Object result, Throwable throwable) { - if (!Holder.isAllowDiscovery()) { - return; - } - - PolarisServiceDiscovery discovery = new PolarisServiceDiscovery(Holder.getNacosContextProperties(), - Holder.getDiscoveryProperties(), DiscoveryUtils.buildDiscoveryHandler()); - - ReflectionUtils.doWithFields(target.getClass(), field -> { - ReflectionUtils.makeAccessible(field); - List discoveryClients = (List) ReflectionUtils.getField(field, target); - List wraps = new ArrayList<>(); - discoveryClients.forEach(discoveryClient -> wraps.add(new ProxyDiscoveryClient(discovery))); - ReflectionUtils.setField(field, target, wraps); - }, field -> StringUtils.equals(field.getName(), "discoveryClients")); - } - - private static class ProxyDiscoveryClient implements ReactiveDiscoveryClient { - - public final String description = "Spring Cloud Tencent Polaris Discovery Client."; - - private final PolarisServiceDiscovery discovery; - - private ProxyDiscoveryClient(PolarisServiceDiscovery discovery) { - this.discovery = discovery; - } - - @Override - public String description() { - return description; - } - - @Override - public Flux getInstances(String serviceId) { - return Flux.fromIterable(discovery.getInstances(serviceId)); - } - - @Override - public Flux getServices() { - return Flux.fromIterable(discovery.getServices()); - } - } - -} - diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/registry/LosslessProxyServiceRegistry.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/registry/LosslessProxyServiceRegistry.java deleted file mode 100644 index c466e9d4..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/registry/LosslessProxyServiceRegistry.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.discovery.registry; - -import cn.polarismesh.agent.plugin.spring.cloud.common.Holder; -import com.tencent.cloud.common.util.ApplicationContextAwareUtils; -import com.tencent.cloud.plugin.lossless.SpringCloudLosslessActionProvider; -import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer; -import com.tencent.polaris.api.pojo.BaseInstance; - -import org.springframework.cloud.client.serviceregistry.Registration; -import org.springframework.cloud.client.serviceregistry.ServiceRegistry; - -/** - * Lossless proxy for {@link ServiceRegistry}. - * - * @author Shedfree Wu - */ -public class LosslessProxyServiceRegistry implements ServiceRegistry { - - private final ServiceRegistry target; - - public LosslessProxyServiceRegistry(ServiceRegistry target) { - this.target = target; - } - - @Override - public void register(Registration registration) { - Runnable registerAction = () -> target.register(registration); - SpringCloudLosslessActionProvider losslessActionProvider = new SpringCloudLosslessActionProvider( - target, registration, Holder.getLosslessProperties(), registerAction); - - RegistrationTransformer registrationTransformer = ApplicationContextAwareUtils.getApplicationContext() - .getBean(RegistrationTransformer.class); - BaseInstance baseInstance = SpringCloudLosslessActionProvider.getBaseInstance( - registration, registrationTransformer); - Holder.getContextManager().getLosslessAPI().setLosslessActionProvider(baseInstance, losslessActionProvider); - // replace by lossless register - Holder.getContextManager().getLosslessAPI().losslessRegister(baseInstance); - } - - @Override - public void deregister(Registration registration) { - target.deregister(registration); - } - - @Override - public void close() { - target.close(); - } - - @Override - public void setStatus(Registration registration, String status) { - target.setStatus(registration, status); - } - - @Override - public T getStatus(Registration registration) { - return target.getStatus(registration); - } -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/registry/RegistryInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/registry/RegistryInterceptor.java deleted file mode 100644 index 0c9ef170..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/registry/RegistryInterceptor.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * 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.discovery.registry; - - -import java.util.Collections; - -import cn.polarismesh.agent.core.common.utils.ReflectionUtils; -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; -import com.tencent.cloud.polaris.registry.PolarisRegistration; -import com.tencent.cloud.polaris.registry.PolarisServiceRegistry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext; -import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; -import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; -import org.springframework.cloud.client.serviceregistry.Registration; -import org.springframework.cloud.client.serviceregistry.ServiceRegistry; - -/** - * hack {@link AbstractAutoServiceRegistration#start()} - *

- * Polaris Ribbon Server 实现类 - */ -public class RegistryInterceptor extends BaseInterceptor { - - private static final Logger LOGGER = LoggerFactory.getLogger(RegistryInterceptor.class); - - @Override - public void onBefore(Object target, Object[] args) { - if (!Holder.isAllowDiscovery()) { - return; - } - - LOGGER.debug("[PolarisAgent] replace ServiceRegistry to ProxyServiceRegistry, target : {}", target); - ServiceRegistry registry; - LosslessProxyServiceRegistry losslessProxyServiceRegistry; - - String clsName = target.getClass().getCanonicalName(); - - if (clsName.contains("org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration")) { - registry = (ServiceRegistry) ReflectionUtils.getObjectByFieldName(target, "serviceRegistry"); - losslessProxyServiceRegistry = new LosslessProxyServiceRegistry(new ProxyServiceRegistry(registry)); - ReflectionUtils.setValueByFieldName(target, "serviceRegistry", losslessProxyServiceRegistry); - } - else { - registry = (ServiceRegistry) ReflectionUtils.getSuperObjectByFieldName(target, "serviceRegistry"); - losslessProxyServiceRegistry = new LosslessProxyServiceRegistry(new ProxyServiceRegistry(registry)); - ReflectionUtils.setSuperValueByFieldName(target, "serviceRegistry", losslessProxyServiceRegistry); - } - LOGGER.debug("[PolarisAgent] finished replace ServiceRegistry to ProxyServiceRegistry"); - } - - public static class ProxyServiceRegistry implements ServiceRegistry { - - private final ServiceRegistry target; - - private final PolarisServiceRegistry polarisRegistry; - - public ProxyServiceRegistry(ServiceRegistry target) { - this.target = target; - this.polarisRegistry = new PolarisServiceRegistry(Holder.getDiscoveryProperties(), Holder.getContextManager(), - DiscoveryUtils.buildDiscoveryHandler(), Holder.getStaticMetadataManager(), Holder.getPolarisStatProperties()); - } - - private PolarisRegistration buildPolarisRegistration() { - return new PolarisRegistration( - Holder.getDiscoveryProperties(), - Holder.getPolarisContextProperties(), - Holder.getConsulContextProperties(), - Holder.getContextManager().getSDKContext(), - Holder.getStaticMetadataManager(), - Holder.getNacosContextProperties(), - getBeanIfExist(ServletWebServerApplicationContext.class), - getBeanIfExist(ReactiveWebServerApplicationContext.class), - Collections.emptyList() - ); - } - - @Override - public void register(Registration registration) { - LOGGER.info("[PolarisAgent] begin do register to polaris action."); - Holder.getPolarisContextProperties().setLocalPort(registration.getPort()); - PolarisRegistration polarisRegistration = buildPolarisRegistration(); - polarisRegistry.register(polarisRegistration); - } - - @Override - public void deregister(Registration registration) { - LOGGER.info("[PolarisAgent] begin de deregister from polaris action."); - Holder.getPolarisContextProperties().setLocalPort(registration.getPort()); - PolarisRegistration polarisRegistration = buildPolarisRegistration(); - polarisRegistry.deregister(polarisRegistration); - } - - @Override - public void close() { - target.close(); - polarisRegistry.close(); - } - - @Override - public void setStatus(Registration registration, String status) { - PolarisRegistration polarisRegistration = buildPolarisRegistration(); - polarisRegistry.setStatus(polarisRegistration, status); - } - - @Override - public T getStatus(Registration registration) { - PolarisRegistration polarisRegistration = buildPolarisRegistration(); - return (T) polarisRegistry.getStatus(polarisRegistration); - } - - private T getBeanIfExist(Class cls) { - try { - return ApplicationContextAwareUtils.getApplicationContext().getBean(cls); - } - catch (Exception e) { - LOGGER.error(e.getMessage()); - return null; - } - } - - } -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java index 6a3b5c92..bfcc6aee 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java @@ -20,9 +20,12 @@ import java.util.Arrays; import java.util.List; +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.discovery.DiscoveryHandler; import cn.polarismesh.agent.plugin.spring.cloud.metadata.MetadataHandler; import cn.polarismesh.agent.plugin.spring.cloud.router.RouterHandler; import cn.polarismesh.agent.plugin.spring.cloud.rpc.RpcEnhancementHandler; @@ -37,9 +40,17 @@ public class ApplicationContextAwareInterceptor extends BaseInterceptor { + private static final CommonLogger logger = StdoutCommonLoggerFactory.INSTANCE + .getLogger(ApplicationContextAwareInterceptor.class.getCanonicalName()); + @Override public void onAfter(Object target, Object[] args, Object result, Throwable throwable) { ConfigurableApplicationContext context = (ConfigurableApplicationContext) args[0]; + String enable = context.getEnvironment().getProperty("spring.cloud.polaris.enabled"); + if (null != enable && !Boolean.parseBoolean(enable)) { + logger.warn("polaris is disabled, no polaris inject actions will be taken"); + return; + } ApplicationContextAwareUtils utils = new ApplicationContextAwareUtils(); // MetadataContext 需要读取到 agent 配置的内容 @@ -59,7 +70,7 @@ public void onAfter(Object target, Object[] args, Object result, Throwable throw private List buildAwares() { return Arrays.asList( new BaseBeanHandler(), - new ConfigHandler(), + new DiscoveryHandler(), new RpcEnhancementHandler(), new MetadataHandler(), new RouterHandler() diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/BlockingLoadBalancerClientInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/BlockingLoadBalancerClientInterceptor.java deleted file mode 100644 index f889039d..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/BlockingLoadBalancerClientInterceptor.java +++ /dev/null @@ -1,23 +0,0 @@ -package cn.polarismesh.agent.plugin.spring.cloud.interceptor; - -import cn.polarismesh.agent.plugin.spring.cloud.interceptor.BaseInterceptor; -import com.tencent.cloud.common.metadata.MetadataContextHolder; - -import org.springframework.cloud.client.ServiceInstance; - -/** - * @author liaochuntao - */ -public class BlockingLoadBalancerClientInterceptor extends BaseInterceptor { - - @Override - public void onBefore(Object target, Object[] args) { - Object server = args[0]; - if (server instanceof ServiceInstance) { - ServiceInstance instance = (ServiceInstance) server; - MetadataContextHolder.get().setLoadbalancer("host", instance.getHost()); - MetadataContextHolder.get().setLoadbalancer("port", String.valueOf(instance.getPort())); - } - } - -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/DisableSpringCloudAlibabaInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java similarity index 71% rename from polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/DisableSpringCloudAlibabaInterceptor.java rename to polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java index ddebb38e..0d614de6 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/DisableSpringCloudAlibabaInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java @@ -17,6 +17,10 @@ package cn.polarismesh.agent.plugin.spring.cloud.interceptor; +import cn.polarismesh.agent.plugin.spring.cloud.common.Holder; +import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator; +import com.tencent.polaris.configuration.api.core.ConfigFileService; +import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; @@ -34,9 +38,9 @@ * * @author liaochuntao */ -public class DisableSpringCloudAlibabaInterceptor extends BaseInterceptor { +public class ConfigurationInjectInterceptor extends BaseInterceptor { - private static final Logger LOGGER = LoggerFactory.getLogger(DisableSpringCloudAlibabaInterceptor.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationInjectInterceptor.class); @Override public void onBefore(Object target, Object[] args) { @@ -59,17 +63,19 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp } private void disableSpringCloudAlibabaAbility(ConfigurableEnvironment environment) { - - String disableSCA = "__disable__sca__"; - +// String disableSCA = "__disable__sca__"; +// +// Properties properties = new Properties(); +// properties.setProperty("spring.cloud.sentinel.enabled", "false"); +// properties.setProperty("spring.cloud.nacos.discovery.watch.enabled", "false"); +// properties.setProperty("spring.cloud.loadbalancer.cache.enabled", "false"); +// properties.setProperty("spring.cloud.nacos.config.enabled", "false"); + + String disableCheck = "__disable__check__"; Properties properties = new Properties(); - properties.setProperty("spring.cloud.sentinel.enabled", "false"); - properties.setProperty("spring.cloud.nacos.discovery.watch.enabled", "false"); - properties.setProperty("spring.cloud.loadbalancer.cache.enabled", "false"); - properties.setProperty("spring.cloud.nacos.config.enabled", "false"); - + properties.setProperty("spring.cloud.polaris.config.import-check.enabled", "false"); // 设置 spring.cloud.sentinel.enabled 为 false - environment.getPropertySources().addFirst(new PropertiesPropertySource(disableSCA, properties)); + environment.getPropertySources().addFirst(new PropertiesPropertySource(disableCheck, properties)); LOGGER.info("[PolarisAgent] disable spring cloud alibaba all ability"); } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/EnvironmentChangeEventListener.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/EnvironmentChangeEventListener.java new file mode 100644 index 00000000..e6000579 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/EnvironmentChangeEventListener.java @@ -0,0 +1,48 @@ +package cn.polarismesh.agent.plugin.spring.cloud.interceptor; + +import cn.polarismesh.agent.plugin.spring.cloud.config.ConfigHandler; +import cn.polarismesh.agent.plugin.spring.cloud.metadata.MetadataHandler; +import cn.polarismesh.agent.plugin.spring.cloud.router.RouterHandler; +import cn.polarismesh.agent.plugin.spring.cloud.rpc.RpcEnhancementHandler; + +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.SmartApplicationListener; + +public class EnvironmentChangeEventListener implements SmartApplicationListener { + + private final RouterHandler routerHandler = new RouterHandler(); + + private final MetadataHandler metadataHandler = new MetadataHandler(); + + private final RpcEnhancementHandler rpcEnhancementHandler = new RpcEnhancementHandler(); + + private final ConfigHandler configHandler = new ConfigHandler(); + + @Override + public boolean supportsEventType(Class eventType) { + return eventType == EnvironmentChangeEvent.class || eventType == ContextRefreshedEvent.class; + } + + @Override + public void onApplicationEvent(ApplicationEvent event) { + Object source = event.getSource(); + if (!(source instanceof ApplicationContext)) { + return; + } + ApplicationContext applicationContext = (ApplicationContext) source; + ConfigurableApplicationContext cfgCtx1 = (ConfigurableApplicationContext) applicationContext; + String enable = cfgCtx1.getEnvironment().getProperty("spring.cloud.polaris.enabled"); + System.out.println("enable is " + enable); + if (null == enable || Boolean.parseBoolean(enable)) { + configHandler.setApplicationContext(applicationContext); + routerHandler.setApplicationContext(applicationContext); + metadataHandler.setApplicationContext(applicationContext); + rpcEnhancementHandler.setApplicationContext(applicationContext); + } + } + +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/router/RouterHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/router/RouterHandler.java index 8203b7d1..a9c53a65 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/router/RouterHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/router/RouterHandler.java @@ -17,21 +17,29 @@ package cn.polarismesh.agent.plugin.spring.cloud.router; +import java.util.ArrayList; +import java.util.Map; import java.util.function.Supplier; 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 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; import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties; +import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; +import com.tencent.cloud.polaris.router.spi.RouterResponseInterceptor; +import com.tencent.cloud.rpc.enhancement.transformer.InstanceTransformer; import org.springframework.beans.BeansException; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @@ -89,13 +97,63 @@ public PolarisRuleBasedRouterProperties get() { } if (null != ClassUtils.getClazz("org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier", Thread.currentThread().getContextClassLoader())) { - registerBean(applicationContext, "loadBalancerConfiguration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(LoadBalancerConfiguration.class) - .getBeanDefinition()); + if (null != ClassUtils.getClazz("org.springframework.web.reactive.function.client.WebClient", Thread.currentThread() + .getContextClassLoader())) { + String reactiveEnableStr = applicationContext.getEnvironment() + .getProperty("spring.cloud.discovery.reactive.enabled"); + if (null == reactiveEnableStr || Boolean.parseBoolean(reactiveEnableStr)) { + registerBean(applicationContext, "polarisRouterDiscoveryClientServiceInstanceListSupplier", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition(ServiceInstanceListSupplier.class, new Supplier() { + @Override + public ServiceInstanceListSupplier get() { + PolarisSDKContextManager polarisSDKContextManager = (PolarisSDKContextManager) applicationContext.getBean("polarisSDKContextManager"); + Map requestInterceptors = applicationContext.getBeansOfType(RouterRequestInterceptor.class); + Map responseInterceptors = applicationContext.getBeansOfType(RouterResponseInterceptor.class); + InstanceTransformer instanceTransformer = applicationContext.getBean("instanceTransformer", InstanceTransformer.class); + return new PolarisRouterServiceInstanceListSupplier( + ServiceInstanceListSupplier.builder().withDiscoveryClient() + .build((ConfigurableApplicationContext) applicationContext), + polarisSDKContextManager.getRouterAPI(), + new ArrayList<>(requestInterceptors.values()), + new ArrayList<>(responseInterceptors.values()), + instanceTransformer); + } + }).getBeanDefinition()); + }); + } + } + String blockingEnable = applicationContext.getEnvironment().getProperty("spring.cloud.discovery.blocking.enabled"); + if (null == blockingEnable || Boolean.parseBoolean(blockingEnable)) { + registerBean(applicationContext, "polarisRouterDiscoveryClientServiceInstanceListSupplier", (ctx, name) -> { + ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); + beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition(ServiceInstanceListSupplier.class, new Supplier() { + @Override + public ServiceInstanceListSupplier get() { + PolarisSDKContextManager polarisSDKContextManager = (PolarisSDKContextManager) applicationContext.getBean("polarisSDKContextManager"); + Map requestInterceptors = applicationContext.getBeansOfType(RouterRequestInterceptor.class); + Map responseInterceptors = applicationContext.getBeansOfType(RouterResponseInterceptor.class); + InstanceTransformer instanceTransformer = applicationContext.getBean("instanceTransformer", InstanceTransformer.class); + return new PolarisRouterServiceInstanceListSupplier( + ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().build((ConfigurableApplicationContext) applicationContext), + polarisSDKContextManager.getRouterAPI(), + new ArrayList<>(requestInterceptors.values()), + new ArrayList<>(responseInterceptors.values()), + instanceTransformer); + } + }).getBeanDefinition()); }); + + } +// registerBean(applicationContext, "loadBalancerConfiguration", (ctx, name) -> { +// ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; +// DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); +// beanFactory.registerBeanDefinition(name, +// BeanDefinitionBuilder.genericBeanDefinition(LoadBalancerConfiguration.class) +// .getBeanDefinition()); +// }); } } diff --git a/pom.xml b/pom.xml index 9718d95f..0a5565ce 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ 1.7.0-RC.0 - 1.15.3 + 1.15.4-SNAPSHOT UTF-8 false UTF-8 From 761549a16ccee92ffd28e0d67cc7a3a942c9199f Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 15 Apr 2024 11:13:41 +0800 Subject: [PATCH 02/24] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/plugin/spring/cloud/MainPlugin.java | 2 +- .../AbstractContextHandler.java | 2 +- .../{context => base}/BaseBeanHandler.java | 14 +----- .../spring/cloud/config/ConfigHandler.java | 2 +- .../cloud/discovery/DiscoveryHandler.java | 2 +- .../ApplicationContextAwareInterceptor.java | 3 +- .../ConfigurationInjectInterceptor.java | 1 - .../EnvironmentChangeEventListener.java | 48 ------------------- .../cloud/metadata/MetadataHandler.java | 2 +- .../spring/cloud/router/RouterHandler.java | 17 +++---- .../cloud/rpc/RpcEnhancementHandler.java | 2 +- 11 files changed, 17 insertions(+), 78 deletions(-) rename polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/{context => base}/AbstractContextHandler.java (96%) rename polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/{context => base}/BaseBeanHandler.java (84%) delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/EnvironmentChangeEventListener.java diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java index b8a08dbe..bc86a5bd 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java @@ -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); } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/AbstractContextHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/AbstractContextHandler.java similarity index 96% rename from polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/AbstractContextHandler.java rename to polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/AbstractContextHandler.java index 73215c91..400790d6 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/AbstractContextHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/AbstractContextHandler.java @@ -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; diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/BaseBeanHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/BaseBeanHandler.java similarity index 84% rename from polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/BaseBeanHandler.java rename to polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/BaseBeanHandler.java index 13e4adcd..38974ef9 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/context/BaseBeanHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/BaseBeanHandler.java @@ -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; diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java index 68c5e904..9f5a7212 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java @@ -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; diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java index 4f4e30e0..d949f594 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java @@ -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; diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java index bfcc6aee..2b0e79d4 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java @@ -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; diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java index 0d614de6..526f75e8 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java @@ -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"); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/EnvironmentChangeEventListener.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/EnvironmentChangeEventListener.java deleted file mode 100644 index e6000579..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/EnvironmentChangeEventListener.java +++ /dev/null @@ -1,48 +0,0 @@ -package cn.polarismesh.agent.plugin.spring.cloud.interceptor; - -import cn.polarismesh.agent.plugin.spring.cloud.config.ConfigHandler; -import cn.polarismesh.agent.plugin.spring.cloud.metadata.MetadataHandler; -import cn.polarismesh.agent.plugin.spring.cloud.router.RouterHandler; -import cn.polarismesh.agent.plugin.spring.cloud.rpc.RpcEnhancementHandler; - -import org.springframework.cloud.context.environment.EnvironmentChangeEvent; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationEvent; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.event.ContextRefreshedEvent; -import org.springframework.context.event.SmartApplicationListener; - -public class EnvironmentChangeEventListener implements SmartApplicationListener { - - private final RouterHandler routerHandler = new RouterHandler(); - - private final MetadataHandler metadataHandler = new MetadataHandler(); - - private final RpcEnhancementHandler rpcEnhancementHandler = new RpcEnhancementHandler(); - - private final ConfigHandler configHandler = new ConfigHandler(); - - @Override - public boolean supportsEventType(Class eventType) { - return eventType == EnvironmentChangeEvent.class || eventType == ContextRefreshedEvent.class; - } - - @Override - public void onApplicationEvent(ApplicationEvent event) { - Object source = event.getSource(); - if (!(source instanceof ApplicationContext)) { - return; - } - ApplicationContext applicationContext = (ApplicationContext) source; - ConfigurableApplicationContext cfgCtx1 = (ConfigurableApplicationContext) applicationContext; - String enable = cfgCtx1.getEnvironment().getProperty("spring.cloud.polaris.enabled"); - System.out.println("enable is " + enable); - if (null == enable || Boolean.parseBoolean(enable)) { - configHandler.setApplicationContext(applicationContext); - routerHandler.setApplicationContext(applicationContext); - metadataHandler.setApplicationContext(applicationContext); - rpcEnhancementHandler.setApplicationContext(applicationContext); - } - } - -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/metadata/MetadataHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/metadata/MetadataHandler.java index 1212f44d..b248ec4b 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/metadata/MetadataHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/metadata/MetadataHandler.java @@ -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; diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/router/RouterHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/router/RouterHandler.java index a9c53a65..bebe86c5 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/router/RouterHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/router/RouterHandler.java @@ -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; @@ -80,7 +79,7 @@ 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, @@ -88,7 +87,7 @@ public PolarisRuleBasedRouterProperties get() { }); 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, @@ -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() { @@ -137,14 +137,15 @@ public ServiceInstanceListSupplier get() { Map 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) -> { diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/rpc/RpcEnhancementHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/rpc/RpcEnhancementHandler.java index 8039d5c5..da1c979e 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/rpc/RpcEnhancementHandler.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/rpc/RpcEnhancementHandler.java @@ -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; From 8e5277b4d2d7a31e59ca22cf87a8ad4f491792ed Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 15 Apr 2024 12:17:06 +0800 Subject: [PATCH 03/24] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0actuator?= =?UTF-8?q?=E5=88=B0demo=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- polaris-agent-build/conf/polaris-agent.config | 2 +- .../quickstart-examples/consumer/pom.xml | 5 ++ .../quickstart-examples/provider/pom.xml | 5 ++ .../spring-cloud-2023/application.properties | 47 +++---------------- 4 files changed, 17 insertions(+), 42 deletions(-) diff --git a/polaris-agent-build/conf/polaris-agent.config b/polaris-agent-build/conf/polaris-agent.config index 0a206306..0e3aab95 100644 --- a/polaris-agent-build/conf/polaris-agent.config +++ b/polaris-agent-build/conf/polaris-agent.config @@ -1 +1 @@ -plugins.enable=spring-cloud-2021-plugin,spring-cloud-2020-plugin \ No newline at end of file +plugins.enable=spring-cloud-2023-plugin \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/pom.xml index 08bb97ab..a508d3bb 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/pom.xml @@ -56,6 +56,11 @@ org.springframework.cloud spring-cloud-starter-loadbalancer + + + org.springframework.boot + spring-boot-starter-actuator + diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/pom.xml index 12b87e59..e10ef71c 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/pom.xml @@ -51,6 +51,11 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config + + + org.springframework.boot + spring-boot-starter-actuator + diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/conf/plugin/spring-cloud-2023/application.properties b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/conf/plugin/spring-cloud-2023/application.properties index e86ea73c..7347e7ae 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/conf/plugin/spring-cloud-2023/application.properties +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/conf/plugin/spring-cloud-2023/application.properties @@ -1,61 +1,26 @@ -# 应用名称(必填) spring.application.name=application-name -# 配置北极星服务端地址 spring.cloud.polaris.address=grpc\://127.0.0.1\:8091 spring.cloud.polaris.enabled=true -# 启用 Java Agent 的 Spring Cloud Tencent 注册发现能力 spring.cloud.discovery.enabled=true -# 配置服务注册发现的命名空间信息 spring.cloud.polaris.discovery.namespace=default -# 启用从北极星 spring.cloud.polaris.discovery.enabled=true spring.cloud.polaris.discovery.register=true -## 是否启用北极星的 LoadBalancer 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 +spring.cloud.polaris.router.enabled=true +spring.cloud.polaris.router.rule-router.enabled=true +spring.cloud.polaris.router.metadata-router.enabled=true +spring.cloud.polaris.router.nearby-router.enabled=true -## 是否启用北极星的服务限流能力 spring.cloud.polaris.ratelimit.enabled=false -# 设置触发限流时的提示信息 -# spring.cloud.polaris.ratelimit.rejectRequestTips="" -# 设置触发限流时的响应码 spring.cloud.polaris.ratelimit.rejectHttpCode=429 -# 设置限流匀速排队最大排队时间 spring.cloud.polaris.ratelimit.maxQueuingTime=1000 -## RPC 调用增强 -spring.cloud.tencent.rpc-enhancement.enabled=false -# 开启 RPC 调用结果上报 -spring.cloud.tencent.rpc-enhancement.reporter.enabled=false +spring.cloud.tencent.rpc-enhancement.enabled=true +spring.cloud.tencent.rpc-enhancement.reporter.enabled=true -# 配置北极星监控指标上报 spring.cloud.polaris.stat.enabled=false -# 指标上报监听端口 spring.cloud.polaris.stat.port=0 -# 指标上报暴露的 http path 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 From dae0a9ebf567c195dff0c1c9f4ed5ffd0d1cfdad Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 15 Apr 2024 12:35:22 +0800 Subject: [PATCH 04/24] fix: add lossless healthcheck for example --- .../quickstart-examples/consumer/deployment.yaml | 4 ++-- .../quickstart-examples/gateway/deployment.yaml | 2 +- .../quickstart-examples/provider/deployment.yaml | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/deployment.yaml index f63d0a19..abfbf9f1 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/consumer/deployment.yaml @@ -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: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/deployment.yaml index bf18dc10..e2d9a806 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/gateway/deployment.yaml @@ -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 diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/deployment.yaml index 042215fd..92b066b3 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-2023-examples/quickstart-examples/provider/deployment.yaml @@ -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: From 33c84f12f5a543cfea2da8b3ca8453ec3dd83b86 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 15 Apr 2024 13:10:43 +0800 Subject: [PATCH 05/24] fix: add configuration comments --- .../spring-cloud-2023/application.properties | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/conf/plugin/spring-cloud-2023/application.properties b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/conf/plugin/spring-cloud-2023/application.properties index 7347e7ae..2aa0c330 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/conf/plugin/spring-cloud-2023/application.properties +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/conf/plugin/spring-cloud-2023/application.properties @@ -1,26 +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 - +# 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 - +# switch for polaris loadbalancer feature spring.cloud.polaris.loadbalancer.enabled=true - +# 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 +# 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 - +# 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 +# path for polaris stat spring.cloud.polaris.stat.path=/metrics From 4c1533dd48a9f4052341c13242b125aef0eeb5d7 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 15 Apr 2024 16:22:56 +0800 Subject: [PATCH 06/24] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E6=97=A5=E5=BF=97=E4=B8=8D=E8=BE=93=E5=87=BA=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/spring/cloud/common/Holder.java | 2 +- .../src/main/resources/polaris-log4j.xml | 144 ++++++++---- .../src/main/resources/polaris-log4j2.xml | 188 +++++++++++---- .../src/main/resources/polaris-logback.xml | 220 +++++++++++++----- 4 files changed, 402 insertions(+), 152 deletions(-) diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java index 0af70db1..78290a72 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java @@ -235,7 +235,7 @@ private static void runConfigModifiers(Environment environment) throws IOExcepti logConfig = Holder.class.getClassLoader().getResource("polaris-log4j.xml").getFile(); } } - System.setProperty(LoggingConsts.LOGGING_CONFIG_PROPERTY, logConfig); + System.setProperty(LoggingConsts.LOGGING_CONFIG_PROPERTY, "jar:" + logConfig); if (StringUtils.isBlank(polarisContextProperties.getLocalIpAddress())) { polarisContextProperties.setLocalIpAddress(environment.getProperty("spring.cloud.client.ip-address")); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-log4j.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-log4j.xml index 3afab3c5..f338d38a 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-log4j.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-log4j.xml @@ -1,43 +1,103 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-log4j2.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-log4j2.xml index d59af79b..81a556d3 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-log4j2.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-log4j2.xml @@ -1,52 +1,142 @@ - - - - %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n - - - - - - - - - - - - - %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n + + + + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n + + + + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n + + + + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n + + + + + + + + + + + + + %m|%d{yyyy-MM-dd HH:mm:ss.SSS}%n + + + + + + + + + + + + + %m|%d{yyyy-MM-dd HH:mm:ss.SSS}%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-logback.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-logback.xml index 5469f4b2..b0768896 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-logback.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/src/main/resources/polaris-logback.xml @@ -1,63 +1,163 @@ - polaris - - - - ${polaris.log.home}/polaris.log - - - ${polaris.log.home}/polaris.log.%i - ${polaris.log.retain.count:-7} - - - - ${polaris.log.file.size:-10MB} - - - - %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n - - - - - ${polaris.log.home}/polaris-update-event.log - - - ${polaris.log.home}/polaris-update-event.log.%i - ${polaris-update-event.log.retain.count:-7} - - - - ${polaris-update-event.log.file.size:-10MB} - - - - %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n - - - - - 0 - 512 - - - - - - - - - - - - - - - \ No newline at end of file + polaris + + + + ${polaris.log.home}/polaris.log + + + ${polaris.log.home}/polaris.log.%i + ${polaris.log.retain.count:-7} + + + + ${polaris.log.file.size:-10MB} + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n + + + + + ${polaris.log.home}/polaris-update-event.log + + + ${polaris.log.home}/polaris-update-event.log.%i + ${polaris.update.event.log.retain.count:-7} + + + + ${polaris.update.event.log.file.size:-10MB} + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n + + + + + ${polaris.log.home}/polaris-circuitbreaker-event.log + + + ${polaris.log.home}/polaris-circuitbreaker-event.log.%i + + ${polaris.circuitbreaker.event.log.retain.count:-7} + + + + ${polaris.circuitbreaker.event.log.file.size:-10MB} + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n + + + + + ${polaris.log.home}/polaris-healthcheck-event.log + + + ${polaris.log.home}/polaris-healthcheck-event.log.%i + + ${polaris.healthcheck.event.log.retain.count:-7} + + + + ${polaris.healthcheck.event.log.file.size:-10MB} + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n + + + + + ${polaris.log.home}/polaris-lossless-event.log + + + ${polaris.log.home}/polaris-lossless-event.log.%i + + ${polaris.lossless.event.log.retain.count:-7} + + + + ${polaris.lossless.event.log.file.size:-10MB} + + + + %m|%d{yyyy-MM-dd HH:mm:ss.SSS}%n + + + + + ${polaris.log.home}/polaris-instance-heartbeat.log + + + ${polaris.log.home}/polaris-instance-heartbeat.log.%i + + ${polaris.instance.heartbeat.log.retain.count:-7} + + + + ${polaris.instance.heartbeat.log.file.size:-10MB} + + + + %m|%d{yyyy-MM-dd HH:mm:ss.SSS}%n + + + + + 0 + 512 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 0adca0b363b93e95e00e585c6e6452b591bd16b6 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 15 Apr 2024 20:11:58 +0800 Subject: [PATCH 07/24] release: release version to 1.7.0-RC1 --- .../spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml | 2 +- pom.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml index e96f4b72..2dfb5c5c 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-2023-plugin/pom.xml @@ -14,7 +14,7 @@ 2023.0.0 - 1.14.0-2023.0.0-SNAPSHOT + 1.14.0-2023.0.0-RC1 3.2.3 1.7.30 17 diff --git a/pom.xml b/pom.xml index 0a5565ce..90e79840 100644 --- a/pom.xml +++ b/pom.xml @@ -14,8 +14,8 @@ https://github.com/polarismesh/polaris-java-agent - 1.7.0-RC.0 - 1.15.4-SNAPSHOT + 1.7.0-RC1 + 1.15.4 UTF-8 false UTF-8 From 0d37c120b37b4c21c6017737343200b4c4b96547 Mon Sep 17 00:00:00 2001 From: andrewshan Date: Sat, 4 May 2024 00:45:58 +0800 Subject: [PATCH 08/24] feat: new add java agent for hoxton --- .../core/common/utils/ReflectionUtils.java | 46 +++++ .../extension/interceptor/Interceptor.java | 8 +- .../src/main/resources/application.properties | 2 +- .../agent/plugin/spring/cloud/MainPlugin.java | 44 ++--- .../cloud/base/AbstractContextHandler.java | 44 ----- .../spring/cloud/base/BaseBeanHandler.java | 100 ---------- .../spring/cloud/common/ClassNames.java | 7 + .../cloud/common/PropertiesProvider.java | 70 +++++++ .../spring/cloud/config/ConfigHandler.java | 113 ----------- .../cloud/discovery/DiscoveryHandler.java | 179 ----------------- .../ApplicationContextAwareInterceptor.java | 120 ------------ .../cloud/interceptor/BaseInterceptor.java | 76 -------- .../ConfigurationInjectInterceptor.java | 79 -------- .../ConfigurationParserInterceptor.java | 180 ++++++++++++++++++ ...ConfigurationPostProcessorInterceptor.java | 48 +++++ .../cloud/metadata/MetadataHandler.java | 40 ---- pom.xml | 2 +- 17 files changed, 376 insertions(+), 782 deletions(-) delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/AbstractContextHandler.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/BaseBeanHandler.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/BaseInterceptor.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/metadata/MetadataHandler.java diff --git a/polaris-agent-core/polaris-agent-core-common/src/main/java/cn/polarismesh/agent/core/common/utils/ReflectionUtils.java b/polaris-agent-core/polaris-agent-core-common/src/main/java/cn/polarismesh/agent/core/common/utils/ReflectionUtils.java index 0c6ed576..0a1a2d7e 100644 --- a/polaris-agent-core/polaris-agent-core-common/src/main/java/cn/polarismesh/agent/core/common/utils/ReflectionUtils.java +++ b/polaris-agent-core/polaris-agent-core-common/src/main/java/cn/polarismesh/agent/core/common/utils/ReflectionUtils.java @@ -17,6 +17,7 @@ package cn.polarismesh.agent.core.common.utils; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -497,4 +498,49 @@ public static Object invokeMethodByName(Object target, String methodName, Object throw new PolarisAgentException("invokeMethodByName", e); } } + + /** + * 根据类型获取构造器 + * @param clazz + * @param parameterTypes + * @return + * @param + */ + public static Constructor accessibleConstructor(Class clazz, Class... parameterTypes) { + try { + Constructor ctor = clazz.getDeclaredConstructor(parameterTypes); + makeAccessible(ctor); + return ctor; + } catch (NoSuchMethodException e) { + throw new PolarisAgentException("accessibleConstructor failed: ", e); + } + } + + public static void makeAccessible(Constructor ctor) { + if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) { + ctor.setAccessible(true); + } + } + + public static void makeAccessible(Method ctor) { + if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) { + ctor.setAccessible(true); + } + } + + public static T invokeConstructor(Constructor constructor, Object... params) { + try { + return constructor.newInstance(params); + } catch (Exception e) { + throw new PolarisAgentException("invokeConstructor failed: ", e); + } + } + + public static Object invokeMethod(Method method, Object target, Object... params) { + try { + return method.invoke(target, params); + } catch (Exception e) { + throw new PolarisAgentException("invokeMethod failed: ", e); + } + } } diff --git a/polaris-agent-core/polaris-agent-core-extension/src/main/java/cn/polarismesh/agent/core/extension/interceptor/Interceptor.java b/polaris-agent-core/polaris-agent-core-extension/src/main/java/cn/polarismesh/agent/core/extension/interceptor/Interceptor.java index 73dc8aa5..e6dbf3d4 100644 --- a/polaris-agent-core/polaris-agent-core-extension/src/main/java/cn/polarismesh/agent/core/extension/interceptor/Interceptor.java +++ b/polaris-agent-core/polaris-agent-core-extension/src/main/java/cn/polarismesh/agent/core/extension/interceptor/Interceptor.java @@ -19,7 +19,11 @@ public interface Interceptor { - void before(Object target, Object[] args); + default void before(Object target, Object[] args) { - void after(Object target, Object[] args, Object result, Throwable throwable); + } + + default void after(Object target, Object[] args, Object result, Throwable throwable) { + + } } diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties index 786a4d16..3d6589eb 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties @@ -1,7 +1,7 @@ server.port=65201 spring.application.name=service-provider-H spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.enabled=true spring.cloud.gateway.discovery.locator.enabled=false spring.cloud.nacos.username=nacos spring.cloud.nacos.password=nacos diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java index d486d296..53ca510f 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java @@ -17,6 +17,8 @@ package cn.polarismesh.agent.plugin.spring.cloud; +import java.security.ProtectionDomain; + import cn.polarismesh.agent.core.extension.AgentPlugin; import cn.polarismesh.agent.core.extension.PluginContext; import cn.polarismesh.agent.core.extension.instrument.InstrumentClass; @@ -27,12 +29,8 @@ 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.ApplicationContextAwareInterceptor; -import org.springframework.context.ApplicationContext; -import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationInjectInterceptor; - - -import java.security.ProtectionDomain; +import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationParserInterceptor; +import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationPostProcessorInterceptor; /** * Polaris Spring Cloud hoxton Plugin @@ -51,48 +49,40 @@ public void init(PluginContext context) { * add polaris transformers */ private void addPolarisTransformers(TransformOperations operations) { + // 注入默认配置 + operations.transform(ClassNames.CONFIGURATION_CLAZZ_POST_PROCESSOR, ConfigurationPostProcessorTransform.class); - // 在 agent 中注入 Spring 的 ApplicationContext - operations.transform(ClassNames.APPLICATION_CONTEXT_AWARE, ApplicationContextAwareTransform.class); - - - // EnvironmentPostProcessor 处理 - // operations.transform(ClassNames.ENVIRONMENT_POST_PROCESSOR, ConfigurationInjectTransform.class); - + // 注入bootstrap的bean定义 + operations.transform(ClassNames.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class); } - /** - * 注入 spring 的 {@link ApplicationContext} - */ - public static class ApplicationContextAwareTransform implements TransformCallback { + public static class ConfigurationParserTransform implements TransformCallback { @Override - public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, - Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { - + public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); - InstrumentMethod constructMethod = target.getConstructor("org.springframework.context.ConfigurableApplicationContext"); + InstrumentMethod constructMethod = target.getDeclaredMethod("parse", "java.util.Set"); if (constructMethod != null) { - constructMethod.addInterceptor(ApplicationContextAwareInterceptor.class); + constructMethod.addInterceptor(ConfigurationParserInterceptor.class); } return target.toBytecode(); } } - public static class ConfigurationInjectTransform implements TransformCallback { + public static class ConfigurationPostProcessorTransform implements TransformCallback { @Override - public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, - Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { + public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); - InstrumentMethod constructMethod = target.getDeclaredMethod("onApplicationEnvironmentPreparedEvent", "org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent"); + InstrumentMethod constructMethod = target.getDeclaredMethod("processConfigBeanDefinitions", "org.springframework.beans.factory.support.BeanDefinitionRegistry"); if (constructMethod != null) { - constructMethod.addInterceptor(ConfigurationInjectInterceptor.class); + constructMethod.addInterceptor(ConfigurationPostProcessorInterceptor.class); } return target.toBytecode(); } } + } \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/AbstractContextHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/AbstractContextHandler.java deleted file mode 100644 index 341c5233..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/AbstractContextHandler.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.base; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; - -import java.util.function.BiConsumer; - -public abstract class AbstractContextHandler implements ApplicationContextAware { - - public static boolean hasBeanDefinition(ApplicationContext context, String name) { - if (context.containsBeanDefinition(name)) { - return true; - } - ApplicationContext parent = context.getParent(); - if (null != parent) { - return hasBeanDefinition(parent, name); - } - return false; - } - - protected void registerBean(ApplicationContext context, String name, BiConsumer callback) { - if (hasBeanDefinition(context, name)) { - return; - } - callback.accept(context, name); - } -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/BaseBeanHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/BaseBeanHandler.java deleted file mode 100644 index 64a3222f..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/base/BaseBeanHandler.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.base; - -import cn.polarismesh.agent.plugin.spring.cloud.common.Holder; -import com.tencent.cloud.common.metadata.StaticMetadataManager; -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.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; - -import java.util.function.Supplier; - -public class BaseBeanHandler extends AbstractContextHandler { - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - registerPolarisSDKContextManager(applicationContext); - registerBean(applicationContext, "staticMetadataManager", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(StaticMetadataManager.class, new Supplier() { - @Override - public StaticMetadataManager get() { - return Holder.getStaticMetadataManager(); - } - }).getBeanDefinition()); - }); - registerBean(applicationContext, "polarisContextPostConfiguration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( - PolarisContextPostConfiguration.class).getBeanDefinition()); - }); - registerBean(applicationContext, "serviceRuleManager", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(ServiceRuleManager.class, new Supplier() { - @Override - public ServiceRuleManager get() { - return Holder.newServiceRuleManager(); - } - }).getBeanDefinition()); - }); - registerBean(applicationContext, "polarisContextProperties", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(PolarisContextProperties.class, new Supplier() { - @Override - public PolarisContextProperties get() { - return Holder.getPolarisContextProperties(); - } - }).getBeanDefinition()); - }); - registerBean(applicationContext, "polarisLoadBalancerAutoConfiguration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( - PolarisLoadBalancerAutoConfiguration.class).getBeanDefinition()); - }); - } - - private void registerPolarisSDKContextManager(ApplicationContext context) { - registerBean(context, "polarisSDKContextManager", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(PolarisSDKContextManager.class, new Supplier() { - @Override - public PolarisSDKContextManager get() { - return Holder.getContextManager(); - } - }).getBeanDefinition()); - }); - } -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java index 658a0d3d..72252098 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java @@ -88,4 +88,11 @@ public class ClassNames { public static final String INTERCEPTING_HTTP_ACCESSOR = "org.springframework.http.client.support.InterceptingHttpAccessor"; + + public static final String PROPERTY_BOOTSTRAP_CONFIGURATION = "org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration"; + + public static final String CONFIGURATION_CLAZZ_PARSER = "org.springframework.context.annotation.ConfigurationClassParser"; + + + public static final String CONFIGURATION_CLAZZ_POST_PROCESSOR = "org.springframework.context.annotation.ConfigurationClassPostProcessor"; } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java new file mode 100644 index 00000000..aafbe644 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java @@ -0,0 +1,70 @@ +/* + * 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.common; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import com.tencent.polaris.api.utils.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.core.env.PropertiesPropertySource; + +public class PropertiesProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesProvider.class); + + /** + * load the properties source from default application.yaml + * @return propertySources + */ + public static List loadPropertiesSource() { + InputStream stream = PropertiesProvider.class.getClassLoader().getResourceAsStream("default-plugin.conf"); + Properties defaultProperties = new Properties(); + try { + defaultProperties.load(stream); + } catch (IOException e) { + throw new IllegalStateException("fail to load default-plugin.conf", e); + } + List propertySources = new ArrayList<>(); + propertySources.add(new PropertiesPropertySource("__default_polaris_agent_spring_cloud_tencent__", defaultProperties)); + + String configPath = Paths.get(System.getProperty(Constant.AGENT_CONF_PATH), "conf").toString(); + LOGGER.info("load property sources from config path " + configPath); + Properties properties = new Properties(); + String confPath = Paths.get(configPath, "plugin", "spring-cloud-hoxton", "application.properties").toString(); + String cmdVal = System.getProperty("polaris.agent.user.application.conf"); + if (StringUtils.isNotBlank(cmdVal)) { + confPath = cmdVal; + } + try { + properties.load(Files.newInputStream(Paths.get(confPath).toFile().toPath())); + } + catch (IOException e) { + throw new IllegalStateException("fail to load config from " + configPath, e); + } + propertySources.add(new PropertiesPropertySource("__polaris_agent_spring_cloud_tencent__", properties)); + return propertySources; + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java deleted file mode 100644 index fdf540d6..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/config/ConfigHandler.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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 cn.polarismesh.agent.plugin.spring.cloud.base.AbstractContextHandler; -import cn.polarismesh.agent.plugin.spring.cloud.common.Holder; -import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; -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.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.env.Environment; - -import java.util.function.Supplier; - -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() { - @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() { - @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() { - @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() { - @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() { - @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, "polarisConfigAutoConfiguration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(PolarisConfigAutoConfiguration.class).getBeanDefinition()); - }); - } - -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java deleted file mode 100644 index e27a5502..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/discovery/DiscoveryHandler.java +++ /dev/null @@ -1,179 +0,0 @@ -package cn.polarismesh.agent.plugin.spring.cloud.discovery; - -import cn.polarismesh.agent.plugin.spring.cloud.base.AbstractContextHandler; -import cn.polarismesh.agent.plugin.spring.cloud.common.Holder; -import com.google.common.collect.Maps; -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.discovery.PolarisDiscoveryAutoConfiguration; -import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; -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.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.annotation.Autowired; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; -import org.springframework.stereotype.Component; - -import java.util.Map; -import java.util.function.Supplier; - -public class DiscoveryHandler extends AbstractContextHandler { - - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - String discoveryEnable = applicationContext.getEnvironment().getProperty("spring.cloud.discovery.enabled"); - if (null == discoveryEnable || Boolean.parseBoolean(discoveryEnable)) { - registerBean(applicationContext, "polarisDiscoveryProperties", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(PolarisDiscoveryProperties.class, new Supplier() { - @Override - public PolarisDiscoveryProperties get() { - return Holder.getDiscoveryProperties(); - } - }).getBeanDefinition()); - }); - registerBean(applicationContext, "polarisDiscoveryHandler", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(PolarisDiscoveryHandler.class, new Supplier() { - @Override - public PolarisDiscoveryHandler get() { - PolarisDiscoveryProperties polarisDiscoveryProperties = (PolarisDiscoveryProperties) ctx.getBean("polarisDiscoveryProperties"); - PolarisSDKContextManager polarisSDKContextManager = (PolarisSDKContextManager) ctx.getBean("polarisSDKContextManager"); - return new PolarisDiscoveryHandler(polarisDiscoveryProperties, polarisSDKContextManager); - } - }).getBeanDefinition()); - }); - registerBean(applicationContext, "polarisStatProperties", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(PolarisStatProperties.class, new Supplier() { - @Override - public PolarisStatProperties get() { - return Holder.getPolarisStatProperties(); - } - }).getBeanDefinition()); - }); - registerBean(applicationContext, "polarisDiscoveryAutoConfiguration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( - PolarisDiscoveryAutoConfiguration.class).getBeanDefinition()); - }); - registerBean(applicationContext, "sdkContext", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(SDKContext.class, new Supplier() { - @Override - public SDKContext get() { - return Holder.getContextManager().getSDKContext(); - } - }).getBeanDefinition()); - }); - registerBean(applicationContext, "assemblyAPI", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(AssemblyAPI.class, new Supplier() { - @Override - public AssemblyAPI get() { - return Holder.getContextManager().getAssemblyAPI(); - } - }).getBeanDefinition()); - }); - registerBean(applicationContext, "polarisRegistration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( - PolarisRegistration.class).setPrimary(true).getBeanDefinition()); - }); - registerBean(applicationContext, "polarisServiceRegistry", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( - PolarisServiceRegistry.class).setPrimary(true).getBeanDefinition()); - }); - registerBean(applicationContext, "autoServiceRegistrationProperties", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(AutoServiceRegistrationProperties.class, new Supplier() { - @Override - public AutoServiceRegistrationProperties get() { - return new AutoServiceRegistrationProperties(); - } - }).setPrimary(true).getBeanDefinition()); - }); - System.out.println("11111112321312341231231231231231231231312312312312313121312312"); - registerBean(applicationContext, "polarisAutoServiceRegistration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( - PolarisAutoServiceRegistration.class).setFactoryMethodOnBean("org.springframework.beans.factory.support.DefaultListableBeanFactory","").setPrimary(true).getBeanDefinition()); - }); -// @Component -// class TimeCostBeanPostProcessor implements BeanPostProcessor { -// -// Map costMap = Maps.newConcurrentMap(); -// -// @Autowired -// private ApplicationContext applicationContext; -// -// @Override -// public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { -// costMap.put(beanName, System.currentTimeMillis()); -// return bean; -// } -// -// @Override -// public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { -// if ("autoServiceRegistrationProperties".equals(beanName)) { -// // 在polarisAutoServiceRegistration初始化之后注册autoServiceRegistrationProperties -// ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) applicationContext; -// DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); -// beanFactory.registerBeanDefinition("polarisAutoServiceRegistration", -// BeanDefinitionBuilder.genericBeanDefinition(PolarisAutoServiceRegistration.class) -// .getBeanDefinition()); -// } -// Long start = costMap.get(beanName); -// long cost = System.currentTimeMillis() - start; -// costMap.put(beanName, cost); -//// System.out.println("class: " + bean.getClass().getName() -//// + "\tbean: "+ beanName -//// + "\ttime: "+ cost); -// -// return bean; -// } -// } -// registerBean(applicationContext, "polarisSDKContextManager", (ctx, name) -> { -// ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; -// DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); -// beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( -// PolarisSDKContextManager.class).setPrimary(true).getBeanDefinition()); -// }); - registerBean(applicationContext, "losslessAutoConfiguration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, BeanDefinitionBuilder.genericBeanDefinition( - LosslessAutoConfiguration.class).setPrimary(true).getBeanDefinition()); - }); - - } - } -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java deleted file mode 100644 index 6dcaa477..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ApplicationContextAwareInterceptor.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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.interceptor; - -import cn.polarismesh.agent.core.common.logger.CommonLogger; -import cn.polarismesh.agent.core.common.logger.StdoutCommonLoggerFactory; -import cn.polarismesh.agent.plugin.spring.cloud.base.BaseBeanHandler; -import cn.polarismesh.agent.plugin.spring.cloud.common.Holder; -import cn.polarismesh.agent.plugin.spring.cloud.discovery.DiscoveryHandler; -import cn.polarismesh.agent.plugin.spring.cloud.metadata.MetadataHandler; -import com.google.common.collect.Maps; -import com.tencent.cloud.common.metadata.MetadataContext; -import com.tencent.cloud.common.util.ApplicationContextAwareUtils; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.context.ApplicationContextAware; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; -import com.tencent.cloud.polaris.registry.PolarisAutoServiceRegistration; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -public class ApplicationContextAwareInterceptor extends BaseInterceptor { - - private static final CommonLogger logger = StdoutCommonLoggerFactory.INSTANCE - .getLogger(ApplicationContextAwareInterceptor.class.getCanonicalName()); - - - @Override - public void onAfter(Object target, Object[] args, Object result, Throwable throwable) { - - - ConfigurableApplicationContext context = (ConfigurableApplicationContext) args[0]; - String enable = context.getEnvironment().getProperty("spring.cloud.polaris.enabled"); - if (null != enable && !Boolean.parseBoolean(enable)) { - logger.warn("polaris is disabled, no polaris inject actions will be taken"); - return; - } - ApplicationContextAwareUtils utils = new ApplicationContextAwareUtils(); - - // MetadataContext 需要读取到 agent 配置的内容 - AnnotationConfigApplicationContext tmpCtx = new AnnotationConfigApplicationContext((DefaultListableBeanFactory) context.getBeanFactory()); - tmpCtx.setEnvironment((ConfigurableEnvironment) Holder.getEnvironment()); - utils.setApplicationContext(tmpCtx); - // 触发 MetadataContext 的加载机制 - MetadataContext metadataContext = new MetadataContext(); - // 重写 SCT 中错误的 LOCAL_SERVICE 信息 - MetadataContext.LOCAL_SERVICE = Holder.getDiscoveryProperties().getService(); - - // 设置为真正的 ApplicationContext - utils.setApplicationContext(context); - buildAwares().forEach(aware -> aware.setApplicationContext(context)); - -// @Component -// class TimeCostBeanPostProcessor implements BeanPostProcessor { -// -// Map costMap = Maps.newConcurrentMap(); -// -// @Autowired -// private ApplicationContext applicationContext; -// -// @Override -// public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { -// costMap.put(beanName, System.currentTimeMillis()); -// return bean; -// } -// -// @Override -// public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { -// if ("autoServiceRegistrationProperties".equals(beanName)) { -// // 在polarisAutoServiceRegistration初始化之后注册autoServiceRegistrationProperties -// ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) applicationContext; -// DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); -// beanFactory.registerBeanDefinition("polarisAutoServiceRegistration", -// BeanDefinitionBuilder.genericBeanDefinition(PolarisAutoServiceRegistration.class) -// .getBeanDefinition()); -// } -// Long start = costMap.get(beanName); -// long cost = System.currentTimeMillis() - start; -// costMap.put(beanName, cost); -//// System.out.println("class: " + bean.getClass().getName() -//// + "\tbean: "+ beanName -//// + "\ttime: "+ cost); -// -// return bean; -// } -// } - } - - private List buildAwares() { - return Arrays.asList( - new BaseBeanHandler(), - new DiscoveryHandler(), - new MetadataHandler() - ); - } -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/BaseInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/BaseInterceptor.java deleted file mode 100644 index 758aea3f..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/BaseInterceptor.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.interceptor; - -import cn.polarismesh.agent.core.common.exception.PolarisAgentException; -import cn.polarismesh.agent.core.extension.interceptor.Interceptor; -import cn.polarismesh.agent.plugin.spring.cloud.common.Holder; -import com.tencent.cloud.common.metadata.MetadataContextHolder; -import org.springframework.util.ReflectionUtils; -import org.springframework.context.ApplicationContext; - -import java.lang.reflect.Field; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * @author liaochuntao - */ -public abstract class BaseInterceptor implements Interceptor { - - private static final AtomicBoolean initialize = new AtomicBoolean(false); - - @Override - public final void before(Object target, Object[] args) { - init(); - onBefore(target, args); - } - - @Override - public final void after(Object target, Object[] args, Object result, Throwable throwable) { - init(); - onAfter(target, args, result, throwable); - } - - public void onBefore(Object target, Object[] args) { - - } - - public void onAfter(Object target, Object[] args, Object result, Throwable throwable) { - - } - - - private void init() { - if (initialize.compareAndSet(false, true)) { - try { - Holder.init(); - - Field field = MetadataContextHolder.class.getDeclaredField("metadataLocalProperties"); - field.setAccessible(true); - ReflectionUtils.setField(field, null, Holder.getLocalProperties()); - - field = MetadataContextHolder.class.getDeclaredField("staticMetadataManager"); - field.setAccessible(true); - ReflectionUtils.setField(field, null, Holder.getStaticMetadataManager()); - } - catch (Exception e) { - throw new PolarisAgentException("setValueByFieldName", e); - } - } - } -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java deleted file mode 100644 index d516fc70..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationInjectInterceptor.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.interceptor; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; -import org.springframework.boot.env.EnvironmentPostProcessor; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.PropertiesPropertySource; - -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; - -/** - * {@link org.springframework.boot.util.Instantiator#instantiate(Stream)} - * - * @author liaochuntao - */ -public class ConfigurationInjectInterceptor extends BaseInterceptor { - - private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationInjectInterceptor.class); - - @Override - public void onBefore(Object target, Object[] args) { - ApplicationEnvironmentPreparedEvent event = (ApplicationEnvironmentPreparedEvent) args[0]; - - List external = new ArrayList<>(); - // 插入禁止 spring cloud alibaba 的一切能力 - external.add(new DisableSpringCloudAlibabaAbility()); - - for (EnvironmentPostProcessor processor : external) { - processor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication()); - } - } - - private static class DisableSpringCloudAlibabaAbility implements EnvironmentPostProcessor { - - @Override - public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { - disableSpringCloudAlibabaAbility(environment); - } - - private void disableSpringCloudAlibabaAbility(ConfigurableEnvironment environment) { -// String disableSCA = "__disable__sca__"; -// -// Properties properties = new Properties(); -// properties.setProperty("spring.cloud.sentinel.enabled", "false"); -// properties.setProperty("spring.cloud.nacos.discovery.watch.enabled", "false"); -// properties.setProperty("spring.cloud.loadbalancer.cache.enabled", "false"); -// properties.setProperty("spring.cloud.nacos.config.enabled", "false"); - - String disableCheck = "__disable__check__"; - Properties properties = new Properties(); - properties.setProperty("spring.cloud.polaris.config.import-check.enabled", "false"); - environment.getPropertySources().addFirst(new PropertiesPropertySource(disableCheck, properties)); - - LOGGER.info("[PolarisAgent] disable spring cloud alibaba all ability"); - } - } - -} \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java new file mode 100644 index 00000000..5cf41e06 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -0,0 +1,180 @@ +/* + * 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.interceptor; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Set; +import java.util.function.Predicate; + +import cn.polarismesh.agent.core.common.utils.ClassUtils; +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.core.extension.interceptor.Interceptor; +import com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration; +import com.tencent.cloud.common.util.ApplicationContextAwareUtils; +import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration; +import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesAutoConfiguration; +import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextBootstrapAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration; +import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; +import com.tencent.cloud.polaris.router.config.FeignAutoConfiguration; +import com.tencent.cloud.polaris.router.config.RouterAutoConfiguration; +import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration; +import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementBootstrapConfiguration; +import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration; +import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration; + +import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.BeanDefinitionHolder; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class ConfigurationParserInterceptor implements Interceptor { + + private static final Predicate DEFAULT_EXCLUSION_FILTER = className -> + (className.startsWith("java.lang.annotation.") || className.startsWith("org.springframework.stereotype.")); + + private static boolean isMainBeanDefinition(BeanDefinitionHolder beanDefinitionHolder) { + BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition(); + if (beanDefinition instanceof AnnotatedGenericBeanDefinition) { + AnnotatedGenericBeanDefinition annotatedBeanDefinition = (AnnotatedGenericBeanDefinition)beanDefinition; + Class beanClass = annotatedBeanDefinition.getBeanClass(); + Annotation[] annotations = beanClass.getAnnotations(); + for (Annotation annotation : annotations) { + Class aClass = annotation.annotationType(); + if ("org.springframework.boot.autoconfigure.SpringBootApplication".equals(aClass.getCanonicalName())) { + return true; + } + } + } + return false; + } + + @Override + public void after(Object target, Object[] args, Object result, Throwable throwable) { + Set candidates = (Set) args[0]; + if (candidates.size() != 1) { + return; + } + BeanDefinitionHolder beanDefinitionHolder = (BeanDefinitionHolder) candidates.iterator().next(); + if ("bootstrapImportSelectorConfiguration".equals(beanDefinitionHolder.getBeanName())) { + // bootstrap + Class clazz = ClassUtils.getClazz("org.springframework.context.annotation.ConfigurationClass", null); + Constructor constructor = ReflectionUtils.accessibleConstructor(clazz, Class.class, String.class); + Method processConfigurationClass = ReflectionUtils.findMethod(target.getClass(), "processConfigurationClass", clazz, Predicate.class); + ReflectionUtils.makeAccessible(processConfigurationClass); + + BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry"); + + Object polarisContextBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisContextBootstrapAutoConfiguration.class, "polarisContextBootstrapAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisContextBootstrapAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisContextBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisContextBootstrapAutoConfiguration.class).getBeanDefinition()); + + // rpc + Object polarisStatPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisStatPropertiesBootstrapConfiguration.class, "polarisStatPropertiesBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisStatPropertiesBootstrapConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisStatPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisStatPropertiesAutoConfiguration.class).getBeanDefinition()); + Object rpcEnhancementBootstrapConfiguration = ReflectionUtils.invokeConstructor(constructor, RpcEnhancementBootstrapConfiguration.class, "rpcEnhancementBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, rpcEnhancementBootstrapConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("rpcEnhancementBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + RpcEnhancementBootstrapConfiguration.class).getBeanDefinition()); + + // lossless + Object losslessPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, LosslessPropertiesAutoConfiguration.class, "losslessPropertiesAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, losslessPropertiesAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("losslessPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + LosslessPropertiesAutoConfiguration.class).getBeanDefinition()); + Object losslessPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(constructor, LosslessPropertiesBootstrapConfiguration.class, "losslessPropertiesBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, losslessPropertiesBootstrapConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("losslessPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + LosslessPropertiesBootstrapConfiguration.class).getBeanDefinition()); + + } else if (isMainBeanDefinition(beanDefinitionHolder)) { + Class clazz = ClassUtils.getClazz("org.springframework.context.annotation.ConfigurationClass", null); + Constructor constructor = ReflectionUtils.accessibleConstructor(clazz, Class.class, String.class); + Method processConfigurationClass = ReflectionUtils.findMethod(target.getClass(), "processConfigurationClass", clazz, Predicate.class); + ReflectionUtils.makeAccessible(processConfigurationClass); + + BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry"); + + // sct common + Object applicationContextAwareUtils = ReflectionUtils.invokeConstructor(constructor, ApplicationContextAwareUtils.class, "applicationContextAwareUtils"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, applicationContextAwareUtils, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("applicationContextAwareUtils", BeanDefinitionBuilder.genericBeanDefinition( + ApplicationContextAwareUtils.class).getBeanDefinition()); + Object metadataAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, MetadataAutoConfiguration.class, "metadataAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, metadataAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("metadataAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + MetadataAutoConfiguration.class).getBeanDefinition()); + + // polaris-context + Object polarisContextAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisContextAutoConfiguration.class, "polarisContextAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisContextAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisContextAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisContextAutoConfiguration.class).getBeanDefinition()); + Object polarisContextPostConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisContextPostConfiguration.class, "polarisContextPostConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisContextPostConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisContextPostConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisContextPostConfiguration.class).getBeanDefinition()); + + // rpc + Object polarisStatPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisStatPropertiesAutoConfiguration.class, "polarisStatPropertiesAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisStatPropertiesAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisStatPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisStatPropertiesBootstrapConfiguration.class).getBeanDefinition()); + Object rpcEnhancementAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, RpcEnhancementAutoConfiguration.class, "rpcEnhancementAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, rpcEnhancementAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("rpcEnhancementAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + RpcEnhancementAutoConfiguration.class).getBeanDefinition()); + + // lossless + Object losslessAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, LosslessAutoConfiguration.class, "losslessAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, losslessAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("losslessAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + LosslessAutoConfiguration.class).getBeanDefinition()); + + // loadbalancer + Object polarisLoadBalancerAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisLoadBalancerAutoConfiguration.class, "polarisLoadBalancerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisLoadBalancerAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisLoadBalancerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisLoadBalancerAutoConfiguration.class).getBeanDefinition()); + + // router + Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, RouterAutoConfiguration.class, "routerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, routerAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("routerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + RouterAutoConfiguration.class).getBeanDefinition()); + if (null != ClassUtils.getClazz("feign.RequestInterceptor", + Thread.currentThread().getContextClassLoader())) { + Object feignAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, FeignAutoConfiguration.class, "feignAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, feignAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("feignAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + FeignAutoConfiguration.class).getBeanDefinition()); + } + + } + } +} + + diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java new file mode 100644 index 00000000..ca0115c4 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java @@ -0,0 +1,48 @@ +/* + * 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.interceptor; + +import java.util.List; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.core.extension.interceptor.Interceptor; +import cn.polarismesh.agent.plugin.spring.cloud.common.PropertiesProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertiesPropertySource; + +public class ConfigurationPostProcessorInterceptor implements Interceptor { + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationPostProcessorInterceptor.class); + + @Override + public void before(Object target, Object[] args) { + ConfigurableEnvironment environment = (ConfigurableEnvironment) ReflectionUtils.getObjectByFieldName(target, "environment"); + List propertySources = PropertiesProvider.loadPropertiesSource(); + MutablePropertySources mutablePropertySources = environment.getPropertySources(); + if (mutablePropertySources.contains(propertySources.get(0).getName())) { + return; + } + for (PropertiesPropertySource propertiesPropertySource : propertySources) { + environment.getPropertySources().addFirst(propertiesPropertySource); + } + } + +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/metadata/MetadataHandler.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/metadata/MetadataHandler.java deleted file mode 100644 index ad271828..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/metadata/MetadataHandler.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.metadata; - -import cn.polarismesh.agent.plugin.spring.cloud.base.AbstractContextHandler; -import com.tencent.cloud.metadata.config.MetadataTransferAutoConfiguration; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; - -public class MetadataHandler extends AbstractContextHandler { - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - registerBean(applicationContext, "metadataTransferAutoConfiguration", (ctx, name) -> { - ConfigurableApplicationContext cfgCtx = (ConfigurableApplicationContext) ctx; - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) cfgCtx.getBeanFactory(); - beanFactory.registerBeanDefinition(name, - BeanDefinitionBuilder.genericBeanDefinition(MetadataTransferAutoConfiguration.class).getBeanDefinition()); - }); - } - -} diff --git a/pom.xml b/pom.xml index 90e79840..e226cc04 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ https://github.com/polarismesh/polaris-java-agent - 1.7.0-RC1 + 1.7.0-RC2 1.15.4 UTF-8 false From 286d397215a311e30b2148711e23c58032d06751 Mon Sep 17 00:00:00 2001 From: andrewshan Date: Sat, 4 May 2024 22:57:45 +0800 Subject: [PATCH 09/24] feat: support service registry and config --- .../spring-cloud-hoxton-plugin/pom.xml | 2 +- .../agent/plugin/spring/cloud/MainPlugin.java | 16 ++++++ .../spring/cloud/common/ClassNames.java | 4 ++ .../ConfigurationParserInterceptor.java | 42 ++++++++++++++++ .../interceptor/RegisterBeanInterceptor.java | 50 +++++++++++++++++++ 5 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/RegisterBeanInterceptor.java diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml index bd48c53e..1ceac457 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml @@ -14,7 +14,7 @@ Hoxton.SR12 - 1.14.0-Hoxton.SR12-RC1 + 1.14.0-Hoxton.SR12-SNAPSHOT 2.3.7.RELEASE 1.7.30 diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java index 53ca510f..6a61ad6d 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java @@ -31,6 +31,7 @@ import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationParserInterceptor; import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationPostProcessorInterceptor; +import cn.polarismesh.agent.plugin.spring.cloud.interceptor.RegisterBeanInterceptor; /** * Polaris Spring Cloud hoxton Plugin @@ -54,6 +55,9 @@ private void addPolarisTransformers(TransformOperations operations) { // 注入bootstrap的bean定义 operations.transform(ClassNames.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class); + + // 注入bean定义的调整设置 + operations.transform(ClassNames.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class); } public static class ConfigurationParserTransform implements TransformCallback { @@ -84,5 +88,17 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, } } + public static class RegisterBeanDefinitionTransform implements TransformCallback { + + @Override + public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { + InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); + InstrumentMethod constructMethod = target.getDeclaredMethod("registerBeanDefinition", "java.lang.String", "org.springframework.beans.factory.config.BeanDefinition"); + if (constructMethod != null) { + constructMethod.addInterceptor(RegisterBeanInterceptor.class); + } + return target.toBytecode(); + } + } } \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java index 72252098..711bc4f0 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java @@ -95,4 +95,8 @@ public class ClassNames { public static final String CONFIGURATION_CLAZZ_POST_PROCESSOR = "org.springframework.context.annotation.ConfigurationClassPostProcessor"; + + public static final String BEAN_DEFINITION_REGISTRY = "org.springframework.beans.factory.support.DefaultListableBeanFactory"; + + } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java index 5cf41e06..0117e432 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -31,10 +31,17 @@ import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration; import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesAutoConfiguration; import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration; +import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; +import com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration; +import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; +import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.context.config.PolarisContextBootstrapAutoConfiguration; import com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration; +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; +import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration; +import com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration; import com.tencent.cloud.polaris.router.config.FeignAutoConfiguration; import com.tencent.cloud.polaris.router.config.RouterAutoConfiguration; import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration; @@ -110,6 +117,18 @@ public void after(Object target, Object[] args, Object result, Throwable throwab registry.registerBeanDefinition("losslessPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( LosslessPropertiesBootstrapConfiguration.class).getBeanDefinition()); + // register + Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesBootstrapAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("discoveryPropertiesBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + DiscoveryPropertiesBootstrapAutoConfiguration.class).getBeanDefinition()); + + // config + Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigBootstrapAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisConfigBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisConfigBootstrapAutoConfiguration.class).getBeanDefinition()); + } else if (isMainBeanDefinition(beanDefinitionHolder)) { Class clazz = ClassUtils.getClazz("org.springframework.context.annotation.ConfigurationClass", null); Constructor constructor = ReflectionUtils.accessibleConstructor(clazz, Class.class, String.class); @@ -173,6 +192,29 @@ public void after(Object target, Object[] args, Object result, Throwable throwab FeignAutoConfiguration.class).getBeanDefinition()); } + // registry + Object discoveryPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesAutoConfiguration.class, "discoveryPropertiesAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("discoveryPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + DiscoveryPropertiesAutoConfiguration.class).getBeanDefinition()); + Object polarisDiscoveryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryAutoConfiguration.class, "polarisDiscoveryAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisDiscoveryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisDiscoveryAutoConfiguration.class).getBeanDefinition()); + Object polarisDiscoveryRibbonAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryRibbonAutoConfiguration.class, "polarisDiscoveryRibbonAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryRibbonAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisDiscoveryRibbonAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisDiscoveryRibbonAutoConfiguration.class).getBeanDefinition()); + Object polarisServiceRegistryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisServiceRegistryAutoConfiguration.class, "polarisServiceRegistryAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisServiceRegistryAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisServiceRegistryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisServiceRegistryAutoConfiguration.class).getBeanDefinition()); + + // config + Object polarisConfigAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigAutoConfiguration.class, "polarisConfigAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisConfigAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisConfigAutoConfiguration.class).getBeanDefinition()); } } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/RegisterBeanInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/RegisterBeanInterceptor.java new file mode 100644 index 00000000..6e98df0d --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/RegisterBeanInterceptor.java @@ -0,0 +1,50 @@ +/* + * 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.interceptor; + +import java.util.HashSet; +import java.util.Set; + +import cn.polarismesh.agent.core.extension.interceptor.Interceptor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.beans.factory.config.BeanDefinition; + +public class RegisterBeanInterceptor implements Interceptor { + + private static final Logger LOGGER = LoggerFactory.getLogger(RegisterBeanInterceptor.class); + + private final Set primaryBeanNames = new HashSet<>(); + + public RegisterBeanInterceptor() { + primaryBeanNames.add("polarisAutoServiceRegistration"); + primaryBeanNames.add("polarisRegistration"); + primaryBeanNames.add("polarisServiceRegistry"); + } + + @Override + public void before(Object target, Object[] args) { + String beanName = (String) args[0]; + if (!primaryBeanNames.contains(beanName)) { + return; + } + BeanDefinition beanDefinition = (BeanDefinition) args[1]; + beanDefinition.setPrimary(true); + } +} From 6fa3d0bcc98dc4cbfd5948fee5bce1e4994cd50f Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 6 May 2024 12:35:45 +0800 Subject: [PATCH 10/24] feat: refrator all inject logics into BeanInjector --- .../spring-cloud-hoxton-plugin/pom.xml | 44 +++ .../agent/plugin/spring/cloud/MainPlugin.java | 7 +- .../spring/cloud/common/BeanInjector.java | 38 ++ .../spring/cloud/common/ClassNames.java | 102 ----- .../plugin/spring/cloud/common/Constant.java | 14 +- .../plugin/spring/cloud/common/Holder.java | 355 ------------------ .../cloud/inject/CommonBeanInjector.java | 54 +++ .../cloud/inject/ConfigBeanInjector.java | 37 ++ .../inject/LoadbalancerBeanInjector.java | 44 +++ .../cloud/inject/LosslessBeanInjector.java | 53 +++ .../inject/PolarisContextBeanInjector.java | 62 +++ .../cloud/inject/RegistryBeanInjector.java | 37 ++ .../cloud/inject/RouterBeanInjector.java | 37 ++ .../inject/RpcEnhancementBeanInjector.java | 58 +++ .../ConfigurationParserInterceptor.java | 116 ++---- 15 files changed, 515 insertions(+), 543 deletions(-) create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java delete mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml index 1ceac457..c77582a6 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml @@ -366,6 +366,50 @@ + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-circuitbreaker + ${spring.cloud.tencent.version} + + + com.tencent.cloud + spring-cloud-tencent-rpc-enhancement + + + com.tencent.polaris + polaris-circuitbreaker-factory + + + com.tencent.polaris + healthchecker-http + + + com.tencent.polaris + healthchecker-udp + + + com.tencent.polaris + healthchecker-tcp + + + + + + com.tencent.cloud + spring-cloud-starter-tencent-polaris-ratelimit + ${spring.cloud.tencent.version} + + + com.tencent.cloud + spring-cloud-tencent-rpc-enhancement + + + com.tencent.polaris + polaris-ratelimit-factory + + + + diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java index 6a61ad6d..8e544ba2 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java @@ -27,7 +27,6 @@ import cn.polarismesh.agent.core.extension.instrument.exception.InstrumentException; import cn.polarismesh.agent.core.extension.transform.TransformCallback; 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.ConfigurationParserInterceptor; import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationPostProcessorInterceptor; @@ -51,13 +50,13 @@ public void init(PluginContext context) { */ private void addPolarisTransformers(TransformOperations operations) { // 注入默认配置 - operations.transform(ClassNames.CONFIGURATION_CLAZZ_POST_PROCESSOR, ConfigurationPostProcessorTransform.class); + operations.transform(Constant.CONFIGURATION_CLAZZ_POST_PROCESSOR, ConfigurationPostProcessorTransform.class); // 注入bootstrap的bean定义 - operations.transform(ClassNames.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class); + operations.transform(Constant.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class); // 注入bean定义的调整设置 - operations.transform(ClassNames.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class); + operations.transform(Constant.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class); } public static class ConfigurationParserTransform implements TransformCallback { diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java new file mode 100644 index 00000000..55aeb9a7 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java @@ -0,0 +1,38 @@ +/* + * 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.common; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public interface BeanInjector { + + /** + * 在Bootstrap启动过程中进行Bean装载 + */ + void onBootstrapStartup(Object configurationParser, + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry); + + /** + * 在应用启动过程中进行Bean装载 + */ + void onApplicationStartup(Object configurationParser, + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry); +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java deleted file mode 100644 index 711bc4f0..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/ClassNames.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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.common; - -import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; - -import java.util.List; - -/** - * @author liaochuntao - */ -public class ClassNames { - - - /** - * {@link org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration#start()} - */ - public static final String SERVICE_REGISTRATION = "org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration"; - - /** - * {@link org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient#CompositeDiscoveryClient(List)} - */ - public static final String DISCOVERY_CLIENT = "org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient"; - - /** - * {@link org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClient#ReactiveCompositeDiscoveryClient(List)} - */ - public static final String REACTIVE_DISCOVERY_CLIENT = "org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClient"; - - /** - * {@link org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplierBuilder#withDiscoveryClient()} - * {@link org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplierBuilder#withBlockingDiscoveryClient()} - * {@link org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplierBuilder#withCaching()} - */ - public static final String ROUTER = "org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplierBuilder"; - - /** - * {@link org.springframework.web.server.handler.FilteringWebHandler#FilteringWebHandler(org.springframework.web.server.WebHandler, List)} - */ - public static final String REACTIVE_WEB_FILTER = "org.springframework.web.server.handler.FilteringWebHandler"; - - /** - * {@link org.springframework.web.servlet.DispatcherServlet#initStrategies(ApplicationContext)} - */ - public static final String SERVLET_WEB_FILTER = "org.springframework.web.servlet.DispatcherServlet"; - - /** - * {@link org.springframework.http.client.support.InterceptingHttpAccessor#getInterceptors()} - */ - public static final String REST_TEMPLATE = "org.springframework.http.client.support.InterceptingHttpAccessor"; - - /** - * {@link org.springframework.cloud.openfeign.FeignClientFactoryBean#getInheritedAwareInstances(FeignContext, Class)} - */ - public static final String FEIGN_TEMPLATE = "org.springframework.cloud.openfeign.FeignClientFactoryBean"; - - /** - * {@link org.springframework.context.support.ApplicationContextAwareProcessor#ApplicationContextAwareProcessor(ConfigurableApplicationContext)} - */ - public static final String APPLICATION_CONTEXT_AWARE = "org.springframework.context.support.ApplicationContextAwareProcessor"; - - /** - * {@link org.springframework.boot.env.EnvironmentPostProcessorApplicationListener#onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent)} - */ - public static final String ENVIRONMENT_POST_PROCESSOR = "org.springframework.boot.env.EnvironmentPostProcessorApplicationListener"; - - /** - * {@link org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient} - */ - public static final String BLOCKING_LOADBALANCER_CLIENT = "org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient"; - - - public static final String INTERCEPTING_HTTP_ACCESSOR = "org.springframework.http.client.support.InterceptingHttpAccessor"; - - public static final String PROPERTY_BOOTSTRAP_CONFIGURATION = "org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration"; - - public static final String CONFIGURATION_CLAZZ_PARSER = "org.springframework.context.annotation.ConfigurationClassParser"; - - - public static final String CONFIGURATION_CLAZZ_POST_PROCESSOR = "org.springframework.context.annotation.ConfigurationClassPostProcessor"; - - public static final String BEAN_DEFINITION_REGISTRY = "org.springframework.beans.factory.support.DefaultListableBeanFactory"; - - -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java index 2e34941d..d0d325db 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java @@ -17,12 +17,20 @@ package cn.polarismesh.agent.plugin.spring.cloud.common; +import java.util.function.Predicate; + public interface Constant { - String POLARIS_CONF_FILE = "polaris.yml"; + String AGENT_CONF_PATH = "__agent_conf_path__"; - int POLARIS_DEFAULT_WEIGHT = 100; + String CONFIGURATION_CLAZZ_PARSER = "org.springframework.context.annotation.ConfigurationClassParser"; - String AGENT_CONF_PATH = "__agent_conf_path__"; + + String CONFIGURATION_CLAZZ_POST_PROCESSOR = "org.springframework.context.annotation.ConfigurationClassPostProcessor"; + + String BEAN_DEFINITION_REGISTRY = "org.springframework.beans.factory.support.DefaultListableBeanFactory"; + + Predicate DEFAULT_EXCLUSION_FILTER = className -> + (className.startsWith("java.lang.annotation.") || className.startsWith("org.springframework.stereotype.")); } \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java deleted file mode 100644 index bf9c3731..00000000 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Holder.java +++ /dev/null @@ -1,355 +0,0 @@ -/* - * 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.common; - -import cn.polarismesh.agent.core.common.exception.PolarisAgentException; -import com.tencent.cloud.common.metadata.StaticMetadataManager; -import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; -import com.tencent.cloud.plugin.lossless.config.LosslessConfigModifier; -import com.tencent.cloud.plugin.lossless.config.LosslessProperties; -import com.tencent.cloud.polaris.DiscoveryConfigModifier; -import com.tencent.cloud.polaris.PolarisDiscoveryConfigModifier; -import com.tencent.cloud.polaris.PolarisDiscoveryProperties; -import com.tencent.cloud.polaris.config.ConfigurationModifier; -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.cloud.polaris.context.ModifyAddress; -import com.tencent.cloud.polaris.context.PolarisConfigModifier; -import com.tencent.cloud.polaris.context.PolarisSDKContextManager; -import com.tencent.cloud.polaris.context.ServiceRuleManager; -import com.tencent.cloud.polaris.context.config.PolarisContextProperties; -import com.tencent.cloud.polaris.extend.consul.ConsulConfigModifier; -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; -import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties; -import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatProperties; -import com.tencent.cloud.rpc.enhancement.stat.config.StatConfigModifier; -import com.tencent.polaris.api.utils.StringUtils; -import com.tencent.polaris.logging.LoggingConsts; -import com.tencent.polaris.logging.PolarisLogging; -import org.springframework.boot.context.properties.bind.Bindable; -import org.springframework.boot.context.properties.bind.Binder; -import org.springframework.cloud.client.HostInfoEnvironmentPostProcessor; -import org.springframework.cloud.commons.util.InetUtils; -import org.springframework.cloud.commons.util.InetUtilsProperties; -import org.springframework.core.ResolvableType; -import org.springframework.core.env.Environment; -import org.springframework.core.env.PropertiesPropertySource; -import org.springframework.core.env.StandardEnvironment; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.*; - -import static com.tencent.cloud.polaris.extend.nacos.NacosContextProperties.DEFAULT_GROUP; - -/** - * @author liaochuntao - */ -public class Holder { - - private static final String GROUP_SERVER_ID_FORMAT = "%s__%s"; - private static final String NACOS_CLUSTER = "nacos.cluster"; - - private static PolarisDiscoveryProperties discoveryProperties; - - private static ConsulContextProperties consulContextProperties; - - private static NacosContextProperties nacosContextProperties; - - private static MetadataLocalProperties localProperties; - - private static StaticMetadataManager staticMetadataManager; - - private static PolarisContextProperties polarisContextProperties; - - private static PolarisCryptoConfigProperties polarisCryptoConfigProperties; - - private static PolarisRuleBasedRouterProperties routerProperties; - - private static PolarisNearByRouterProperties nearByRouterProperties; - - private static LosslessProperties losslessProperties; - - private static PolarisMetadataRouterProperties metadataRouterProperties; - - private static PolarisConfigProperties polarisConfigProperties; - - private static PolarisStatProperties polarisStatProperties; - - private static RpcEnhancementReporterProperties rpcEnhancementReporterProperties; - - private static String CONF_FILE_PATH; - - private static boolean allowDiscovery = true; - - private static PolarisSDKContextManager contextManager; - - private static Environment environment; - - private static PolarisPropertySourceManager polarisPropertySourceManager; - - private static void initProperties() { - polarisContextProperties = new PolarisContextProperties(); - discoveryProperties = new PolarisDiscoveryProperties(); - consulContextProperties = new ConsulContextProperties(); - nacosContextProperties = new NacosContextProperties(); - localProperties = new MetadataLocalProperties(); - routerProperties = new PolarisRuleBasedRouterProperties(); - nearByRouterProperties = new PolarisNearByRouterProperties(); - metadataRouterProperties = new PolarisMetadataRouterProperties(); - polarisConfigProperties = new PolarisConfigProperties(); - polarisStatProperties = new PolarisStatProperties(); - losslessProperties = new LosslessProperties(); - rpcEnhancementReporterProperties = new RpcEnhancementReporterProperties(); - polarisCryptoConfigProperties = new PolarisCryptoConfigProperties(); - discoveryProperties.setService(Holder.getLocalService()); - polarisPropertySourceManager = new PolarisPropertySourceManager(); - } - - public static void init() { - CONF_FILE_PATH = Paths.get(System.getProperty(Constant.AGENT_CONF_PATH), "conf").toString(); - initProperties(); - try (InetUtils utils = new InetUtils(new InetUtilsProperties())) { - polarisContextProperties.setLocalIpAddress(utils.findFirstNonLoopbackHostInfo().getIpAddress()); - // 读取 application.yaml - environment = buildEnv(); - - // sct 本身的额外的配饰信息 - bindObject("spring.cloud.tencent.metadata", localProperties, environment); - bindObject("spring.cloud.polaris", polarisContextProperties, environment); - staticMetadataManager = new StaticMetadataManager(localProperties, null); - - allowDiscovery = environment.getProperty("spring.cloud.discovery.enabled", Boolean.class, true); - // 服务发现配置 - discoveryProperties.setRegisterEnabled(environment.getProperty("spring.cloud.polaris.discovery.register", Boolean.class, true)); - discoveryProperties.setProtocol(environment.getProperty("spring.cloud.polaris.discovery.protocol", String.class, "http")); - discoveryProperties.setService(environment.getProperty("spring.application.name", String.class)); - discoveryProperties.setWeight(environment.getProperty("spring.cloud.polaris.discovery.weight", Integer.class, 100)); - String namespace = environment.getProperty("spring.cloud.polaris.namespace", String.class); - if (StringUtils.isBlank(namespace)) { - namespace = environment.getProperty("spring.cloud.polaris.discovery.namespace", String.class, "default"); - } - discoveryProperties.setNamespace(namespace); - - polarisContextProperties.setNamespace(namespace); - polarisContextProperties.setService(discoveryProperties.getService()); - bindObject("spring.cloud.consul", consulContextProperties, environment); - bindObject("spring.cloud.nacos.discovery", nacosContextProperties, environment); - - // 路由规则配置 - bindObject("spring.cloud.polaris.router.rule-router", routerProperties, environment); - 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); - - // 监控 - bindObject("spring.cloud.polaris.stat", polarisStatProperties, environment); - - bindObject("spring.cloud.polaris.lossless", losslessProperties, environment); - - // rpc 调用增强 - bindObject("spring.cloud.tencent.rpc-enhancement.reporter", rpcEnhancementReporterProperties, environment); - - runConfigModifiers(environment); - } catch (Throwable ex) { - throw new PolarisAgentException(ex); - } - } - - private static Environment buildEnv() throws Exception { - StandardEnvironment environment = new StandardEnvironment(); - HostInfoEnvironmentPostProcessor processor = new HostInfoEnvironmentPostProcessor(); - processor.postProcessEnvironment(environment, null); - - InputStream stream = Holder.class.getClassLoader().getResourceAsStream("default-plugin.conf"); - Properties defaultProperties = new Properties(); - defaultProperties.load(stream); - environment.getPropertySources() - .addFirst(new PropertiesPropertySource("__default_polaris_agent_spring_cloud_tencent__", defaultProperties)); - - Properties properties = new Properties(); - - String confPath = Paths.get(CONF_FILE_PATH, "plugin", "spring-cloud-hoxton", "application.properties").toString(); - String cmdVal = System.getProperty("polaris.agent.user.application.conf"); - if (StringUtils.isNotBlank(cmdVal)) { - confPath = cmdVal; - } - - properties.load(Files.newInputStream(Paths.get(confPath).toFile().toPath())); - environment.getPropertySources() - .addFirst(new PropertiesPropertySource("__polaris_agent_spring_cloud_tencent__", properties)); - - return environment; - } - - private static void bindObject(String prefix, Object bean, Environment environment) { - Binder binder = Binder.get(environment); - ResolvableType type = ResolvableType.forClass(bean.getClass()); - Bindable target = Bindable.of(type).withExistingValue(bean); - binder.bind(prefix, target); - } - - private static void runConfigModifiers(Environment environment) throws IOException { - - String logConfig = ""; - try { - Class.forName(PolarisLogging.LOGBACK_CLASSIC_LOGGER); - logConfig = Holder.class.getClassLoader().getResource("polaris-logback.xml").getFile(); - } catch (ClassNotFoundException e) { - try { - Class.forName(PolarisLogging.LOG4J2_CLASSIC_LOGGER); - logConfig = Holder.class.getClassLoader().getResource("polaris-log4j2.xml").getFile(); - } catch (ClassNotFoundException e1) { - logConfig = Holder.class.getClassLoader().getResource("polaris-log4j.xml").getFile(); - } - } - System.setProperty(LoggingConsts.LOGGING_CONFIG_PROPERTY, "jar:" + logConfig); - - if (StringUtils.isBlank(polarisContextProperties.getLocalIpAddress())) { - polarisContextProperties.setLocalIpAddress(environment.getProperty("spring.cloud.client.ip-address")); - } - - List modifiers = new ArrayList<>(Arrays.asList( - new ModifyAddress(polarisContextProperties), - new DiscoveryConfigModifier(discoveryProperties), - new PolarisDiscoveryConfigModifier(discoveryProperties), - // new RouterConfigModifier(nearByRouterProperties), - //new RateLimitConfigModifier(rateLimitProperties), - new StatConfigModifier(polarisStatProperties, environment) - //new CircuitBreakerConfigModifier(rpcEnhancementReporterProperties), - )); - 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)); - } - - contextManager = new PolarisSDKContextManager(polarisContextProperties, environment, modifiers); - contextManager.init(); - } - - private static String getLocalService() { - String serviceId = ""; - if (Objects.isNull(nacosContextProperties)) { - serviceId = discoveryProperties.getService(); - } else { - String group = nacosContextProperties.getGroup(); - if (org.apache.commons.lang.StringUtils.isNotBlank(group) && !DEFAULT_GROUP.equals(group)) { - serviceId = String.format(GROUP_SERVER_ID_FORMAT, group, discoveryProperties.getService()); - } else { - serviceId = discoveryProperties.getService(); - } - } - return serviceId; - } - - public static Environment getEnvironment() { - return environment; - } - - public static PolarisStatProperties getPolarisStatProperties() { - return polarisStatProperties; - } - - public static PolarisSDKContextManager getContextManager() { - return contextManager; - } - - public static MetadataLocalProperties getLocalProperties() { - return localProperties; - } - - public static StaticMetadataManager getStaticMetadataManager() { - return staticMetadataManager; - } - - public static PolarisContextProperties getPolarisContextProperties() { - return polarisContextProperties; - } - - public static PolarisDiscoveryProperties getDiscoveryProperties() { - return discoveryProperties; - } - - public static PolarisRuleBasedRouterProperties getRouterProperties() { - return routerProperties; - } - - public static PolarisNearByRouterProperties getNearByRouterProperties() { - return nearByRouterProperties; - } - - public static PolarisMetadataRouterProperties getMetadataRouterProperties() { - return metadataRouterProperties; - } - - public static ConsulContextProperties getConsulContextProperties() { - return consulContextProperties; - } - - public static NacosContextProperties getNacosContextProperties() { - return nacosContextProperties; - } - - public static RpcEnhancementReporterProperties getRpcEnhancementReporterProperties() { - return rpcEnhancementReporterProperties; - } - - public static PolarisConfigProperties getPolarisConfigProperties() { - return polarisConfigProperties; - } - - public static PolarisCryptoConfigProperties getPolarisCryptoConfigProperties() { - return polarisCryptoConfigProperties; - } - - public static ServiceRuleManager newServiceRuleManager() { - return new ServiceRuleManager(getContextManager().getSDKContext(), getContextManager().getConsumerAPI()); - } - - public static boolean isAllowDiscovery() { - return allowDiscovery; - } - - public static LosslessProperties getLosslessProperties() { - return losslessProperties; - } - - public static PolarisPropertySourceManager getPolarisPropertySourceManager() { - return polarisPropertySourceManager; - } -} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java new file mode 100644 index 00000000..34c067fc --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java @@ -0,0 +1,54 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration; +import com.tencent.cloud.common.metadata.endpoint.PolarisMetadataEndpointAutoConfiguration; +import com.tencent.cloud.common.util.ApplicationContextAwareUtils; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class CommonBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object applicationContextAwareUtils = ReflectionUtils.invokeConstructor(configClassCreator, ApplicationContextAwareUtils.class, "applicationContextAwareUtils"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, applicationContextAwareUtils, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("applicationContextAwareUtils", BeanDefinitionBuilder.genericBeanDefinition( + ApplicationContextAwareUtils.class).getBeanDefinition()); + Object metadataAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, MetadataAutoConfiguration.class, "metadataAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, metadataAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("metadataAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + MetadataAutoConfiguration.class).getBeanDefinition()); + Object polarisMetadataEndpointAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisMetadataEndpointAutoConfiguration.class, "polarisMetadataEndpointAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisMetadataEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisMetadataEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisMetadataEndpointAutoConfiguration.class).getBeanDefinition()); + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java new file mode 100644 index 00000000..b1af247e --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java @@ -0,0 +1,37 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; + +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class ConfigBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java new file mode 100644 index 00000000..f3e6f2c7 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java @@ -0,0 +1,44 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class LoadbalancerBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object polarisLoadBalancerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisLoadBalancerAutoConfiguration.class, "polarisLoadBalancerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisLoadBalancerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisLoadBalancerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisLoadBalancerAutoConfiguration.class).getBeanDefinition()); + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java new file mode 100644 index 00000000..fcce305d --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java @@ -0,0 +1,53 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration; +import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesAutoConfiguration; +import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class LosslessBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object losslessPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessPropertiesBootstrapConfiguration.class, "losslessPropertiesBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, losslessPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("losslessPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + LosslessPropertiesBootstrapConfiguration.class).getBeanDefinition()); + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object losslessPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessPropertiesAutoConfiguration.class, "losslessPropertiesAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, losslessPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("losslessPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + LosslessPropertiesAutoConfiguration.class).getBeanDefinition()); + Object losslessAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessAutoConfiguration.class, "losslessAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, losslessAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("losslessAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + LosslessAutoConfiguration.class).getBeanDefinition()); + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java new file mode 100644 index 00000000..af3d4d4c --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java @@ -0,0 +1,62 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextBootstrapAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration; +import com.tencent.cloud.polaris.context.logging.PolarisLoggingApplicationListener; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class PolarisContextBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object polarisContextBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisContextBootstrapAutoConfiguration.class, "polarisContextBootstrapAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisContextBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisContextBootstrapAutoConfiguration.class).getBeanDefinition()); + + + } + + @Override + public void onApplicationStartup(Object configurationParser, + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object polarisContextAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisContextAutoConfiguration.class, "polarisContextAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisContextAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisContextAutoConfiguration.class).getBeanDefinition()); + Object polarisContextPostConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisContextPostConfiguration.class, "polarisContextPostConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextPostConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisContextPostConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisContextPostConfiguration.class).getBeanDefinition()); + Object polarisLoggingApplicationListener = ReflectionUtils.invokeConstructor(configClassCreator, PolarisLoggingApplicationListener.class, "polarisLoggingApplicationListener"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisLoggingApplicationListener, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisLoggingApplicationListener", BeanDefinitionBuilder.genericBeanDefinition( + PolarisLoggingApplicationListener.class).getBeanDefinition()); + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java new file mode 100644 index 00000000..64318867 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java @@ -0,0 +1,37 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; + +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class RegistryBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java new file mode 100644 index 00000000..5b9977ac --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java @@ -0,0 +1,37 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; + +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class RouterBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java new file mode 100644 index 00000000..ad22e484 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java @@ -0,0 +1,58 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration; +import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementBootstrapConfiguration; +import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration; +import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class RpcEnhancementBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object polarisStatPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesBootstrapConfiguration.class, "polarisStatPropertiesBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisStatPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisStatPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisStatPropertiesAutoConfiguration.class).getBeanDefinition()); + Object rpcEnhancementBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RpcEnhancementBootstrapConfiguration.class, "rpcEnhancementBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, rpcEnhancementBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("rpcEnhancementBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + RpcEnhancementBootstrapConfiguration.class).getBeanDefinition()); + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object polarisStatPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesAutoConfiguration.class, "polarisStatPropertiesAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisStatPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisStatPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisStatPropertiesBootstrapConfiguration.class).getBeanDefinition()); + Object rpcEnhancementAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RpcEnhancementAutoConfiguration.class, "rpcEnhancementAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, rpcEnhancementAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("rpcEnhancementAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + RpcEnhancementAutoConfiguration.class).getBeanDefinition()); + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java index 0117e432..b7ce4e13 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -20,34 +20,30 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; import java.util.Set; import java.util.function.Predicate; import cn.polarismesh.agent.core.common.utils.ClassUtils; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.core.extension.interceptor.Interceptor; -import com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration; -import com.tencent.cloud.common.util.ApplicationContextAwareUtils; -import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration; -import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesAutoConfiguration; -import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.inject.CommonBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.LoadbalancerBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.LosslessBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.PolarisContextBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.RpcEnhancementBeanInjector; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; -import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; -import com.tencent.cloud.polaris.context.config.PolarisContextBootstrapAutoConfiguration; -import com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; -import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration; import com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration; import com.tencent.cloud.polaris.router.config.FeignAutoConfiguration; import com.tencent.cloud.polaris.router.config.RouterAutoConfiguration; -import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration; -import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementBootstrapConfiguration; -import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration; -import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration; import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; @@ -57,8 +53,16 @@ public class ConfigurationParserInterceptor implements Interceptor { - private static final Predicate DEFAULT_EXCLUSION_FILTER = className -> - (className.startsWith("java.lang.annotation.") || className.startsWith("org.springframework.stereotype.")); + private final List beanInjectors = new ArrayList<>(); + + public ConfigurationParserInterceptor() { + beanInjectors.add(new CommonBeanInjector()); + beanInjectors.add(new PolarisContextBeanInjector()); + beanInjectors.add(new RpcEnhancementBeanInjector()); + beanInjectors.add(new LosslessBeanInjector()); + beanInjectors.add(new LoadbalancerBeanInjector()); + } + private static boolean isMainBeanDefinition(BeanDefinitionHolder beanDefinitionHolder) { BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition(); @@ -92,40 +96,24 @@ public void after(Object target, Object[] args, Object result, Throwable throwab BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry"); - Object polarisContextBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisContextBootstrapAutoConfiguration.class, "polarisContextBootstrapAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisContextBootstrapAutoConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisContextBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisContextBootstrapAutoConfiguration.class).getBeanDefinition()); + for (BeanInjector beanInjector : beanInjectors) { + beanInjector.onBootstrapStartup(target, constructor, processConfigurationClass, registry); + } // rpc - Object polarisStatPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisStatPropertiesBootstrapConfiguration.class, "polarisStatPropertiesBootstrapConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisStatPropertiesBootstrapConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisStatPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisStatPropertiesAutoConfiguration.class).getBeanDefinition()); - Object rpcEnhancementBootstrapConfiguration = ReflectionUtils.invokeConstructor(constructor, RpcEnhancementBootstrapConfiguration.class, "rpcEnhancementBootstrapConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, rpcEnhancementBootstrapConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("rpcEnhancementBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - RpcEnhancementBootstrapConfiguration.class).getBeanDefinition()); + // lossless - Object losslessPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, LosslessPropertiesAutoConfiguration.class, "losslessPropertiesAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, losslessPropertiesAutoConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("losslessPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - LosslessPropertiesAutoConfiguration.class).getBeanDefinition()); - Object losslessPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(constructor, LosslessPropertiesBootstrapConfiguration.class, "losslessPropertiesBootstrapConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, losslessPropertiesBootstrapConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("losslessPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - LosslessPropertiesBootstrapConfiguration.class).getBeanDefinition()); // register Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesBootstrapAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("discoveryPropertiesBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( DiscoveryPropertiesBootstrapAutoConfiguration.class).getBeanDefinition()); // config Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigBootstrapAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisConfigBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisConfigBootstrapAutoConfiguration.class).getBeanDefinition()); @@ -137,82 +125,52 @@ public void after(Object target, Object[] args, Object result, Throwable throwab BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry"); - // sct common - Object applicationContextAwareUtils = ReflectionUtils.invokeConstructor(constructor, ApplicationContextAwareUtils.class, "applicationContextAwareUtils"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, applicationContextAwareUtils, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("applicationContextAwareUtils", BeanDefinitionBuilder.genericBeanDefinition( - ApplicationContextAwareUtils.class).getBeanDefinition()); - Object metadataAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, MetadataAutoConfiguration.class, "metadataAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, metadataAutoConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("metadataAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - MetadataAutoConfiguration.class).getBeanDefinition()); - - // polaris-context - Object polarisContextAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisContextAutoConfiguration.class, "polarisContextAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisContextAutoConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisContextAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisContextAutoConfiguration.class).getBeanDefinition()); - Object polarisContextPostConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisContextPostConfiguration.class, "polarisContextPostConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisContextPostConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisContextPostConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisContextPostConfiguration.class).getBeanDefinition()); + for (BeanInjector beanInjector : beanInjectors) { + beanInjector.onApplicationStartup(target, constructor, processConfigurationClass, registry); + } + // rpc - Object polarisStatPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisStatPropertiesAutoConfiguration.class, "polarisStatPropertiesAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisStatPropertiesAutoConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisStatPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisStatPropertiesBootstrapConfiguration.class).getBeanDefinition()); - Object rpcEnhancementAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, RpcEnhancementAutoConfiguration.class, "rpcEnhancementAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, rpcEnhancementAutoConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("rpcEnhancementAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - RpcEnhancementAutoConfiguration.class).getBeanDefinition()); + // lossless - Object losslessAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, LosslessAutoConfiguration.class, "losslessAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, losslessAutoConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("losslessAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - LosslessAutoConfiguration.class).getBeanDefinition()); // loadbalancer - Object polarisLoadBalancerAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisLoadBalancerAutoConfiguration.class, "polarisLoadBalancerAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisLoadBalancerAutoConfiguration, DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisLoadBalancerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisLoadBalancerAutoConfiguration.class).getBeanDefinition()); // router Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, RouterAutoConfiguration.class, "routerAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, routerAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, routerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("routerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( RouterAutoConfiguration.class).getBeanDefinition()); if (null != ClassUtils.getClazz("feign.RequestInterceptor", Thread.currentThread().getContextClassLoader())) { Object feignAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, FeignAutoConfiguration.class, "feignAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, feignAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, feignAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("feignAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( FeignAutoConfiguration.class).getBeanDefinition()); } // registry Object discoveryPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesAutoConfiguration.class, "discoveryPropertiesAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("discoveryPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( DiscoveryPropertiesAutoConfiguration.class).getBeanDefinition()); Object polarisDiscoveryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryAutoConfiguration.class, "polarisDiscoveryAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisDiscoveryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisDiscoveryAutoConfiguration.class).getBeanDefinition()); Object polarisDiscoveryRibbonAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryRibbonAutoConfiguration.class, "polarisDiscoveryRibbonAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryRibbonAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryRibbonAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisDiscoveryRibbonAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisDiscoveryRibbonAutoConfiguration.class).getBeanDefinition()); Object polarisServiceRegistryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisServiceRegistryAutoConfiguration.class, "polarisServiceRegistryAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisServiceRegistryAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisServiceRegistryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisServiceRegistryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisServiceRegistryAutoConfiguration.class).getBeanDefinition()); // config Object polarisConfigAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigAutoConfiguration.class, "polarisConfigAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigAutoConfiguration, DEFAULT_EXCLUSION_FILTER); + ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisConfigAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisConfigAutoConfiguration.class).getBeanDefinition()); } From 1604631e3d0de0e447631de24495f4b050f929a0 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Tue, 7 May 2024 20:44:51 +0800 Subject: [PATCH 11/24] feat: add gray release example & dispatch bean register into plugins --- .../gray-release-examples/pom.xml | 23 +++++ .../service-a/Dockerfile | 28 ++++++ .../service-a/deployment.yaml | 49 +++++++++ .../gray-release-examples/service-a/pom.xml | 87 ++++++++++++++++ .../cloud/QuickstartCalleeService.java | 41 ++++++++ .../examples/alibaba/cloud/ServiceA.java | 92 +++++++++++++++++ .../src/main/resources/application.yaml | 27 +++++ .../service-b/Dockerfile | 28 ++++++ .../service-b/deployment.yaml | 49 +++++++++ .../gray-release-examples/service-b/pom.xml | 85 ++++++++++++++++ .../cloud/QuickstartCalleeService.java | 41 ++++++++ .../examples/alibaba/cloud/ServiceB.java | 89 +++++++++++++++++ .../src/main/resources/application.yaml | 30 ++++++ .../service-c/Dockerfile | 28 ++++++ .../service-c/deployment.yaml | 49 +++++++++ .../gray-release-examples/service-c/pom.xml | 80 +++++++++++++++ .../examples/alibaba/cloud/ServiceC.java | 78 +++++++++++++++ .../src/main/resources/application.yaml | 30 ++++++ .../service-gateway/Dockerfile | 28 ++++++ .../service-gateway/deployment.yaml | 49 +++++++++ .../service-gateway/pom.xml | 58 +++++++++++ .../alibaba/cloud/ServiceGateway.java | 29 ++++++ .../src/main/resources/application.yaml | 36 +++++++ .../spring-cloud-hoxton-examples/pom.xml | 1 + .../inject/CircuitBreakerBeanInjector.java | 62 ++++++++++++ .../cloud/inject/ConfigBeanInjector.java | 21 +++- .../inject/PolarisContextBeanInjector.java | 5 + .../cloud/inject/RateLimitBeanInjector.java | 41 ++++++++ .../cloud/inject/RegistryBeanInjector.java | 44 ++++++++- .../cloud/inject/RouterBeanInjector.java | 23 ++++- .../ConfigurationParserInterceptor.java | 99 +++++++++---------- 31 files changed, 1374 insertions(+), 56 deletions(-) create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/pom.xml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/Dockerfile create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/deployment.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/QuickstartCalleeService.java create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceA.java create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/Dockerfile create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/deployment.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/pom.xml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/QuickstartCalleeService.java create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/Dockerfile create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/deployment.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/pom.xml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/Dockerfile create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/deployment.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceGateway.java create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/pom.xml new file mode 100644 index 00000000..7e270aea --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/pom.xml @@ -0,0 +1,23 @@ + + + + spring-cloud-hoxton-examples + com.tencent.polaris + ${revision} + ../pom.xml + + 4.0.0 + + hoxton-gray-release-examples + pom + + service-a + service-b + service-c + service-gateway + + + + \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/Dockerfile b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/Dockerfile new file mode 100644 index 00000000..f8284a09 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/Dockerfile @@ -0,0 +1,28 @@ +FROM alpine:3.19.1 + +ARG file_name + +COPY ./target/${file_name} /app/main.jar + +WORKDIR /app + +RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories + +RUN set -eux && \ + apk add openjdk17 && \ + apk add bind-tools && \ + apk add busybox-extras && \ + apk add findutils && \ + apk add tcpdump && \ + apk add tzdata && \ + apk add curl && \ + apk add bash && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone && \ + date + +RUN chmod 777 /app/ + +RUN ls -la /app/ + +ENTRYPOINT ["java", "-jar", "/app/main.jar"] \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/deployment.yaml new file mode 100644 index 00000000..a8b7a25d --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/deployment.yaml @@ -0,0 +1,49 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: service-a-2023 + name: service-a-2023 + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: service-a-2023 + template: + metadata: + labels: + app: service-a-2023 + annotations: + polarismesh.cn/javaagent: "true" + 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\"}" + spec: + containers: + - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-a:1.7.0-RC.0-java17 + imagePullPolicy: Always + name: consumer + resources: + limits: + cpu: "500m" + memory: 1000Mi + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + command: + - /bin/bash + - -c + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar + lifecycle: + preStop: + exec: + command: ["curl","-X","PUT","http://127.0.0.1:28080/offline","&&","sleep","30"] + readinessProbe: + httpGet: + path: /online + port: 28080 + initialDelaySeconds: 3 + periodSeconds: 3 + restartPolicy: Always \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml new file mode 100644 index 00000000..dff8fe67 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml @@ -0,0 +1,87 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.3.7.RELEASE + + + 4.0.0 + + 2023-service-a + Demo Service-A Project For Spring Cloud Alibaba + polaris-javaagent-demo-sc-gray-hoxton-service-a + jar + + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2.2.1.RELEASE + pom + import + + + + org.springframework.cloud + spring-cloud-dependencies + Hoxton.SR12 + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + repackage + + + + + + + + \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/QuickstartCalleeService.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/QuickstartCalleeService.java new file mode 100644 index 00000000..eb6b3b38 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/QuickstartCalleeService.java @@ -0,0 +1,41 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent 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.examples.alibaba.cloud; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * Quickstart callee feign client. + * + * @author Haotian Zhang + */ +@FeignClient(value = "service-b-hoxton") +public interface QuickstartCalleeService { + + /** + * Get sum of two value. + * + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/sum") + String sum(@RequestParam("value1") int value1, @RequestParam("value2") int value2); +} diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceA.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceA.java new file mode 100644 index 00000000..bb7c5971 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceA.java @@ -0,0 +1,92 @@ +/* + * 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.examples.alibaba.cloud; + +import java.util.Collections; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Bean; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +/** + * @author liaochuntao + */ +@SpringBootApplication +@EnableFeignClients +public class ServiceA { + + public static void main(String[] args) { + SpringApplication.run(ServiceA.class, args); + } + + @LoadBalanced + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + @RestController + public static class EchoController { + + private Registration registration; + + private RestTemplate template; + + @Autowired + private QuickstartCalleeService quickstartCalleeService; + + @Value("${spring.cloud.tencent.metadata.content.lane:base}") + private String lane; + + public EchoController(RestTemplate restTemplate, Registration registration) { + this.template = restTemplate; + // this.registration = registration; + } + + + @GetMapping("/echo") + public String echo() { + String content = String.format("[%s] -> ", lane); + String resp = template.getForObject("http://service-b-hoxton/echo", String.class); + content += resp; + return content; + } + + @GetMapping("/sum") + public String sum(@RequestParam("value1") int value1, @RequestParam int value2) { + String content = String.format("[%s] -> ", lane); + String resp = quickstartCalleeService.sum(value1, value2); + content += resp; + return content; + } + + + + } + +} diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml new file mode 100644 index 00000000..31fffe33 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml @@ -0,0 +1,27 @@ +server: + port: 65203 + +spring: + application: + name: service-a-hoxton + cloud: + nacos: + discovery: + enabled: true + server-addr: 127.0.0.1:8848 + config: + enabled: false + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/Dockerfile b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/Dockerfile new file mode 100644 index 00000000..f8284a09 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/Dockerfile @@ -0,0 +1,28 @@ +FROM alpine:3.19.1 + +ARG file_name + +COPY ./target/${file_name} /app/main.jar + +WORKDIR /app + +RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories + +RUN set -eux && \ + apk add openjdk17 && \ + apk add bind-tools && \ + apk add busybox-extras && \ + apk add findutils && \ + apk add tcpdump && \ + apk add tzdata && \ + apk add curl && \ + apk add bash && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone && \ + date + +RUN chmod 777 /app/ + +RUN ls -la /app/ + +ENTRYPOINT ["java", "-jar", "/app/main.jar"] \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/deployment.yaml new file mode 100644 index 00000000..ab008070 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/deployment.yaml @@ -0,0 +1,49 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: service-b-2023 + name: service-b-2023 + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: service-b-2023 + template: + metadata: + labels: + app: service-b-2023 + annotations: + polarismesh.cn/javaagent: "true" + 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\"}" + spec: + containers: + - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-b:1.7.0-RC.0-java17 + imagePullPolicy: Always + name: consumer + resources: + limits: + cpu: "500m" + memory: 1000Mi + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + command: + - /bin/bash + - -c + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar + lifecycle: + preStop: + exec: + command: ["curl","-X","PUT","http://127.0.0.1:28080/offline","&&","sleep","30"] + readinessProbe: + httpGet: + path: /online + port: 28080 + initialDelaySeconds: 3 + periodSeconds: 3 + restartPolicy: Always \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/pom.xml new file mode 100644 index 00000000..e49f5a2e --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/pom.xml @@ -0,0 +1,85 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.3.7.RELEASE + + + 4.0.0 + + 2023-service-b + Demo Service-B Project For Spring Cloud Alibaba + polaris-javaagent-demo-sc-gray-hoxton-service-b + jar + + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2.2.1.RELEASE + pom + import + + + + org.springframework.cloud + spring-cloud-dependencies + Hoxton.SR12 + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + repackage + + + + + + + + \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/QuickstartCalleeService.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/QuickstartCalleeService.java new file mode 100644 index 00000000..2c3fc33d --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/QuickstartCalleeService.java @@ -0,0 +1,41 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent 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.examples.alibaba.cloud; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * Quickstart callee feign client. + * + * @author Haotian Zhang + */ +@FeignClient(value = "service-c-hoxton") +public interface QuickstartCalleeService { + + /** + * Get sum of two value. + * + * @param value1 value 1 + * @param value2 value 2 + * @return sum + */ + @GetMapping("/sum") + String sum(@RequestParam("value1") int value1, @RequestParam("value2") int value2); +} diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java new file mode 100644 index 00000000..06d7b3e0 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java @@ -0,0 +1,89 @@ +/* + * 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.examples.alibaba.cloud; + +import java.util.Collections; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.Bean; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +/** + * @author liaochuntao + */ +@SpringBootApplication +@EnableFeignClients +public class ServiceB { + + public static void main(String[] args) { + SpringApplication.run(ServiceB.class, args); + } + + @LoadBalanced + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + @RestController + public static class EchoController { + + private Registration registration; + + private RestTemplate restTemplate; + + @Autowired + private QuickstartCalleeService quickstartCalleeService; + + @Value("${spring.cloud.tencent.metadata.content.lane:base}") + private String lane; + + public EchoController(RestTemplate restTemplate, Registration registration) { + this.restTemplate = restTemplate; + // this.registration = registration; + } + + + @GetMapping("/echo") + public String echo() { + String content = String.format("[%s] -> ", lane); + String resp = restTemplate.getForObject("http://service-c-hoxton/echo", String.class); + content += resp; + return content; + } + + @GetMapping("/sum") + public String sum(@RequestParam("value1") int value1, @RequestParam int value2) { + String content = String.format("[%s] -> ", lane); + String resp = quickstartCalleeService.sum(value1, value2); + content += resp; + return content; + } + } + +} diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.yaml new file mode 100644 index 00000000..e24c672f --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.yaml @@ -0,0 +1,30 @@ +server: + port: 65202 + +spring: + application: + name: service-b-hoxton + cloud: + nacos: + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: false + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:service-b-hoxton.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/Dockerfile b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/Dockerfile new file mode 100644 index 00000000..f8284a09 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/Dockerfile @@ -0,0 +1,28 @@ +FROM alpine:3.19.1 + +ARG file_name + +COPY ./target/${file_name} /app/main.jar + +WORKDIR /app + +RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories + +RUN set -eux && \ + apk add openjdk17 && \ + apk add bind-tools && \ + apk add busybox-extras && \ + apk add findutils && \ + apk add tcpdump && \ + apk add tzdata && \ + apk add curl && \ + apk add bash && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone && \ + date + +RUN chmod 777 /app/ + +RUN ls -la /app/ + +ENTRYPOINT ["java", "-jar", "/app/main.jar"] \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/deployment.yaml new file mode 100644 index 00000000..d4a3dd56 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/deployment.yaml @@ -0,0 +1,49 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: service-c-2023 + name: service-c-2023 + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: service-c-2023 + template: + metadata: + labels: + app: service-c-2023 + annotations: + polarismesh.cn/javaagent: "true" + 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\"}" + spec: + containers: + - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-c:1.7.0-RC.0-java17 + imagePullPolicy: Always + name: consumer + resources: + limits: + cpu: "500m" + memory: 1000Mi + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + command: + - /bin/bash + - -c + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar + lifecycle: + preStop: + exec: + command: ["curl","-X","PUT","http://127.0.0.1:28080/offline","&&","sleep","30"] + readinessProbe: + httpGet: + path: /online + port: 28080 + initialDelaySeconds: 3 + periodSeconds: 3 + restartPolicy: Always \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/pom.xml new file mode 100644 index 00000000..3fb15b85 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/pom.xml @@ -0,0 +1,80 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.3.7.RELEASE + + + 4.0.0 + + hoxton-service-c + Demo Service-C Project For Spring Cloud Alibaba + polaris-javaagent-demo-sc-gray-hoxton-service-c + jar + + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2.2.1.RELEASE + pom + import + + + + org.springframework.cloud + spring-cloud-dependencies + Hoxton.SR12 + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + repackage + + + + + + + + \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java new file mode 100644 index 00000000..0bf361df --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java @@ -0,0 +1,78 @@ +/* + * 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.examples.alibaba.cloud; + +import java.util.Collections; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.context.annotation.Bean; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +/** + * @author liaochuntao + */ +@SpringBootApplication +public class ServiceC { + + public static void main(String[] args) { + SpringApplication.run(ServiceC.class, args); + } + + @LoadBalanced + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + @RestController + public static class EchoController { + + private Registration registration; + + @Value("${spring.cloud.tencent.metadata.content.lane:base}") + private String lane; + + // public EchoController(Registration registration) { + // this.registration = registration; + // } + + + @GetMapping("/echo") + public String echo() { + return String.format("[%s]", lane); + } + + @GetMapping("/sum") + public String sum(@RequestParam("value1") int value1, @RequestParam int value2) { + String content = String.format("[%s] -> ", lane); + String resp = String.valueOf(value1 + value2); + content += resp; + return content; + } + + } + +} diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.yaml new file mode 100644 index 00000000..474f01fa --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.yaml @@ -0,0 +1,30 @@ +server: + port: 65201 + +spring: + application: + name: service-c-hoxton + cloud: + nacos: + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: false + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:service-c-hoxton.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/Dockerfile b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/Dockerfile new file mode 100644 index 00000000..f8284a09 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/Dockerfile @@ -0,0 +1,28 @@ +FROM alpine:3.19.1 + +ARG file_name + +COPY ./target/${file_name} /app/main.jar + +WORKDIR /app + +RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories + +RUN set -eux && \ + apk add openjdk17 && \ + apk add bind-tools && \ + apk add busybox-extras && \ + apk add findutils && \ + apk add tcpdump && \ + apk add tzdata && \ + apk add curl && \ + apk add bash && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone && \ + date + +RUN chmod 777 /app/ + +RUN ls -la /app/ + +ENTRYPOINT ["java", "-jar", "/app/main.jar"] \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/deployment.yaml new file mode 100644 index 00000000..a9062581 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/deployment.yaml @@ -0,0 +1,49 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: sc-gateway-2023 + name: sc-gateway-2023 + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: sc-gateway-2023 + template: + metadata: + labels: + app: sc-gateway-2023 + annotations: + polarismesh.cn/javaagent: "true" + 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\"}" + spec: + containers: + - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-gateway:1.7.0-RC.0-java17 + imagePullPolicy: Always + name: consumer + resources: + limits: + cpu: "500m" + memory: 1000Mi + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + command: + - /bin/bash + - -c + - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar + lifecycle: + preStop: + exec: + command: ["curl","-X","PUT","http://127.0.0.1:28080/offline","&&","sleep","30"] + readinessProbe: + httpGet: + path: /online + port: 28080 + initialDelaySeconds: 3 + periodSeconds: 3 + restartPolicy: Always \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml new file mode 100644 index 00000000..0e222355 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml @@ -0,0 +1,58 @@ + + + org.springframework.boot + spring-boot-starter-parent + 2.3.7.RELEASE + + + 4.0.0 + polaris-javaagent-demo-sc-gray-hoxton-service-gateway + hoxton-service-gateway + Demo Service-Gateway Project For Spring Cloud Alibaba 2023 + jar + + + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + 2.2.1.RELEASE + pom + import + + + + org.springframework.cloud + spring-cloud-dependencies + 2023.0.0 + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + org.springframework.boot + spring-boot-starter-actuator + + + + diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceGateway.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceGateway.java new file mode 100644 index 00000000..14a7242b --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceGateway.java @@ -0,0 +1,29 @@ +/* + * 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.examples.alibaba.cloud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ServiceGateway { + + public static void main(String[] args) { + SpringApplication.run(ServiceGateway.class, args); + } +} diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml new file mode 100644 index 00000000..fafe39cb --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml @@ -0,0 +1,36 @@ +server: + port: 65200 + +spring: + application: + name: sc-gateway-hoxton + cloud: + gateway: + discovery: + locator: + enabled: true + nacos: + username: 'nacos' + password: 'nacos' + discovery: + enabled: false + server-addr: 127.0.0.1:8848 + config: + enabled: true + serverAddr: 127.0.0.1:8848 + username: 'nacos' + password: 'nacos' + config: + import: + - nacos:sc-gateway-2023.properties?refreshEnabled=true +management: + endpoint: + health: + show-details: always + endpoints: + web: + exposure: + include: '*' +logging: + level: + com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/pom.xml index 2e35f0fa..ade6f974 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/pom.xml @@ -16,6 +16,7 @@ quickstart-examples + gray-release-examples \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java new file mode 100644 index 00000000..09c2ef17 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java @@ -0,0 +1,62 @@ +package cn.polarismesh.agent.plugin.spring.cloud.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerAutoConfiguration; +import com.tencent.cloud.polaris.circuitbreaker.config.ReactivePolarisCircuitBreakerAutoConfiguration; +import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerFeignClientAutoConfiguration; +import com.tencent.cloud.polaris.circuitbreaker.config.GatewayPolarisCircuitBreakerAutoConfiguration; +import org.springframework.cloud.openfeign.PolarisFeignCircuitBreakerTargeterAutoConfiguration; +import com.tencent.cloud.polaris.circuitbreaker.endpoint.PolarisCircuitBreakerEndpointAutoConfiguration; +import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerBootstrapConfiguration; + +public class CircuitBreakerBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + + Object polarisCircuitBreakerBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerBootstrapConfiguration.class, "polarisCircuitBreakerBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisCircuitBreakerBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisCircuitBreakerBootstrapConfiguration.class).getBeanDefinition()); +// Object polarisFeignCircuitBreakerTargeterAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisFeignCircuitBreakerTargeterAutoConfiguration.class, "polarisFeignCircuitBreakerTargeterAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisFeignCircuitBreakerTargeterAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisFeignCircuitBreakerTargeterAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisFeignCircuitBreakerTargeterAutoConfiguration.class).getBeanDefinition()); + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object polarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerAutoConfiguration.class, "polarisCircuitBreakerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); + Object reactivePolarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, ReactivePolarisCircuitBreakerAutoConfiguration.class, "reactivePolarisCircuitBreakerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, reactivePolarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("reactivePolarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + ReactivePolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); +// Object polarisCircuitBreakerFeignClientAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerFeignClientAutoConfiguration.class, "polarisCircuitBreakerFeignClientAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerFeignClientAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisCircuitBreakerFeignClientAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisCircuitBreakerFeignClientAutoConfiguration.class).getBeanDefinition()); +// Object gatewayPolarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, GatewayPolarisCircuitBreakerAutoConfiguration.class, "gatewayPolarisCircuitBreakerAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, gatewayPolarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("gatewayPolarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// GatewayPolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); +// Object polarisFeignCircuitBreakerTargeterAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisFeignCircuitBreakerTargeterAutoConfiguration.class, "polarisFeignCircuitBreakerTargeterAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisFeignCircuitBreakerTargeterAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisFeignCircuitBreakerTargeterAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisFeignCircuitBreakerTargeterAutoConfiguration.class).getBeanDefinition()); + Object polarisCircuitBreakerEndpointAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerEndpointAutoConfiguration.class, "polarisCircuitBreakerEndpointAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisCircuitBreakerEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisCircuitBreakerEndpointAutoConfiguration.class).getBeanDefinition()); + } +} + diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java index b1af247e..f91e0d2c 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java @@ -20,18 +20,35 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; +import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; +import com.tencent.cloud.polaris.config.endpoint.PolarisConfigEndpointAutoConfiguration; public class ConfigBeanInjector implements BeanInjector { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { - + Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisConfigBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisConfigBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisConfigBootstrapAutoConfiguration.class).getBeanDefinition()); } @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { - + Object polarisConfigAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigAutoConfiguration.class, "polarisConfigAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisConfigAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisConfigAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisConfigAutoConfiguration.class).getBeanDefinition()); + Object polarisConfigEndpointAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigEndpointAutoConfiguration.class, "polarisConfigEndpointAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisConfigEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisConfigEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisConfigEndpointAutoConfiguration.class).getBeanDefinition()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java index af3d4d4c..c92ad763 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java @@ -20,6 +20,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; @@ -58,5 +59,9 @@ public void onApplicationStartup(Object configurationParser, ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisLoggingApplicationListener, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisLoggingApplicationListener", BeanDefinitionBuilder.genericBeanDefinition( PolarisLoggingApplicationListener.class).getBeanDefinition()); +// Object polarisSDKContextManager = ReflectionUtils.invokeConstructor(configClassCreator, PolarisSDKContextManager.class, "polarisSDKContextManager"); +// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisSDKContextManager, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisSDKContextManager", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisSDKContextManager.class).getBeanDefinition()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java new file mode 100644 index 00000000..03806a3a --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java @@ -0,0 +1,41 @@ +package cn.polarismesh.agent.plugin.spring.cloud.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitAutoConfiguration; +import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesAutoConfiguration; +import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesBootstrapConfiguration; +import com.tencent.cloud.polaris.ratelimit.endpoint.PolarisRateLimitRuleEndpointAutoConfiguration; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; + +public class RateLimitBeanInjector implements BeanInjector { + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object polarisRateLimitPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitPropertiesBootstrapConfiguration.class, "polarisRateLimitPropertiesBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisRateLimitPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisRateLimitPropertiesBootstrapConfiguration.class).getBeanDefinition()); + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Object polarisRateLimitAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitAutoConfiguration.class, "polarisRateLimitAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisRateLimitAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisRateLimitAutoConfiguration.class).getBeanDefinition()); + Object polarisRateLimitPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitPropertiesAutoConfiguration.class, "polarisRateLimitPropertiesAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisRateLimitPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisRateLimitPropertiesAutoConfiguration.class).getBeanDefinition()); + Object polarisRateLimitRuleEndpointAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitRuleEndpointAutoConfiguration.class, "polarisRateLimitRuleEndpointAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitRuleEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisRateLimitRuleEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisRateLimitRuleEndpointAutoConfiguration.class).getBeanDefinition()); + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java index 64318867..b75d4333 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java @@ -20,18 +20,56 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; +import com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration; +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; +import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; +import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration; +import com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration; +import com.tencent.cloud.polaris.endpoint.PolarisDiscoveryEndpointAutoConfiguration; +import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration; +import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; public class RegistryBeanInjector implements BeanInjector { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { - + Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, discoveryPropertiesBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("discoveryPropertiesBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + DiscoveryPropertiesBootstrapAutoConfiguration.class).getBeanDefinition()); } @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { - + Object discoveryPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, DiscoveryPropertiesAutoConfiguration.class, "discoveryPropertiesAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, discoveryPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("discoveryPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + DiscoveryPropertiesAutoConfiguration.class).getBeanDefinition()); + Object polarisDiscoveryAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisDiscoveryAutoConfiguration.class, "polarisDiscoveryAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisDiscoveryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisDiscoveryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisDiscoveryAutoConfiguration.class).getBeanDefinition()); + Object polarisDiscoveryRibbonAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisDiscoveryRibbonAutoConfiguration.class, "polarisDiscoveryRibbonAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisDiscoveryRibbonAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisDiscoveryRibbonAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisDiscoveryRibbonAutoConfiguration.class).getBeanDefinition()); + Object polarisServiceRegistryAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisServiceRegistryAutoConfiguration.class, "polarisServiceRegistryAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisServiceRegistryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisServiceRegistryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisServiceRegistryAutoConfiguration.class).getBeanDefinition()); + Object polarisDiscoveryEndpointAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisDiscoveryEndpointAutoConfiguration.class, "polarisDiscoveryEndpointAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisDiscoveryEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisDiscoveryEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisDiscoveryEndpointAutoConfiguration.class).getBeanDefinition()); +// Object polarisFeignBeanPostProcessor = ReflectionUtils.invokeConstructor(configClassCreator, PolarisFeignBeanPostProcessor.class, "polarisFeignBeanPostProcessor"); +// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisFeignBeanPostProcessor, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisFeignBeanPostProcessor", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisFeignBeanPostProcessor.class).getBeanDefinition()); } -} +} \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java index 5b9977ac..525269fd 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java @@ -20,8 +20,15 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import cn.polarismesh.agent.core.common.utils.ClassUtils; +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.polaris.router.config.FeignAutoConfiguration; +import com.tencent.cloud.polaris.router.config.RouterAutoConfiguration; +import com.tencent.cloud.polaris.router.endpoint.PolarisRouterEndpointAutoConfiguration; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; public class RouterBeanInjector implements BeanInjector { @@ -32,6 +39,20 @@ public void onBootstrapStartup(Object configurationParser, Constructor config @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { - + Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RouterAutoConfiguration.class, "routerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, routerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("routerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + RouterAutoConfiguration.class).getBeanDefinition()); + if (null != ClassUtils.getClazz("feign.RequestInterceptor", + Thread.currentThread().getContextClassLoader())) { + Object feignAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, FeignAutoConfiguration.class, "feignAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, feignAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("feignAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + FeignAutoConfiguration.class).getBeanDefinition()); + } + Object polarisRouterEndpointAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRouterEndpointAutoConfiguration.class, "polarisRouterEndpointAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRouterEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisRouterEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisRouterEndpointAutoConfiguration.class).getBeanDefinition()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java index b7ce4e13..5435486b 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -29,26 +29,20 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.core.extension.interceptor.Interceptor; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; -import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.inject.CircuitBreakerBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.CommonBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.ConfigBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.LoadbalancerBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.LosslessBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.PolarisContextBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.RateLimitBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.RegistryBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.RouterBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.RpcEnhancementBeanInjector; -import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; -import com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration; -import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; -import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; -import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; -import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration; -import com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration; -import com.tencent.cloud.polaris.router.config.FeignAutoConfiguration; -import com.tencent.cloud.polaris.router.config.RouterAutoConfiguration; import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; public class ConfigurationParserInterceptor implements Interceptor { @@ -58,9 +52,14 @@ public class ConfigurationParserInterceptor implements Interceptor { public ConfigurationParserInterceptor() { beanInjectors.add(new CommonBeanInjector()); beanInjectors.add(new PolarisContextBeanInjector()); + beanInjectors.add(new RegistryBeanInjector()); + beanInjectors.add(new ConfigBeanInjector()); beanInjectors.add(new RpcEnhancementBeanInjector()); beanInjectors.add(new LosslessBeanInjector()); beanInjectors.add(new LoadbalancerBeanInjector()); + beanInjectors.add(new RouterBeanInjector()); + beanInjectors.add(new CircuitBreakerBeanInjector()); + beanInjectors.add(new RateLimitBeanInjector()); } @@ -106,16 +105,16 @@ public void after(Object target, Object[] args, Object result, Throwable throwab // lossless // register - Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("discoveryPropertiesBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - DiscoveryPropertiesBootstrapAutoConfiguration.class).getBeanDefinition()); +// Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("discoveryPropertiesBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// DiscoveryPropertiesBootstrapAutoConfiguration.class).getBeanDefinition()); // config - Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisConfigBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisConfigBootstrapAutoConfiguration.class).getBeanDefinition()); +// Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisConfigBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisConfigBootstrapAutoConfiguration.class).getBeanDefinition()); } else if (isMainBeanDefinition(beanDefinitionHolder)) { Class clazz = ClassUtils.getClazz("org.springframework.context.annotation.ConfigurationClass", null); @@ -138,41 +137,41 @@ public void after(Object target, Object[] args, Object result, Throwable throwab // loadbalancer // router - Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, RouterAutoConfiguration.class, "routerAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, routerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("routerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - RouterAutoConfiguration.class).getBeanDefinition()); - if (null != ClassUtils.getClazz("feign.RequestInterceptor", - Thread.currentThread().getContextClassLoader())) { - Object feignAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, FeignAutoConfiguration.class, "feignAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, feignAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("feignAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - FeignAutoConfiguration.class).getBeanDefinition()); - } +// Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, RouterAutoConfiguration.class, "routerAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, routerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("routerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// RouterAutoConfiguration.class).getBeanDefinition()); +// if (null != ClassUtils.getClazz("feign.RequestInterceptor", +// Thread.currentThread().getContextClassLoader())) { +// Object feignAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, FeignAutoConfiguration.class, "feignAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, feignAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("feignAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// FeignAutoConfiguration.class).getBeanDefinition()); +// } // registry - Object discoveryPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesAutoConfiguration.class, "discoveryPropertiesAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("discoveryPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - DiscoveryPropertiesAutoConfiguration.class).getBeanDefinition()); - Object polarisDiscoveryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryAutoConfiguration.class, "polarisDiscoveryAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisDiscoveryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisDiscoveryAutoConfiguration.class).getBeanDefinition()); - Object polarisDiscoveryRibbonAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryRibbonAutoConfiguration.class, "polarisDiscoveryRibbonAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryRibbonAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisDiscoveryRibbonAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisDiscoveryRibbonAutoConfiguration.class).getBeanDefinition()); - Object polarisServiceRegistryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisServiceRegistryAutoConfiguration.class, "polarisServiceRegistryAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisServiceRegistryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisServiceRegistryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisServiceRegistryAutoConfiguration.class).getBeanDefinition()); +// Object discoveryPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesAutoConfiguration.class, "discoveryPropertiesAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("discoveryPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// DiscoveryPropertiesAutoConfiguration.class).getBeanDefinition()); +// Object polarisDiscoveryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryAutoConfiguration.class, "polarisDiscoveryAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisDiscoveryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisDiscoveryAutoConfiguration.class).getBeanDefinition()); +// Object polarisDiscoveryRibbonAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryRibbonAutoConfiguration.class, "polarisDiscoveryRibbonAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryRibbonAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisDiscoveryRibbonAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisDiscoveryRibbonAutoConfiguration.class).getBeanDefinition()); +// Object polarisServiceRegistryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisServiceRegistryAutoConfiguration.class, "polarisServiceRegistryAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisServiceRegistryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisServiceRegistryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisServiceRegistryAutoConfiguration.class).getBeanDefinition()); // config - Object polarisConfigAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigAutoConfiguration.class, "polarisConfigAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisConfigAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisConfigAutoConfiguration.class).getBeanDefinition()); +// Object polarisConfigAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigAutoConfiguration.class, "polarisConfigAutoConfiguration"); +// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); +// registry.registerBeanDefinition("polarisConfigAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( +// PolarisConfigAutoConfiguration.class).getBeanDefinition()); } } } From cada8319ceec47b4575879ca92e7c92fec905fe7 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Wed, 8 May 2024 19:14:48 +0800 Subject: [PATCH 12/24] feat: add environment parameter to obtain the configuration --- .../src/main/resources/application.yaml | 2 +- .../agent/examples/alibaba/cloud/ServiceB.java | 3 --- .../agent/examples/alibaba/cloud/ServiceC.java | 3 --- .../service-gateway/pom.xml | 3 ++- .../src/main/resources/application.yaml | 2 +- .../plugin/spring/cloud/common/BeanInjector.java | 5 +++-- .../cloud/inject/CircuitBreakerBeanInjector.java | 16 +++++++--------- .../spring/cloud/inject/CommonBeanInjector.java | 5 +++-- .../spring/cloud/inject/ConfigBeanInjector.java | 14 +++++++------- .../cloud/inject/LoadbalancerBeanInjector.java | 5 +++-- .../cloud/inject/LosslessBeanInjector.java | 5 +++-- .../cloud/inject/PolarisContextBeanInjector.java | 6 +++--- .../cloud/inject/RateLimitBeanInjector.java | 5 +++-- .../cloud/inject/RegistryBeanInjector.java | 12 +++++------- .../spring/cloud/inject/RouterBeanInjector.java | 5 +++-- .../cloud/inject/RpcEnhancementBeanInjector.java | 5 +++-- .../ConfigurationParserInterceptor.java | 9 +++++---- 17 files changed, 52 insertions(+), 53 deletions(-) diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml index 31fffe33..1774d42b 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml @@ -7,7 +7,7 @@ spring: cloud: nacos: discovery: - enabled: true + enabled: false server-addr: 127.0.0.1:8848 config: enabled: false diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java index 06d7b3e0..aa45e549 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java @@ -17,9 +17,6 @@ package cn.polarismesh.agent.examples.alibaba.cloud; -import java.util.Collections; -import java.util.Optional; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java index 0bf361df..b95f3c59 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java @@ -17,9 +17,6 @@ package cn.polarismesh.agent.examples.alibaba.cloud; -import java.util.Collections; -import java.util.Optional; - import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml index 0e222355..9a472603 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml @@ -25,7 +25,7 @@ org.springframework.cloud spring-cloud-dependencies - 2023.0.0 + Hoxton.SR12 pom import @@ -49,6 +49,7 @@ org.springframework.cloud spring-cloud-starter-loadbalancer + org.springframework.boot spring-boot-starter-actuator diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml index fafe39cb..84b33278 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml @@ -16,7 +16,7 @@ spring: enabled: false server-addr: 127.0.0.1:8848 config: - enabled: true + enabled: false serverAddr: 127.0.0.1:8848 username: 'nacos' password: 'nacos' diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java index 55aeb9a7..08826dc8 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public interface BeanInjector { @@ -28,11 +29,11 @@ public interface BeanInjector { * 在Bootstrap启动过程中进行Bean装载 */ void onBootstrapStartup(Object configurationParser, - Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry); + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment); /** * 在应用启动过程中进行Bean装载 */ void onApplicationStartup(Object configurationParser, - Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry); + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment); } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java index 09c2ef17..ce4a59e3 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java @@ -6,20 +6,18 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; - -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerAutoConfiguration; +import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerBootstrapConfiguration; import com.tencent.cloud.polaris.circuitbreaker.config.ReactivePolarisCircuitBreakerAutoConfiguration; -import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerFeignClientAutoConfiguration; -import com.tencent.cloud.polaris.circuitbreaker.config.GatewayPolarisCircuitBreakerAutoConfiguration; -import org.springframework.cloud.openfeign.PolarisFeignCircuitBreakerTargeterAutoConfiguration; import com.tencent.cloud.polaris.circuitbreaker.endpoint.PolarisCircuitBreakerEndpointAutoConfiguration; -import com.tencent.cloud.polaris.circuitbreaker.config.PolarisCircuitBreakerBootstrapConfiguration; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class CircuitBreakerBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisCircuitBreakerBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerBootstrapConfiguration.class, "polarisCircuitBreakerBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); @@ -32,7 +30,7 @@ public void onBootstrapStartup(Object configurationParser, Constructor config } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerAutoConfiguration.class, "polarisCircuitBreakerAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java index 34c067fc..d7e01c68 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java @@ -29,15 +29,16 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class CommonBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object applicationContextAwareUtils = ReflectionUtils.invokeConstructor(configClassCreator, ApplicationContextAwareUtils.class, "applicationContextAwareUtils"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, applicationContextAwareUtils, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("applicationContextAwareUtils", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java index f91e0d2c..1611ef55 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java @@ -22,18 +22,18 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; - import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; -import com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; +import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.endpoint.PolarisConfigEndpointAutoConfiguration; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; + public class ConfigBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisConfigBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisConfigBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -41,7 +41,7 @@ public void onBootstrapStartup(Object configurationParser, Constructor config } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisConfigAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigAutoConfiguration.class, "polarisConfigAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisConfigAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisConfigAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java index f3e6f2c7..7abe57cc 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java @@ -27,15 +27,16 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class LoadbalancerBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisLoadBalancerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisLoadBalancerAutoConfiguration.class, "polarisLoadBalancerAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisLoadBalancerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisLoadBalancerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java index fcce305d..d1f9a783 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java @@ -29,10 +29,11 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class LosslessBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object losslessPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessPropertiesBootstrapConfiguration.class, "losslessPropertiesBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, losslessPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("losslessPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -40,7 +41,7 @@ public void onBootstrapStartup(Object configurationParser, Constructor config } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object losslessPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessPropertiesAutoConfiguration.class, "losslessPropertiesAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, losslessPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("losslessPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java index c92ad763..e18fb992 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java @@ -20,7 +20,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; -import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; @@ -31,11 +30,12 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class PolarisContextBeanInjector implements BeanInjector { @Override public void onBootstrapStartup(Object configurationParser, - Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisContextBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisContextBootstrapAutoConfiguration.class, "polarisContextBootstrapAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisContextBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -46,7 +46,7 @@ public void onBootstrapStartup(Object configurationParser, @Override public void onApplicationStartup(Object configurationParser, - Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisContextAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisContextAutoConfiguration.class, "polarisContextAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisContextAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java index 03806a3a..62af49b3 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java @@ -13,10 +13,11 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class RateLimitBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisRateLimitPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitPropertiesBootstrapConfiguration.class, "polarisRateLimitPropertiesBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisRateLimitPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -24,7 +25,7 @@ public void onBootstrapStartup(Object configurationParser, Constructor config } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisRateLimitAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitAutoConfiguration.class, "polarisRateLimitAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisRateLimitAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java index b75d4333..8cdb826c 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java @@ -22,23 +22,21 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; - import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration; -import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; +import com.tencent.cloud.polaris.endpoint.PolarisDiscoveryEndpointAutoConfiguration; import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration; import com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration; -import com.tencent.cloud.polaris.endpoint.PolarisDiscoveryEndpointAutoConfiguration; -import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration; -import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration; + import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class RegistryBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, discoveryPropertiesBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("discoveryPropertiesBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -46,7 +44,7 @@ public void onBootstrapStartup(Object configurationParser, Constructor config } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object discoveryPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, DiscoveryPropertiesAutoConfiguration.class, "discoveryPropertiesAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, discoveryPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("discoveryPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java index 525269fd..057d7d20 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java @@ -30,15 +30,16 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class RouterBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RouterAutoConfiguration.class, "routerAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, routerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("routerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java index ad22e484..57f87936 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java @@ -30,10 +30,11 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class RpcEnhancementBeanInjector implements BeanInjector { @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisStatPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesBootstrapConfiguration.class, "polarisStatPropertiesBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisStatPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisStatPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -45,7 +46,7 @@ public void onBootstrapStartup(Object configurationParser, Constructor config } @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry) { + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisStatPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesAutoConfiguration.class, "polarisStatPropertiesAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisStatPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisStatPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java index 5435486b..7901fad2 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -44,6 +44,7 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; public class ConfigurationParserInterceptor implements Interceptor { @@ -94,9 +95,9 @@ public void after(Object target, Object[] args, Object result, Throwable throwab ReflectionUtils.makeAccessible(processConfigurationClass); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry"); - + Environment environment = (Environment) ReflectionUtils.getObjectByFieldName(target, "environment"); for (BeanInjector beanInjector : beanInjectors) { - beanInjector.onBootstrapStartup(target, constructor, processConfigurationClass, registry); + beanInjector.onBootstrapStartup(target, constructor, processConfigurationClass, registry, environment); } // rpc @@ -123,9 +124,9 @@ public void after(Object target, Object[] args, Object result, Throwable throwab ReflectionUtils.makeAccessible(processConfigurationClass); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry"); - + Environment environment = (Environment) ReflectionUtils.getObjectByFieldName(target, "environment"); for (BeanInjector beanInjector : beanInjectors) { - beanInjector.onApplicationStartup(target, constructor, processConfigurationClass, registry); + beanInjector.onApplicationStartup(target, constructor, processConfigurationClass, registry, environment); } From 1be919fbf5686cc81e707b0d91e211b2fd448bcf Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Wed, 8 May 2024 22:00:28 +0800 Subject: [PATCH 13/24] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=85=83?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BC=A0=E8=BE=93=E7=9B=B8=E5=85=B3=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yaml | 2 +- .../inject/MetadataTransferBeanInjector.java | 47 +++++++++++++++++++ .../ConfigurationParserInterceptor.java | 4 +- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml index 84b33278..a82e04e5 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml @@ -3,7 +3,7 @@ server: spring: application: - name: sc-gateway-hoxton + name: service-gateway-hoxton cloud: gateway: discovery: diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java new file mode 100644 index 00000000..bfe563de --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java @@ -0,0 +1,47 @@ +/* + * 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.inject; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import cn.polarismesh.agent.core.common.utils.ReflectionUtils; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import com.tencent.cloud.metadata.config.MetadataTransferAutoConfiguration; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.core.env.Environment; + +public class MetadataTransferBeanInjector implements BeanInjector { + + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + + } + + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + Object metadataTransferAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, MetadataTransferAutoConfiguration.class, "metadataTransferAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, metadataTransferAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("metadataTransferAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + MetadataTransferAutoConfiguration.class).getBeanDefinition()); + } + +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java index 7901fad2..75ab1cb1 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -34,6 +34,7 @@ import cn.polarismesh.agent.plugin.spring.cloud.inject.ConfigBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.LoadbalancerBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.LosslessBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.MetadataTransferBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.PolarisContextBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.RateLimitBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.RegistryBeanInjector; @@ -53,13 +54,14 @@ public class ConfigurationParserInterceptor implements Interceptor { public ConfigurationParserInterceptor() { beanInjectors.add(new CommonBeanInjector()); beanInjectors.add(new PolarisContextBeanInjector()); + beanInjectors.add(new MetadataTransferBeanInjector()); beanInjectors.add(new RegistryBeanInjector()); beanInjectors.add(new ConfigBeanInjector()); beanInjectors.add(new RpcEnhancementBeanInjector()); beanInjectors.add(new LosslessBeanInjector()); beanInjectors.add(new LoadbalancerBeanInjector()); beanInjectors.add(new RouterBeanInjector()); - beanInjectors.add(new CircuitBreakerBeanInjector()); + //beanInjectors.add(new CircuitBreakerBeanInjector()); beanInjectors.add(new RateLimitBeanInjector()); } From 0260fddda83338560f503dd8e6c67ca73630f0b2 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Thu, 9 May 2024 17:18:32 +0800 Subject: [PATCH 14/24] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4example?= =?UTF-8?q?=E6=89=93=E5=8C=85=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bin/build_example_docker.sh | 71 ++++++++++--------- .../spring-cloud-plugins-examples/pom.xml | 4 +- .../service-a/Dockerfile | 3 +- .../service-a/deployment.yaml | 17 +++-- .../gray-release-examples/service-a/pom.xml | 1 + .../service-b/Dockerfile | 3 +- .../service-b/deployment.yaml | 17 +++-- .../gray-release-examples/service-b/pom.xml | 1 + .../service-c/Dockerfile | 3 +- .../service-c/deployment.yaml | 17 +++-- .../service-gateway/Dockerfile | 3 +- .../service-gateway/deployment.yaml | 17 +++-- .../service-gateway/pom.xml | 1 + .../src/main/resources/application.yaml | 4 +- .../consumer/deployment.yaml | 7 +- .../quickstart-examples/consumer/pom.xml | 2 +- .../src/main/resources/application.properties | 3 +- .../provider/deployment.yaml | 7 +- .../quickstart-examples/provider/pom.xml | 2 +- .../cloud/cloud/ProviderApplication.java | 47 +----------- .../src/main/resources/application.properties | 2 +- .../quickstart-examples/scg/deployment.yaml | 7 +- .../quickstart-examples/scg/pom.xml | 2 +- .../src/main/resources/application.properties | 5 +- .../cloud/inject/RouterBeanInjector.java | 20 ++++++ .../ConfigurationParserInterceptor.java | 61 ---------------- .../src/main/resources/default-plugin.conf | 4 +- 27 files changed, 127 insertions(+), 204 deletions(-) diff --git a/polaris-agent-build/bin/build_example_docker.sh b/polaris-agent-build/bin/build_example_docker.sh index f70c6c29..62b177e0 100644 --- a/polaris-agent-build/bin/build_example_docker.sh +++ b/polaris-agent-build/bin/build_example_docker.sh @@ -46,38 +46,45 @@ for docker_file in ${docker_files}; do dir_name=${folder_name##*/} - cp Dockerfile Dockerfile-${dir_name} - docker_tag="${version}-java8" - - filename=$(find ./target -maxdepth 1 -name "*.jar" | grep -v "javadoc" | grep -v "sources" | head -n 1) - if [ -z "${filename}" ]; then - echo "jar file not found" - exit 1 - fi - echo "jar file path is ${filename}" - - filename=${filename##*/} - echo "jar sub file path is ${filename}" - if [ `grep -c "java_version" Dockerfile` -gt 0 ] - then - echo "docker repository java8: ${docker_repository}/${repo_name}, tag : ${docker_tag}" - docker buildx build -f Dockerfile-${dir_name} --no-cache -t ${docker_repository}/${repo_name}:${docker_tag} --build-arg file_name=${filename} --build-arg java_version=8 --platform ${platforms} --push ./ - - docker_tag="${version}-java11" - echo "docker repository java11: ${docker_repository}/${repo_name}, tag : ${docker_tag}" - docker buildx build -f Dockerfile-${dir_name} --no-cache -t ${docker_repository}/${repo_name}:${docker_tag} --build-arg file_name=${filename} --build-arg java_version=11 --platform ${platforms} --push ./ - - docker_tag="${version}-java17" - echo "docker repository java17: ${docker_repository}/${repo_name}, tag : ${docker_tag}" - docker buildx build -f Dockerfile-${dir_name} --no-cache -t ${docker_repository}/${repo_name}:${docker_tag} --build-arg file_name=${filename} --build-arg java_version=17 --platform ${platforms} --push ./ - else - docker_tag="${version}-java17" - echo "docker repository java17: ${docker_repository}/${repo_name}, tag : ${docker_tag}" - docker buildx build -f Dockerfile-${dir_name} --no-cache -t ${docker_repository}/${repo_name}:${docker_tag} --build-arg file_name=${filename} --platform ${platforms} --push ./ - fi - - rm -f Dockerfile-${dir_name} - popd + cp Dockerfile Dockerfile-${dir_name} + + filename=$(find ./target -maxdepth 1 -name "*.jar" | grep -v "javadoc" | grep -v "sources" | head -n 1) + if [ -z "${filename}" ]; then + echo "jar file not found" + exit 1 + fi + echo "jar file path is ${filename}" + + version=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml) + if [ -z "${version}" ]; then + echo "version not defined" + exit 1 + fi + + filename=${filename##*/} + echo "jar sub file path is ${filename}, version is ${version}" + + if [ `grep -c "java_version" Dockerfile` -gt 0 ] + then + docker_tag="${version}-java8" + echo "docker repository java8: ${docker_repository}/${repo_name}, tag : ${docker_tag}" + docker buildx build -f Dockerfile-${dir_name} --no-cache -t ${docker_repository}/${repo_name}:${docker_tag} --build-arg file_name=${filename} --build-arg java_version=8 --platform ${platforms} --push ./ + + docker_tag="${version}-java11" + echo "docker repository java11: ${docker_repository}/${repo_name}, tag : ${docker_tag}" + docker buildx build -f Dockerfile-${dir_name} --no-cache -t ${docker_repository}/${repo_name}:${docker_tag} --build-arg file_name=${filename} --build-arg java_version=11 --platform ${platforms} --push ./ + + docker_tag="${version}-java17" + echo "docker repository java17: ${docker_repository}/${repo_name}, tag : ${docker_tag}" + docker buildx build -f Dockerfile-${dir_name} --no-cache -t ${docker_repository}/${repo_name}:${docker_tag} --build-arg file_name=${filename} --build-arg java_version=17 --platform ${platforms} --push ./ + else + docker_tag="${version}-java17" + echo "docker repository java17: ${docker_repository}/${repo_name}, tag : ${docker_tag}" + docker buildx build -f Dockerfile-${dir_name} --no-cache -t ${docker_repository}/${repo_name}:${docker_tag} --build-arg file_name=${filename} --platform ${platforms} --push ./ + fi + + rm -f Dockerfile-${dir_name} + popd fi done popd \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/pom.xml index cb43473e..a58d7295 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/pom.xml @@ -17,7 +17,7 @@ spring-cloud-2021-examples spring-cloud-2020-examples spring-cloud-hoxton-examples - spring-cloud-2023-examples - + spring-cloud-2023-examples + \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/Dockerfile b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/Dockerfile index f8284a09..e4da5f69 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/Dockerfile +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/Dockerfile @@ -1,6 +1,7 @@ FROM alpine:3.19.1 ARG file_name +ARG java_version COPY ./target/${file_name} /app/main.jar @@ -9,7 +10,7 @@ WORKDIR /app RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories RUN set -eux && \ - apk add openjdk17 && \ + apk add openjdk${java_version} && \ apk add bind-tools && \ apk add busybox-extras && \ apk add findutils && \ diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/deployment.yaml index a8b7a25d..8face3ad 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/deployment.yaml @@ -3,27 +3,26 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - app: service-a-2023 - name: service-a-2023 + app: service-a-hoxton + name: service-a-hoxton namespace: default spec: replicas: 1 selector: matchLabels: - app: service-a-2023 + app: service-a-hoxton template: metadata: labels: - app: service-a-2023 + app: service-a-hoxton annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.7.0-RC.0" + polarismesh.cn/javaagentVersion: "1.7.0-RC2" 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/javaagentFrameworkVersion: "hoxton" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-a:1.7.0-RC.0-java17 + - image: polarismesh/polaris-javaagent-demo-sc-gray-hoxton-service-a:1.7.0-java8 imagePullPolicy: Always name: consumer resources: @@ -35,7 +34,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar + - cd /app && java -Dserver.port=65203 -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml index dff8fe67..34594434 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml @@ -13,6 +13,7 @@ 2023-service-a Demo Service-A Project For Spring Cloud Alibaba polaris-javaagent-demo-sc-gray-hoxton-service-a + 1.7.0 jar diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/Dockerfile b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/Dockerfile index f8284a09..e4da5f69 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/Dockerfile +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/Dockerfile @@ -1,6 +1,7 @@ FROM alpine:3.19.1 ARG file_name +ARG java_version COPY ./target/${file_name} /app/main.jar @@ -9,7 +10,7 @@ WORKDIR /app RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories RUN set -eux && \ - apk add openjdk17 && \ + apk add openjdk${java_version} && \ apk add bind-tools && \ apk add busybox-extras && \ apk add findutils && \ diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/deployment.yaml index ab008070..1878fcd7 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/deployment.yaml @@ -3,27 +3,26 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - app: service-b-2023 - name: service-b-2023 + app: service-b-hoxton + name: service-b-hoxton namespace: default spec: replicas: 1 selector: matchLabels: - app: service-b-2023 + app: service-b-hoxton template: metadata: labels: - app: service-b-2023 + app: service-b-hoxton annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.7.0-RC.0" + polarismesh.cn/javaagentVersion: "1.7.0-RC2" 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/javaagentFrameworkVersion: "hoxton" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-b:1.7.0-RC.0-java17 + - image: polarismesh/polaris-javaagent-demo-sc-gray-hoxton-service-b:1.7.0-java8 imagePullPolicy: Always name: consumer resources: @@ -35,7 +34,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar + - cd /app && java -Dserver.port=65202 -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/pom.xml index e49f5a2e..7689a8a4 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/pom.xml @@ -14,6 +14,7 @@ Demo Service-B Project For Spring Cloud Alibaba polaris-javaagent-demo-sc-gray-hoxton-service-b jar + 1.7.0 diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/Dockerfile b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/Dockerfile index f8284a09..e4da5f69 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/Dockerfile +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/Dockerfile @@ -1,6 +1,7 @@ FROM alpine:3.19.1 ARG file_name +ARG java_version COPY ./target/${file_name} /app/main.jar @@ -9,7 +10,7 @@ WORKDIR /app RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories RUN set -eux && \ - apk add openjdk17 && \ + apk add openjdk${java_version} && \ apk add bind-tools && \ apk add busybox-extras && \ apk add findutils && \ diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/deployment.yaml index d4a3dd56..ac72f42e 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/deployment.yaml @@ -3,27 +3,26 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - app: service-c-2023 - name: service-c-2023 + app: service-c-hoxton + name: service-c-hoxton namespace: default spec: replicas: 1 selector: matchLabels: - app: service-c-2023 + app: service-c-hoxton template: metadata: labels: - app: service-c-2023 + app: service-c-hoxton annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.7.0-RC.0" + polarismesh.cn/javaagentVersion: "1.7.0-RC2" 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/javaagentFrameworkVersion: "hoxton" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-c:1.7.0-RC.0-java17 + - image: polarismesh/polaris-javaagent-demo-sc-gray-hoxton-service-c:1.7.0-java8 imagePullPolicy: Always name: consumer resources: @@ -35,7 +34,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar + - cd /app && java -Dserver.port=65201 -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/Dockerfile b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/Dockerfile index f8284a09..e4da5f69 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/Dockerfile +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/Dockerfile @@ -1,6 +1,7 @@ FROM alpine:3.19.1 ARG file_name +ARG java_version COPY ./target/${file_name} /app/main.jar @@ -9,7 +10,7 @@ WORKDIR /app RUN sed -i 's!http://dl-cdn.alpinelinux.org/!https://mirrors.tencent.com/!g' /etc/apk/repositories RUN set -eux && \ - apk add openjdk17 && \ + apk add openjdk${java_version} && \ apk add bind-tools && \ apk add busybox-extras && \ apk add findutils && \ diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/deployment.yaml index a9062581..e5e052f2 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/deployment.yaml @@ -3,27 +3,26 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - app: sc-gateway-2023 - name: sc-gateway-2023 + app: sc-gateway-hoxton + name: sc-gateway-hoxton namespace: default spec: replicas: 1 selector: matchLabels: - app: sc-gateway-2023 + app: sc-gateway-hoxton template: metadata: labels: - app: sc-gateway-2023 + app: sc-gateway-hoxton annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.7.0-RC.0" + polarismesh.cn/javaagentVersion: "1.7.0-RC2" 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/javaagentFrameworkVersion: "hoxton" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-gray-2023-service-gateway:1.7.0-RC.0-java17 + - image: polarismesh/polaris-javaagent-demo-sc-gray-hoxton-service-gateway:1.7.0-java8 imagePullPolicy: Always name: consumer resources: @@ -35,7 +34,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.config.server-addr=127.0.0.1:8848 -Dspring.cloud.nacos.config.password=nacos -jar main.jar + - cd /app && java -Dserver.port=65200 -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml index 9a472603..486c7a46 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/pom.xml @@ -11,6 +11,7 @@ hoxton-service-gateway Demo Service-Gateway Project For Spring Cloud Alibaba 2023 jar + 1.7.0 diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml index a82e04e5..13b92656 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml @@ -3,7 +3,7 @@ server: spring: application: - name: service-gateway-hoxton + name: sc-gateway-hoxton cloud: gateway: discovery: @@ -22,7 +22,7 @@ spring: password: 'nacos' config: import: - - nacos:sc-gateway-2023.properties?refreshEnabled=true + - nacos:sc-gateway-hoxton.properties?refreshEnabled=true management: endpoint: health: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/deployment.yaml index 2de2d925..663c632e 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/deployment.yaml @@ -17,13 +17,12 @@ spec: app: hoxton-consumer annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.6.1" + polarismesh.cn/javaagentVersion: "1.7.0-RC2" polarismesh.cn/javaagentFrameworkName: "spring-cloud" polarismesh.cn/javaagentFrameworkVersion: "hoxton" - polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.lossless.enabled\": \"true\", \"spring.cloud.polaris.namespace\": \"default\"}" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-consumer:1.6.1-java8 + - image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-consumer:1.7.0-java8 imagePullPolicy: Always name: consumer resources: @@ -35,7 +34,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.discovery.server-addr=10.0.90.37:8848 -Dspring.cloud.nacos.config.server-addr=10.0.90.37:8848 -Dserver.port=11581 -jar main.jar + - cd /app && java -Dserver.port=65002 -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/pom.xml index 483a7d8b..fcd53d5e 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/pom.xml @@ -13,7 +13,7 @@ com.tencent.polaris polaris-javaagent-demo-sc-quickstart-hoxton-consumer jar - 1.6.0 + 1.7.0 hoxton-consumer Demo Consumer Project For Spring Cloud Alibaba diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/resources/application.properties index 04c51aee..12f01521 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/resources/application.properties @@ -1,4 +1,5 @@ server.port=65002 spring.application.name=service-consumer spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.discovery.enabled=true \ No newline at end of file +spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.config.enabled=false \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/deployment.yaml index 680a0cdc..c05ed845 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/deployment.yaml @@ -17,13 +17,12 @@ spec: app: hoxton-provider annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.6.1" + polarismesh.cn/javaagentVersion: "1.7.0-RC2" polarismesh.cn/javaagentFrameworkName: "spring-cloud" polarismesh.cn/javaagentFrameworkVersion: "hoxton" - polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.lossless.enabled\": \"true\", \"spring.cloud.polaris.namespace\": \"default\"}" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-provider:1.6.1-java8 + - image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-provider:1.7.0-java8 imagePullPolicy: Always name: provider resources: @@ -35,7 +34,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.discovery.server-addr=10.0.90.37:8848 -Dspring.cloud.nacos.config.server-addr=10.0.90.37:8848 -Dserver.port=11580 -jar main.jar + - cd /app && java -Dserver.port=65101 -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/pom.xml index 8d366203..3690d775 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/pom.xml @@ -13,7 +13,7 @@ com.tencent.polaris polaris-javaagent-demo-sc-quickstart-hoxton-provider jar - 1.6.0 + 1.7.0 hoxton-provider Demo Provider Project For Spring Cloud Alibaba diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java index dedac899..a765fc6f 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ProviderApplication.java @@ -31,41 +31,6 @@ /** * @author liaochuntao */ -//@SpringBootApplication -//public class ProviderApplication { -// -// public static void main(String[] args) { -// SpringApplication.run(ProviderApplication.class, args); -// } -// -// @RefreshScope -// @RestController -// public static class EchoController { -// -// @Value("${name:}") -// private String name; -// -// private Registration registration; -// -// public EchoController(Registration registration) { -// this.registration = registration; -// } -// -// @GetMapping("/echo/{string}") -// public String echo(@PathVariable String string) { -// String sb = "Hello, I'm provider, receive msg : " -// + string -// + "my metadata : " -// + registration.getMetadata() -// + " name config : " -// + name; -// return sb; -// } -// -// } -// -//} - @EnableDiscoveryClient @SpringBootApplication public class ProviderApplication { @@ -81,21 +46,13 @@ public static class EchoController { @Value("${name:}") private String name; - // private Registration registration; - - // public EchoController(Registration registration) { - // this.registration = registration; - // } - @GetMapping("/echo/{string}") public String echo(@PathVariable String string) { - String sb = "Hello, I'm provider, receive msg : " + return "Hello, I'm provider, receive msg : " + string - + "my metadata : " - // + registration.getMetadata() + + ", my metadata : " + " name config : " + name; - return sb; } } diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties index d97c89f0..891a42d7 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties @@ -1,5 +1,5 @@ server.port=65101 -spring.application.name=service-provider-H +spring.application.name=service-provider-quickstart-hotxon spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.enabled=false spring.cloud.nacos.config.enabled=false diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/deployment.yaml index 9badebe5..96a21783 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/deployment.yaml @@ -17,13 +17,12 @@ spec: app: hoxton-scg annotations: polarismesh.cn/javaagent: "true" - polarismesh.cn/javaagentVersion: "1.6.1" + polarismesh.cn/javaagentVersion: "1.7.0-RC2" polarismesh.cn/javaagentFrameworkName: "spring-cloud" polarismesh.cn/javaagentFrameworkVersion: "hoxton" - polarismesh.cn/javaagentConfig: "{\"spring.cloud.polaris.lossless.enabled\": \"true\", \"spring.cloud.polaris.namespace\": \"default\"}" spec: containers: - - image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-scg:1.6.1-java8 + - image: polarismesh/polaris-javaagent-demo-sc-quickstart-hoxton-scg:1.7.0-java8 imagePullPolicy: Always name: consumer resources: @@ -35,7 +34,7 @@ spec: command: - /bin/bash - -c - - cd /app && java -Dspring.cloud.nacos.discovery.server-addr=10.0.90.37:8848 -Dspring.cloud.nacos.config.server-addr=10.0.90.37:8848 -Dserver.port=11582 -jar main.jar + - cd /app && java -Dserver.port=65000 -jar main.jar lifecycle: preStop: exec: diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/pom.xml index b1c47175..af5bc113 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/pom.xml @@ -13,7 +13,7 @@ com.tencent.polaris polaris-javaagent-demo-sc-quickstart-hoxton-scg jar - 1.6.0 + 1.7.0 hoxton-scg Demo Consumer Project For Spring Cloud Alibaba diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties index 7c6f5fe3..d9587e4f 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties @@ -1,5 +1,6 @@ server.port=65000 -spring.application.name=service-gateway +spring.application.name=service-gateway-quickstart-hoxton spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.discovery.enabled=true +spring.cloud.nacos.discovery.enabled=false spring.cloud.gateway.discovery.locator.enabled=true +spring.cloud.nacos.config.enabled=false \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java index 057d7d20..4d6e8c53 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java @@ -19,6 +19,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.Map; import cn.polarismesh.agent.core.common.utils.ClassUtils; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; @@ -31,6 +32,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; +import org.springframework.core.io.ClassPathResource; public class RouterBeanInjector implements BeanInjector { @Override @@ -39,6 +41,7 @@ public void onBootstrapStartup(Object configurationParser, Constructor config } @Override + @SuppressWarnings("unchecked") public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RouterAutoConfiguration.class, "routerAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, routerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); @@ -55,5 +58,22 @@ public void onApplicationStartup(Object configurationParser, Constructor conf ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRouterEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisRouterEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisRouterEndpointAutoConfiguration.class).getBeanDefinition()); + + // make FeignRibbonClientAutoConfiguration later + Map configurationClasses = (Map) ReflectionUtils.getObjectByFieldName(configurationParser, "configurationClasses"); + Object targetConfigClass = null; + for (Object configClass : configurationClasses.keySet()) { + Object resource = ReflectionUtils.getObjectByFieldName(configClass, "resource"); + if (resource instanceof ClassPathResource) { + ClassPathResource classPathResource = (ClassPathResource) resource; + if ("org/springframework/cloud/openfeign/ribbon/FeignRibbonClientAutoConfiguration.class".equals(classPathResource.getPath())) { + targetConfigClass = configurationClasses.remove(configClass); + break; + } + } + } + if (null != targetConfigClass) { + configurationClasses.put(targetConfigClass, targetConfigClass); + } } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java index 75ab1cb1..049bf2a5 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -102,23 +102,6 @@ public void after(Object target, Object[] args, Object result, Throwable throwab beanInjector.onBootstrapStartup(target, constructor, processConfigurationClass, registry, environment); } - // rpc - - - // lossless - - // register -// Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("discoveryPropertiesBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// DiscoveryPropertiesBootstrapAutoConfiguration.class).getBeanDefinition()); - - // config -// Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("polarisConfigBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// PolarisConfigBootstrapAutoConfiguration.class).getBeanDefinition()); - } else if (isMainBeanDefinition(beanDefinitionHolder)) { Class clazz = ClassUtils.getClazz("org.springframework.context.annotation.ConfigurationClass", null); Constructor constructor = ReflectionUtils.accessibleConstructor(clazz, Class.class, String.class); @@ -131,50 +114,6 @@ public void after(Object target, Object[] args, Object result, Throwable throwab beanInjector.onApplicationStartup(target, constructor, processConfigurationClass, registry, environment); } - - // rpc - - - // lossless - - // loadbalancer - - // router -// Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, RouterAutoConfiguration.class, "routerAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, routerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("routerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// RouterAutoConfiguration.class).getBeanDefinition()); -// if (null != ClassUtils.getClazz("feign.RequestInterceptor", -// Thread.currentThread().getContextClassLoader())) { -// Object feignAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, FeignAutoConfiguration.class, "feignAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, feignAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("feignAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// FeignAutoConfiguration.class).getBeanDefinition()); -// } - - // registry -// Object discoveryPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, DiscoveryPropertiesAutoConfiguration.class, "discoveryPropertiesAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, discoveryPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("discoveryPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// DiscoveryPropertiesAutoConfiguration.class).getBeanDefinition()); -// Object polarisDiscoveryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryAutoConfiguration.class, "polarisDiscoveryAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("polarisDiscoveryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// PolarisDiscoveryAutoConfiguration.class).getBeanDefinition()); -// Object polarisDiscoveryRibbonAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisDiscoveryRibbonAutoConfiguration.class, "polarisDiscoveryRibbonAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisDiscoveryRibbonAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("polarisDiscoveryRibbonAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// PolarisDiscoveryRibbonAutoConfiguration.class).getBeanDefinition()); -// Object polarisServiceRegistryAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisServiceRegistryAutoConfiguration.class, "polarisServiceRegistryAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisServiceRegistryAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("polarisServiceRegistryAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// PolarisServiceRegistryAutoConfiguration.class).getBeanDefinition()); - - // config -// Object polarisConfigAutoConfiguration = ReflectionUtils.invokeConstructor(constructor, PolarisConfigAutoConfiguration.class, "polarisConfigAutoConfiguration"); -// ReflectionUtils.invokeMethod(processConfigurationClass, target, polarisConfigAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("polarisConfigAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( -// PolarisConfigAutoConfiguration.class).getBeanDefinition()); } } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf index 8d261187..2a415ad1 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf @@ -50,7 +50,5 @@ spring.cloud.polaris.stat.path=/metrics spring.cloud.polaris.config.enabled=false # 是否开启配置监听刷新 spring.cloud.polaris.config.auto-refresh=true -# Spring Cloud Tencent 配置刷新机制 -spring.cloud.polaris.config.refresh-type=refresh_context -logging.level.root=DEBUG \ No newline at end of file +logging.level.root=INFO \ No newline at end of file From 4faef540cde3c1493e5131063b8bdca99b83f93f Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Thu, 9 May 2024 18:08:38 +0800 Subject: [PATCH 15/24] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E6=B2=A1=E6=B3=95=E8=B7=B3=E8=BD=AC=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- polaris-agent-build/bin/build_example_docker.sh | 4 ++-- .../gray-release-examples/service-c/pom.xml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/polaris-agent-build/bin/build_example_docker.sh b/polaris-agent-build/bin/build_example_docker.sh index 62b177e0..3b92619a 100644 --- a/polaris-agent-build/bin/build_example_docker.sh +++ b/polaris-agent-build/bin/build_example_docker.sh @@ -57,8 +57,8 @@ for docker_file in ${docker_files}; do version=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml) if [ -z "${version}" ]; then - echo "version not defined" - exit 1 + echo "version not defined, skip" + continue fi filename=${filename##*/} diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/pom.xml index 3fb15b85..d1f703eb 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/pom.xml @@ -14,6 +14,7 @@ Demo Service-C Project For Spring Cloud Alibaba polaris-javaagent-demo-sc-gray-hoxton-service-c jar + 1.7.0 From 7779ff804798c19c1138bdd20e91d4e901eb7284 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Thu, 9 May 2024 21:31:46 +0800 Subject: [PATCH 16/24] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E6=A0=B7?= =?UTF-8?q?=E4=BE=8B=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.properties | 12 +++++++ .../src/main/resources/application.yaml | 27 -------------- .../src/main/resources/application.properties | 11 ++++++ .../src/main/resources/application.yaml | 30 ---------------- .../src/main/resources/application.properties | 11 ++++++ .../src/main/resources/application.yaml | 30 ---------------- .../src/main/resources/application.properties | 10 ++++++ .../src/main/resources/application.yaml | 36 ------------------- 8 files changed, 44 insertions(+), 123 deletions(-) create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.properties delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.properties delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.yaml create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.properties delete mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties new file mode 100644 index 00000000..cb4b9c0d --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties @@ -0,0 +1,12 @@ +server.port=65203 + +spring.application.name=service-a-hoxton +spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +spring.cloud.nacos.config.enabled=false +spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 +spring.cloud.nacos.username='nacos' +spring.cloud.nacos.password='nacos' +management.endpoint.health.show-details=always +management.endpoints.web.exposure.include='*' +feign.hystrix.enabled=true \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml deleted file mode 100644 index 1774d42b..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.yaml +++ /dev/null @@ -1,27 +0,0 @@ -server: - port: 65203 - -spring: - application: - name: service-a-hoxton - cloud: - nacos: - discovery: - enabled: false - server-addr: 127.0.0.1:8848 - config: - enabled: false - serverAddr: 127.0.0.1:8848 - username: 'nacos' - password: 'nacos' -management: - endpoint: - health: - show-details: always - endpoints: - web: - exposure: - include: '*' -logging: - level: - com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.properties new file mode 100644 index 00000000..c9affe8c --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.properties @@ -0,0 +1,11 @@ +server.port=65202 + +spring.application.name=service-b-hoxton +spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +spring.cloud.nacos.config.enabled=false +spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 +spring.cloud.nacos.username='nacos' +spring.cloud.nacos.password='nacos' +management.endpoint.health.show-details=always +management.endpoints.web.exposure.include='*' \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.yaml deleted file mode 100644 index e24c672f..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.yaml +++ /dev/null @@ -1,30 +0,0 @@ -server: - port: 65202 - -spring: - application: - name: service-b-hoxton - cloud: - nacos: - discovery: - enabled: false - server-addr: 127.0.0.1:8848 - config: - enabled: false - serverAddr: 127.0.0.1:8848 - username: 'nacos' - password: 'nacos' - config: - import: - - nacos:service-b-hoxton.properties?refreshEnabled=true -management: - endpoint: - health: - show-details: always - endpoints: - web: - exposure: - include: '*' -logging: - level: - com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.properties new file mode 100644 index 00000000..e7bd9c13 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.properties @@ -0,0 +1,11 @@ +server.port=65201 + +spring.application.name=service-c-hoxton +spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +spring.cloud.nacos.config.enabled=false +spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 +spring.cloud.nacos.username='nacos' +spring.cloud.nacos.password='nacos' +management.endpoint.health.show-details=always +management.endpoints.web.exposure.include='*' \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.yaml deleted file mode 100644 index 474f01fa..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.yaml +++ /dev/null @@ -1,30 +0,0 @@ -server: - port: 65201 - -spring: - application: - name: service-c-hoxton - cloud: - nacos: - discovery: - enabled: false - server-addr: 127.0.0.1:8848 - config: - enabled: false - serverAddr: 127.0.0.1:8848 - username: 'nacos' - password: 'nacos' - config: - import: - - nacos:service-c-hoxton.properties?refreshEnabled=true -management: - endpoint: - health: - show-details: always - endpoints: - web: - exposure: - include: '*' -logging: - level: - com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.properties new file mode 100644 index 00000000..9bb519a1 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.properties @@ -0,0 +1,10 @@ +server.port=65200 + +spring.application.name=sc-gateway-hoxton +spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +spring.cloud.nacos.config.enabled=false +spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 +spring.cloud.nacos.username='nacos' +spring.cloud.nacos.password='nacos' +spring.cloud.gateway.discovery.locator.enabled=true \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml deleted file mode 100644 index 13b92656..00000000 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.yaml +++ /dev/null @@ -1,36 +0,0 @@ -server: - port: 65200 - -spring: - application: - name: sc-gateway-hoxton - cloud: - gateway: - discovery: - locator: - enabled: true - nacos: - username: 'nacos' - password: 'nacos' - discovery: - enabled: false - server-addr: 127.0.0.1:8848 - config: - enabled: false - serverAddr: 127.0.0.1:8848 - username: 'nacos' - password: 'nacos' - config: - import: - - nacos:sc-gateway-hoxton.properties?refreshEnabled=true -management: - endpoint: - health: - show-details: always - endpoints: - web: - exposure: - include: '*' -logging: - level: - com.alibaba.cloud.nacos.configdata: debug \ No newline at end of file From 9c1fcba59b43e478134f176a41d6cf407f9cfdad Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Fri, 10 May 2024 12:31:05 +0800 Subject: [PATCH 17/24] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=86=94?= =?UTF-8?q?=E6=96=AD=E6=97=A0=E6=B3=95=E5=8A=A0=E8=BD=BD=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.properties | 3 +-- .../inject/CircuitBreakerBeanInjector.java | 12 +++++++----- .../ConfigurationParserInterceptor.java | 2 +- .../src/main/resources/default-plugin.conf | 18 ++++++++++-------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties index cb4b9c0d..d49c6280 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties @@ -8,5 +8,4 @@ spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 spring.cloud.nacos.username='nacos' spring.cloud.nacos.password='nacos' management.endpoint.health.show-details=always -management.endpoints.web.exposure.include='*' -feign.hystrix.enabled=true \ No newline at end of file +management.endpoints.web.exposure.include='*' \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java index d70c798b..94b8b90c 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java @@ -17,7 +17,6 @@ public class CircuitBreakerBeanInjector implements BeanInjector { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { - Object polarisCircuitBreakerBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerBootstrapConfiguration.class, "polarisCircuitBreakerBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisCircuitBreakerBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -40,10 +39,13 @@ public void onApplicationStartup(Object configurationParser, Constructor conf ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); - Object reactivePolarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, ReactivePolarisCircuitBreakerAutoConfiguration.class, "reactivePolarisCircuitBreakerAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, reactivePolarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("reactivePolarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - ReactivePolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); + if (null != ClassUtils.getClazz("reactor.core.publisher.Mono", Thread.currentThread().getContextClassLoader()) + && null != ClassUtils.getClazz("reactor.core.publisher.Flux", Thread.currentThread().getContextClassLoader())) { + Object reactivePolarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, ReactivePolarisCircuitBreakerAutoConfiguration.class, "reactivePolarisCircuitBreakerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, reactivePolarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("reactivePolarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + ReactivePolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); + } if (null != ClassUtils.getClazz("feign.Feign", Thread.currentThread().getContextClassLoader()) && null != ClassUtils.getClazz("org.springframework.cloud.openfeign.FeignClientFactoryBean", Thread.currentThread().getContextClassLoader())) { Object polarisCircuitBreakerFeignClientAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerFeignClientAutoConfiguration.class, "polarisCircuitBreakerFeignClientAutoConfiguration"); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java index 049bf2a5..b70de140 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -61,7 +61,7 @@ public ConfigurationParserInterceptor() { beanInjectors.add(new LosslessBeanInjector()); beanInjectors.add(new LoadbalancerBeanInjector()); beanInjectors.add(new RouterBeanInjector()); - //beanInjectors.add(new CircuitBreakerBeanInjector()); + beanInjectors.add(new CircuitBreakerBeanInjector()); beanInjectors.add(new RateLimitBeanInjector()); } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf index 2a415ad1..bfcf6a63 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf @@ -10,21 +10,21 @@ spring.cloud.polaris.discovery.namespace=default spring.cloud.polaris.discovery.enabled=true spring.cloud.polaris.discovery.register=true -## 是否启用北极星的 LoadBalancer +# 是否启用北极星的 LoadBalancer 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.enabled=true # 是否启用北极星的自定义路由能力 -spring.cloud.polaris.router.rule-router.enabled=false +spring.cloud.polaris.router.rule-router.enabled=true # 是否启用北极星的元数据路由能力 -spring.cloud.polaris.router.metadata-router.enabled=false +spring.cloud.polaris.router.metadata-router.enabled=true # 是否启用北极星的就近路由能力 -spring.cloud.polaris.router.nearby-router.enabled=false +spring.cloud.polaris.router.nearby-router.enabled=true -## 是否启用北极星的服务限流能力 +# 是否启用北极星的服务限流能力 spring.cloud.polaris.ratelimit.enabled=true # 设置触发限流时的提示信息 # spring.cloud.polaris.ratelimit.rejectRequestTips="" @@ -33,6 +33,8 @@ spring.cloud.polaris.ratelimit.rejectHttpCode=429 # 设置限流匀速排队最大排队时间 spring.cloud.polaris.ratelimit.maxQueuingTime=1000 +# 是否启用北极星的服务熔断能力 +spring.cloud.polaris.circuitbreaker.enabled=true ## RPC 调用增强 spring.cloud.tencent.rpc-enhancement.enabled=true @@ -47,7 +49,7 @@ spring.cloud.polaris.stat.port=0 spring.cloud.polaris.stat.path=/metrics ## 启用北极星配置中心能力 -spring.cloud.polaris.config.enabled=false +spring.cloud.polaris.config.enabled=true # 是否开启配置监听刷新 spring.cloud.polaris.config.auto-refresh=true From 9a5444096f5a81506986980762c0fcd86d61fe80 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Fri, 10 May 2024 14:41:35 +0800 Subject: [PATCH 18/24] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E5=8C=97?= =?UTF-8?q?=E6=9E=81=E6=98=9F=E7=9A=84=E7=89=88=E6=9C=AC=E5=8F=B7=E4=BE=9D?= =?UTF-8?q?=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e226cc04..a80f88ed 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ 1.7.0-RC2 - 1.15.4 + 1.15.5-SNAPSHOT UTF-8 false UTF-8 From eb8ad9ea24d501abfc016ec9fdfefe920910e8c7 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Fri, 10 May 2024 15:09:28 +0800 Subject: [PATCH 19/24] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring-cloud-hoxton-plugin/pom.xml | 8 -------- pom.xml | 8 ++++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml index c77582a6..4e0b9556 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml @@ -82,12 +82,6 @@ provided - - org.apache.commons - commons-lang3 - 3.10 - - org.springframework.cloud @@ -121,13 +115,11 @@ com.tencent.polaris polaris-all - 1.15.4 com.google.guava guava - 32.0.1-jre diff --git a/pom.xml b/pom.xml index a80f88ed..45bf8edd 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,7 @@ 1.7.0-RC2 1.15.5-SNAPSHOT + 32.0.1-jre UTF-8 false UTF-8 @@ -52,6 +53,13 @@ ${slf4j.vesion} provided + + + com.google.guava + guava + ${guava.version} + + From c26bd51a49ffc43685aa7f1afb335480a50478e1 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Sat, 11 May 2024 12:49:32 +0800 Subject: [PATCH 20/24] =?UTF-8?q?feat=EF=BC=9A=E5=AF=B9=E9=BD=90deployment?= =?UTF-8?q?=E4=B8=8Eapplication=E4=B8=AD=E7=9A=84=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=90=8D=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/examples/alibaba/cloud/ServiceA.java | 4 ++-- .../agent/examples/alibaba/cloud/ServiceB.java | 5 +---- .../agent/examples/alibaba/cloud/ServiceC.java | 7 ------- .../quickstart-examples/consumer/deployment.yaml | 8 ++++---- .../examples/alibaba/cloud/cloud/ConsumerApplication.java | 2 +- .../consumer/src/main/resources/application.properties | 2 +- .../quickstart-examples/provider/deployment.yaml | 8 ++++---- .../provider/src/main/resources/application.properties | 2 +- .../quickstart-examples/scg/deployment.yaml | 8 ++++---- .../scg/src/main/resources/application.properties | 2 +- 10 files changed, 19 insertions(+), 29 deletions(-) diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceA.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceA.java index bb7c5971..f02732eb 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceA.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceA.java @@ -53,7 +53,7 @@ public RestTemplate restTemplate() { @RestController public static class EchoController { - private Registration registration; + //private Registration registration; private RestTemplate template; @@ -63,7 +63,7 @@ public static class EchoController { @Value("${spring.cloud.tencent.metadata.content.lane:base}") private String lane; - public EchoController(RestTemplate restTemplate, Registration registration) { + public EchoController(RestTemplate restTemplate) { this.template = restTemplate; // this.registration = registration; } diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java index aa45e549..f08cd326 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceB.java @@ -50,8 +50,6 @@ public RestTemplate restTemplate() { @RestController public static class EchoController { - private Registration registration; - private RestTemplate restTemplate; @Autowired @@ -60,9 +58,8 @@ public static class EchoController { @Value("${spring.cloud.tencent.metadata.content.lane:base}") private String lane; - public EchoController(RestTemplate restTemplate, Registration registration) { + public EchoController(RestTemplate restTemplate) { this.restTemplate = restTemplate; - // this.registration = registration; } diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java index b95f3c59..ef970567 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/ServiceC.java @@ -47,16 +47,9 @@ public RestTemplate restTemplate() { @RestController public static class EchoController { - private Registration registration; - @Value("${spring.cloud.tencent.metadata.content.lane:base}") private String lane; - // public EchoController(Registration registration) { - // this.registration = registration; - // } - - @GetMapping("/echo") public String echo() { return String.format("[%s]", lane); diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/deployment.yaml index 663c632e..702388c4 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/deployment.yaml @@ -3,18 +3,18 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - app: hoxton-consumer - name: hoxton-consumer + app: service-consumer-hoxton + name: service-consumer-hoxton namespace: default spec: replicas: 1 selector: matchLabels: - app: hoxton-consumer + app: service-consumer-hoxton template: metadata: labels: - app: hoxton-consumer + app: service-consumer-hoxton annotations: polarismesh.cn/javaagent: "true" polarismesh.cn/javaagentVersion: "1.7.0-RC2" diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java index a707d42b..adadfa58 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/java/cn/polarismesh/agent/examples/alibaba/cloud/cloud/ConsumerApplication.java @@ -54,7 +54,7 @@ public EchoController(RestTemplate restTemplate) { @GetMapping("/echo/{str}") public ResponseEntity rest(@PathVariable String str) { - ResponseEntity response = template.getForEntity("http://service-provider/echo/" + str, + ResponseEntity response = template.getForEntity("http://service-provider-hoxton/echo/" + str, String.class); return response; } diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/resources/application.properties index f8516b72..a28035ac 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/consumer/src/main/resources/application.properties @@ -1,5 +1,5 @@ server.port=65002 -spring.application.name=service-consumer-quickstart-hotxon +spring.application.name=service-consumer-hoxton spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.enabled=false spring.cloud.nacos.config.enabled=false \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/deployment.yaml index c05ed845..ea4fd097 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/deployment.yaml @@ -3,18 +3,18 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - app: hoxton-provider - name: hoxton-provider + app: service-provider-hoxton + name: service-provider-hoxton namespace: default spec: replicas: 1 selector: matchLabels: - app: hoxton-provider + app: service-provider-hoxton template: metadata: labels: - app: hoxton-provider + app: service-provider-hoxton annotations: polarismesh.cn/javaagent: "true" polarismesh.cn/javaagentVersion: "1.7.0-RC2" diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties index 0a692e58..c1b8caea 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties @@ -1,5 +1,5 @@ server.port=65001 -spring.application.name=service-provider-quickstart-hotxon +spring.application.name=service-provider-hoxton spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.enabled=false spring.cloud.nacos.config.enabled=false diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/deployment.yaml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/deployment.yaml index 96a21783..79993df7 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/deployment.yaml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/deployment.yaml @@ -3,18 +3,18 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - app: hoxton-scg - name: hoxton-scg + app: scg-gateway-hoxton + name: scg-gateway-hoxton namespace: default spec: replicas: 1 selector: matchLabels: - app: hoxton-scg + app: scg-gateway-hoxton template: metadata: labels: - app: hoxton-scg + app: scg-gateway-hoxton annotations: polarismesh.cn/javaagent: "true" polarismesh.cn/javaagentVersion: "1.7.0-RC2" diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties index d9587e4f..9c7946f5 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties @@ -1,5 +1,5 @@ server.port=65000 -spring.application.name=service-gateway-quickstart-hoxton +spring.application.name=scg-gateway-hoxton spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.enabled=false spring.cloud.gateway.discovery.locator.enabled=true From c6675861920c481cd912b69b674478082c294924 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 13 May 2024 16:12:53 +0800 Subject: [PATCH 21/24] feat: optimize default-plugin.conf to load once per lifecyle --- .../cloud/common/PropertiesProvider.java | 58 +++++++++++-------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java index aafbe644..22695c5d 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java @@ -23,7 +23,9 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; import com.tencent.polaris.api.utils.StringUtils; import org.slf4j.Logger; @@ -35,36 +37,42 @@ public class PropertiesProvider { private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesProvider.class); + private static final Map> LOADED_RESOURCES = new ConcurrentHashMap<>(); + + private static final String CONFIG_FILE_NAME = "default-plugin.conf"; + /** * load the properties source from default application.yaml * @return propertySources */ public static List loadPropertiesSource() { - InputStream stream = PropertiesProvider.class.getClassLoader().getResourceAsStream("default-plugin.conf"); - Properties defaultProperties = new Properties(); - try { - defaultProperties.load(stream); - } catch (IOException e) { - throw new IllegalStateException("fail to load default-plugin.conf", e); - } - List propertySources = new ArrayList<>(); - propertySources.add(new PropertiesPropertySource("__default_polaris_agent_spring_cloud_tencent__", defaultProperties)); + return LOADED_RESOURCES.computeIfAbsent(CONFIG_FILE_NAME, fileName -> { + InputStream stream = PropertiesProvider.class.getClassLoader().getResourceAsStream(fileName); + Properties defaultProperties = new Properties(); + try { + defaultProperties.load(stream); + } catch (IOException e) { + throw new IllegalStateException("fail to load file " + fileName, e); + } + List propertySources = new ArrayList<>(); + propertySources.add(new PropertiesPropertySource("__default_polaris_agent_spring_cloud_tencent__", defaultProperties)); - String configPath = Paths.get(System.getProperty(Constant.AGENT_CONF_PATH), "conf").toString(); - LOGGER.info("load property sources from config path " + configPath); - Properties properties = new Properties(); - String confPath = Paths.get(configPath, "plugin", "spring-cloud-hoxton", "application.properties").toString(); - String cmdVal = System.getProperty("polaris.agent.user.application.conf"); - if (StringUtils.isNotBlank(cmdVal)) { - confPath = cmdVal; - } - try { - properties.load(Files.newInputStream(Paths.get(confPath).toFile().toPath())); - } - catch (IOException e) { - throw new IllegalStateException("fail to load config from " + configPath, e); - } - propertySources.add(new PropertiesPropertySource("__polaris_agent_spring_cloud_tencent__", properties)); - return propertySources; + String configPath = Paths.get(System.getProperty(Constant.AGENT_CONF_PATH), "conf").toString(); + LOGGER.info("load property sources from config path " + configPath); + Properties properties = new Properties(); + String confPath = Paths.get(configPath, "plugin", "spring-cloud-hoxton", "application.properties").toString(); + String cmdVal = System.getProperty("polaris.agent.user.application.conf"); + if (StringUtils.isNotBlank(cmdVal)) { + confPath = cmdVal; + } + try { + properties.load(Files.newInputStream(Paths.get(confPath).toFile().toPath())); + } + catch (IOException e) { + throw new IllegalStateException("fail to load config from " + configPath, e); + } + propertySources.add(new PropertiesPropertySource("__polaris_agent_spring_cloud_tencent__", properties)); + return propertySources; + }); } } From 3ce55760a906b89e72d36d2efba94dbf4986d609 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Mon, 13 May 2024 21:15:44 +0800 Subject: [PATCH 22/24] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=89=93=E5=8D=B0=EF=BC=8C=E5=A2=9E=E5=8A=A0polarisLo?= =?UTF-8?q?ggingApplicationListener=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/core/common/utils/ClassUtils.java | 2 +- .../spring-cloud-plugins/pom.xml | 3 +- .../spring-cloud-hoxton-plugin/pom.xml | 7 +- .../agent/plugin/spring/cloud/MainPlugin.java | 19 ++++ .../inject/CircuitBreakerBeanInjector.java | 5 + .../cloud/inject/CommonBeanInjector.java | 5 + .../cloud/inject/ConfigBeanInjector.java | 5 + .../inject/LoadbalancerBeanInjector.java | 5 + .../cloud/inject/LosslessBeanInjector.java | 5 + .../inject/MetadataTransferBeanInjector.java | 5 + .../inject/PolarisContextBeanInjector.java | 24 +++-- .../cloud/inject/RateLimitBeanInjector.java | 5 + .../cloud/inject/RegistryBeanInjector.java | 9 +- .../cloud/inject/RouterBeanInjector.java | 5 + .../inject/RpcEnhancementBeanInjector.java | 5 + .../ConfigurationParserInterceptor.java | 8 ++ ...ConfigurationPostProcessorInterceptor.java | 2 + .../interceptor/RegisterBeanInterceptor.java | 1 + .../SpringFactoriesLoaderInterceptor.java | 102 ++++++++++++++++++ .../spring-cloud-plugin-common/pom.xml | 50 +++++++++ .../spring/cloud/common/BeanInjector.java | 29 ++++- .../plugin/spring/cloud/common/Constant.java | 2 + .../cloud/common/PropertiesProvider.java | 3 +- 23 files changed, 285 insertions(+), 21 deletions(-) create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/SpringFactoriesLoaderInterceptor.java create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/pom.xml rename polaris-agent-plugins/spring-cloud-plugins/{spring-cloud-hoxton-plugin => spring-cloud-plugin-common}/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java (73%) rename polaris-agent-plugins/spring-cloud-plugins/{spring-cloud-hoxton-plugin => spring-cloud-plugin-common}/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java (91%) rename polaris-agent-plugins/spring-cloud-plugins/{spring-cloud-hoxton-plugin => spring-cloud-plugin-common}/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java (97%) diff --git a/polaris-agent-core/polaris-agent-core-common/src/main/java/cn/polarismesh/agent/core/common/utils/ClassUtils.java b/polaris-agent-core/polaris-agent-core-common/src/main/java/cn/polarismesh/agent/core/common/utils/ClassUtils.java index 90f4a05f..1abd7ce7 100644 --- a/polaris-agent-core/polaris-agent-core-common/src/main/java/cn/polarismesh/agent/core/common/utils/ClassUtils.java +++ b/polaris-agent-core/polaris-agent-core-common/src/main/java/cn/polarismesh/agent/core/common/utils/ClassUtils.java @@ -27,7 +27,7 @@ public static Class getClazz(String clazzName, ClassLoader classLoader) { try { return (Class) Class.forName(clazzName, false, classLoader); } catch (ClassNotFoundException e) { - logger.warn(String.format("class %s not found, error %s", clazzName, e.getLocalizedMessage())); + logger.info(String.format("class %s not found", clazzName)); } return null; } diff --git a/polaris-agent-plugins/spring-cloud-plugins/pom.xml b/polaris-agent-plugins/spring-cloud-plugins/pom.xml index 765d67ce..a983a22c 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/pom.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/pom.xml @@ -18,7 +18,8 @@ spring-cloud-2020-plugin spring-cloud-hoxton-plugin spring-cloud-2023-plugin - + spring-cloud-plugin-common + 1.8 diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml index 4e0b9556..aad4be49 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/pom.xml @@ -15,7 +15,7 @@ Hoxton.SR12 1.14.0-Hoxton.SR12-SNAPSHOT - 2.3.7.RELEASE + 2.3.12.RELEASE 1.7.30 @@ -61,6 +61,11 @@ + + com.tencent.polaris + spring-cloud-plugin-common + ${project.version} + org.springframework.cloud spring-cloud-starter-openfeign diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java index 8e544ba2..9f346cac 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/MainPlugin.java @@ -31,6 +31,7 @@ import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationParserInterceptor; import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationPostProcessorInterceptor; import cn.polarismesh.agent.plugin.spring.cloud.interceptor.RegisterBeanInterceptor; +import cn.polarismesh.agent.plugin.spring.cloud.interceptor.SpringFactoriesLoaderInterceptor; /** * Polaris Spring Cloud hoxton Plugin @@ -57,6 +58,9 @@ private void addPolarisTransformers(TransformOperations operations) { // 注入bean定义的调整设置 operations.transform(Constant.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class); + + // 注入JNI定义 + operations.transform(Constant.SPRING_FACTORIES_LOADER, SpringFactoriesLoaderTransform.class); } public static class ConfigurationParserTransform implements TransformCallback { @@ -100,4 +104,19 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, return target.toBytecode(); } } + + public static class SpringFactoriesLoaderTransform implements TransformCallback { + + @Override + public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) throws InstrumentException { + InstrumentClass target = instrumentor.getInstrumentClass(classLoader, className, classFileBuffer); + InstrumentMethod constructMethod = target.getDeclaredMethod("loadSpringFactories", "java.lang.ClassLoader"); + if (constructMethod != null) { + constructMethod.addInterceptor(SpringFactoriesLoaderInterceptor.class); + } + + return target.toBytecode(); + } + } + } \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java index 62a8c583..baf0d987 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java @@ -15,6 +15,11 @@ import org.springframework.core.env.Environment; public class CircuitBreakerBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-starter-tencent-polaris-circuitbreaker"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisCircuitBreakerBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerBootstrapConfiguration.class, "polarisCircuitBreakerBootstrapConfiguration"); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java index d7e01c68..b717a404 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java @@ -32,6 +32,11 @@ import org.springframework.core.env.Environment; public class CommonBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-tencent-commons"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java index 1611ef55..59cda9f6 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java @@ -32,6 +32,11 @@ import org.springframework.core.env.Environment; public class ConfigBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-starter-tencent-polaris-config"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java index 7abe57cc..bdebf8bf 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java @@ -30,6 +30,11 @@ import org.springframework.core.env.Environment; public class LoadbalancerBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-tencent-polaris-loadbalancer"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java index d1f9a783..b27d5688 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java @@ -32,6 +32,11 @@ import org.springframework.core.env.Environment; public class LosslessBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-tencent-lossless-plugin"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object losslessPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessPropertiesBootstrapConfiguration.class, "losslessPropertiesBootstrapConfiguration"); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java index bfe563de..26252c99 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java @@ -31,6 +31,11 @@ public class MetadataTransferBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-starter-tencent-metadata-transfer"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java index e18fb992..892c0b9e 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java @@ -19,6 +19,10 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; @@ -33,6 +37,18 @@ import org.springframework.core.env.Environment; public class PolarisContextBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-tencent-polaris-context"; + } + + @Override + public Map> getClassNameForType() { + Map> values = new HashMap<>(); + values.put("org.springframework.context.ApplicationListener", Collections.singletonList("com.tencent.cloud.polaris.context.logging.PolarisLoggingApplicationListener")); + return values; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { @@ -55,13 +71,5 @@ public void onApplicationStartup(Object configurationParser, ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextPostConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisContextPostConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisContextPostConfiguration.class).getBeanDefinition()); - Object polarisLoggingApplicationListener = ReflectionUtils.invokeConstructor(configClassCreator, PolarisLoggingApplicationListener.class, "polarisLoggingApplicationListener"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisLoggingApplicationListener, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisLoggingApplicationListener", BeanDefinitionBuilder.genericBeanDefinition( - PolarisLoggingApplicationListener.class).getBeanDefinition()); -// Object polarisSDKContextManager = ReflectionUtils.invokeConstructor(configClassCreator, PolarisSDKContextManager.class, "polarisSDKContextManager"); -// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisSDKContextManager, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("polarisSDKContextManager", BeanDefinitionBuilder.genericBeanDefinition( -// PolarisSDKContextManager.class).getBeanDefinition()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java index 62af49b3..ad46e3ba 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java @@ -16,6 +16,11 @@ import org.springframework.core.env.Environment; public class RateLimitBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-starter-tencent-polaris-ratelimit"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisRateLimitPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitPropertiesBootstrapConfiguration.class, "polarisRateLimitPropertiesBootstrapConfiguration"); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java index 8cdb826c..4ac870e8 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java @@ -35,6 +35,11 @@ import org.springframework.core.env.Environment; public class RegistryBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-starter-tencent-polaris-discovery"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); @@ -65,9 +70,5 @@ public void onApplicationStartup(Object configurationParser, Constructor conf ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisDiscoveryEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisDiscoveryEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisDiscoveryEndpointAutoConfiguration.class).getBeanDefinition()); -// Object polarisFeignBeanPostProcessor = ReflectionUtils.invokeConstructor(configClassCreator, PolarisFeignBeanPostProcessor.class, "polarisFeignBeanPostProcessor"); -// ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisFeignBeanPostProcessor, Constant.DEFAULT_EXCLUSION_FILTER); -// registry.registerBeanDefinition("polarisFeignBeanPostProcessor", BeanDefinitionBuilder.genericBeanDefinition( -// PolarisFeignBeanPostProcessor.class).getBeanDefinition()); } } \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java index 4d6e8c53..ceebc757 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java @@ -35,6 +35,11 @@ import org.springframework.core.io.ClassPathResource; public class RouterBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-starter-tencent-polaris-router"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java index ae586d00..8463ba1c 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java @@ -34,6 +34,11 @@ import org.springframework.core.env.Environment; public class RpcEnhancementBeanInjector implements BeanInjector { + @Override + public String getModule() { + return "spring-cloud-tencent-rpc-enhancement"; + } + @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { Object polarisStatPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesBootstrapConfiguration.class, "polarisStatPropertiesBootstrapConfiguration"); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java index b70de140..5c177fd4 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationParserInterceptor.java @@ -40,6 +40,8 @@ import cn.polarismesh.agent.plugin.spring.cloud.inject.RegistryBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.RouterBeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.inject.RpcEnhancementBeanInjector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; @@ -49,6 +51,8 @@ public class ConfigurationParserInterceptor implements Interceptor { + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationParserInterceptor.class); + private final List beanInjectors = new ArrayList<>(); public ConfigurationParserInterceptor() { @@ -99,8 +103,10 @@ public void after(Object target, Object[] args, Object result, Throwable throwab BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry"); Environment environment = (Environment) ReflectionUtils.getObjectByFieldName(target, "environment"); for (BeanInjector beanInjector : beanInjectors) { + LOGGER.info("[PolarisJavaAgent] start to inject bootstrap bean definitions in module {}", beanInjector.getModule()); beanInjector.onBootstrapStartup(target, constructor, processConfigurationClass, registry, environment); } + LOGGER.info("[PolarisJavaAgent] successfully injected spring cloud tencent bootstrap bean definitions"); } else if (isMainBeanDefinition(beanDefinitionHolder)) { Class clazz = ClassUtils.getClazz("org.springframework.context.annotation.ConfigurationClass", null); @@ -111,8 +117,10 @@ public void after(Object target, Object[] args, Object result, Throwable throwab BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ReflectionUtils.getObjectByFieldName(target, "registry"); Environment environment = (Environment) ReflectionUtils.getObjectByFieldName(target, "environment"); for (BeanInjector beanInjector : beanInjectors) { + LOGGER.info("[PolarisJavaAgent] start to inject application bean definitions in module {}", beanInjector.getModule()); beanInjector.onApplicationStartup(target, constructor, processConfigurationClass, registry, environment); } + LOGGER.info("[PolarisJavaAgent] successfully injected spring cloud tencent application bean definitions"); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java index ca0115c4..0f116f23 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java @@ -41,8 +41,10 @@ public void before(Object target, Object[] args) { return; } for (PropertiesPropertySource propertiesPropertySource : propertySources) { + LOGGER.info("[PolarisJavaAgent] start to add propertiesPropertySource {}", propertiesPropertySource.getName()); environment.getPropertySources().addFirst(propertiesPropertySource); } + LOGGER.info("[PolarisJavaAgent] successfully injected agent properties into environment, size is " + propertySources.size()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/RegisterBeanInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/RegisterBeanInterceptor.java index 6e98df0d..99454caa 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/RegisterBeanInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/RegisterBeanInterceptor.java @@ -46,5 +46,6 @@ public void before(Object target, Object[] args) { } BeanDefinition beanDefinition = (BeanDefinition) args[1]; beanDefinition.setPrimary(true); + LOGGER.info("[PolarisJavaAgent] bean {} has been made primary", beanName); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/SpringFactoriesLoaderInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/SpringFactoriesLoaderInterceptor.java new file mode 100644 index 00000000..7806c862 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/SpringFactoriesLoaderInterceptor.java @@ -0,0 +1,102 @@ +/* + * 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.interceptor; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; + +import cn.polarismesh.agent.core.extension.interceptor.Interceptor; +import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.CircuitBreakerBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.CommonBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.ConfigBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.LoadbalancerBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.LosslessBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.MetadataTransferBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.PolarisContextBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.RateLimitBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.RegistryBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.RouterBeanInjector; +import cn.polarismesh.agent.plugin.spring.cloud.inject.RpcEnhancementBeanInjector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SpringFactoriesLoaderInterceptor implements Interceptor { + + private static final Logger LOGGER = LoggerFactory.getLogger(SpringFactoriesLoaderInterceptor.class); + + private final List beanInjectors = new ArrayList<>(); + + private final Map parsedClasses = new ConcurrentHashMap<>(); + + public SpringFactoriesLoaderInterceptor() { + beanInjectors.add(new CommonBeanInjector()); + beanInjectors.add(new PolarisContextBeanInjector()); + beanInjectors.add(new MetadataTransferBeanInjector()); + beanInjectors.add(new RegistryBeanInjector()); + beanInjectors.add(new ConfigBeanInjector()); + beanInjectors.add(new RpcEnhancementBeanInjector()); + beanInjectors.add(new LosslessBeanInjector()); + beanInjectors.add(new LoadbalancerBeanInjector()); + beanInjectors.add(new RouterBeanInjector()); + beanInjectors.add(new CircuitBreakerBeanInjector()); + beanInjectors.add(new RateLimitBeanInjector()); + } + + + @SuppressWarnings("unchecked") + @Override + public void after(Object target, Object[] args, Object result, Throwable throwable) { + if (args[0] == null) { + return; + } + ClassLoader classLoader = (ClassLoader)args[0]; + parsedClasses.computeIfAbsent(classLoader, new Function() { + @Override + public Boolean apply(ClassLoader classLoader) { + Map> loadedClasses = (Map>) result; + + for (BeanInjector beanInjector : beanInjectors) { + LOGGER.info("[PolarisJavaAgent] start to inject JNI definition in module {}", beanInjector.getModule()); + Map> classNames = beanInjector.getClassNameForType(); + if (classNames.isEmpty()) { + continue; + } + for (Map.Entry> entry : classNames.entrySet()) { + List existsValues = loadedClasses.get(entry.getKey()); + List toAddValues = entry.getValue(); + if (null != existsValues) { + for (String toAddValue : toAddValues) { + if (existsValues.contains(toAddValue)) { + continue; + } + existsValues.add(toAddValue); + } + } else { + classNames.put(entry.getKey(), toAddValues); + } + } + } + return true; + } + }); + } +} diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/pom.xml b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/pom.xml new file mode 100644 index 00000000..5d44d94f --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/pom.xml @@ -0,0 +1,50 @@ + + + spring-cloud-plugins + com.tencent.polaris + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-plugin-common + jar + + Spring Cloud Plugin Common + + + UTF-8 + 2.3.12.RELEASE + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring.boot.version} + pom + import + + + + + + + org.springframework + spring-core + provided + + + org.springframework + spring-beans + provided + + + org.slf4j + slf4j-api + provided + + + diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java similarity index 73% rename from polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java rename to polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java index 08826dc8..10558fcd 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/BeanInjector.java @@ -19,21 +19,42 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public interface BeanInjector { + /** + * Spring Cloud Tencent的模块信息 + * @return 模块信息 + */ + String getModule(); + + /** + * 获取需要通过JNI加载的类名称 + * @return 类型列表 + */ + default Map> getClassNameForType() { + return Collections.emptyMap(); + } + /** * 在Bootstrap启动过程中进行Bean装载 */ - void onBootstrapStartup(Object configurationParser, - Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment); + default void onBootstrapStartup(Object configurationParser, + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + + } /** * 在应用启动过程中进行Bean装载 */ - void onApplicationStartup(Object configurationParser, - Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment); + default void onApplicationStartup(Object configurationParser, + Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + + } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java similarity index 91% rename from polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java rename to polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java index d0d325db..79b351d5 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Constant.java @@ -30,6 +30,8 @@ public interface Constant { String BEAN_DEFINITION_REGISTRY = "org.springframework.beans.factory.support.DefaultListableBeanFactory"; + String SPRING_FACTORIES_LOADER = "org.springframework.core.io.support.SpringFactoriesLoader"; + Predicate DEFAULT_EXCLUSION_FILTER = className -> (className.startsWith("java.lang.annotation.") || className.startsWith("org.springframework.stereotype.")); diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java similarity index 97% rename from polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java rename to polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java index 22695c5d..e970716f 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java @@ -27,7 +27,6 @@ import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; -import com.tencent.polaris.api.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,7 +61,7 @@ public static List loadPropertiesSource() { Properties properties = new Properties(); String confPath = Paths.get(configPath, "plugin", "spring-cloud-hoxton", "application.properties").toString(); String cmdVal = System.getProperty("polaris.agent.user.application.conf"); - if (StringUtils.isNotBlank(cmdVal)) { + if (null != cmdVal && !cmdVal.isEmpty()) { confPath = cmdVal; } try { From 6782b43d535562c54b8e661f444cf722e268fd56 Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Tue, 14 May 2024 10:17:30 +0800 Subject: [PATCH 23/24] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E8=84=9A=E6=9C=AC=E9=85=8D=E7=BD=AE=E9=A1=B9=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- polaris-agent-build/bin/start.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/polaris-agent-build/bin/start.sh b/polaris-agent-build/bin/start.sh index c18cfe36..e20d5ace 100644 --- a/polaris-agent-build/bin/start.sh +++ b/polaris-agent-build/bin/start.sh @@ -43,13 +43,13 @@ region="$(curl -s --connect-timeout 10 -m 10 http://metadata.tencentyun.com/late region_code=$? echo "region is ${region}, return code is ${region_code}" if [ ${region_code} -eq 0 ] && [ -n ${region} ]; then - sed -i "s/spring.cloud.tencent.metadata.content.region=\"\"/spring.cloud.tencent.metadata.content.zone=${region}/g" ${target_config_file} + sed -i "s/spring.cloud.tencent.metadata.content.zone=\"\"/spring.cloud.tencent.metadata.content.zone=${region}/g" ${target_config_file} fi echo "start to fetch zone" zone="$(curl -s --connect-timeout 10 -m 10 http://metadata.tencentyun.com/latest/meta-data/placement/zone)" zone_code=$? -echo "region is ${zone}, return code is ${zone_code}" +echo "zone is ${zone}, return code is ${zone_code}" if [ ${zone_code} -eq 0 ] && [ -n ${zone} ]; then - sed -i "s/spring.cloud.tencent.metadata.content.zone=\"\"/spring.cloud.tencent.metadata.content.campus=${zone}/g" ${target_config_file} + sed -i "s/spring.cloud.tencent.metadata.content.campus=\"\"/spring.cloud.tencent.metadata.content.campus=${zone}/g" ${target_config_file} fi \ No newline at end of file From 11371198bba1bc2166d1f2b3130fff28e6b3ae5f Mon Sep 17 00:00:00 2001 From: andrew shan <45474304+andrewshan@users.noreply.github.com> Date: Tue, 14 May 2024 21:32:36 +0800 Subject: [PATCH 24/24] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=A0?= =?UTF-8?q?=E6=B3=95javaagent=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=9A=84spring.application.name=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gray-release-examples/service-a/pom.xml | 4 - .../src/main/resources/application.properties | 4 +- ...cation.properties => bootstrap.properties} | 4 +- .../src/main/resources/application.properties | 7 +- .../src/main/resources/bootstrap.properties | 36 +++++ .../src/main/resources/application.properties | 9 +- .../src/main/resources/bootstrap.properties | 7 + .../src/main/resources/application.properties | 16 +- .../src/main/resources/bootstrap.properties | 11 +- ...cation.properties => bootstrap.properties} | 0 .../inject/CircuitBreakerBeanInjector.java | 151 +++++++++++------- .../cloud/inject/CommonBeanInjector.java | 16 +- .../cloud/inject/ConfigBeanInjector.java | 23 +++ .../inject/LoadbalancerBeanInjector.java | 15 +- .../cloud/inject/LosslessBeanInjector.java | 23 +++ .../inject/MetadataTransferBeanInjector.java | 15 +- .../inject/PolarisContextBeanInjector.java | 25 ++- .../cloud/inject/RateLimitBeanInjector.java | 22 +++ .../cloud/inject/RegistryBeanInjector.java | 23 +++ .../cloud/inject/RouterBeanInjector.java | 15 +- .../inject/RpcEnhancementBeanInjector.java | 41 +++-- ...ConfigurationPostProcessorInterceptor.java | 7 +- .../src/main/resources/default-plugin.conf | 31 ++-- .../cloud/common/PropertiesProvider.java | 3 +- .../plugin/spring/cloud/common/Utils.java | 60 +++++++ 25 files changed, 418 insertions(+), 150 deletions(-) rename polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/{application.properties => bootstrap.properties} (80%) create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/bootstrap.properties create mode 100644 polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/bootstrap.properties rename polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/{application.properties => bootstrap.properties} (100%) create mode 100644 polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Utils.java diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml index 60431d36..69178afb 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/pom.xml @@ -48,10 +48,6 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery - - com.alibaba.cloud - spring-cloud-starter-alibaba-nacos-config - org.springframework.cloud diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties index d49c6280..0197ebf6 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-a/src/main/resources/application.properties @@ -1,10 +1,8 @@ server.port=65203 spring.application.name=service-a-hoxton -spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.enabled=true spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.config.enabled=false -spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 spring.cloud.nacos.username='nacos' spring.cloud.nacos.password='nacos' management.endpoint.health.show-details=always diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/bootstrap.properties similarity index 80% rename from polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.properties rename to polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/bootstrap.properties index c9affe8c..54a77d36 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-b/src/main/resources/bootstrap.properties @@ -1,9 +1,9 @@ server.port=65202 spring.application.name=service-b-hoxton -spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.enabled=true spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.config.enabled=false +spring.cloud.nacos.config.enabled=true spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 spring.cloud.nacos.username='nacos' spring.cloud.nacos.password='nacos' diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.properties index e7bd9c13..1913c577 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/application.properties @@ -1,11 +1,6 @@ server.port=65201 -spring.application.name=service-c-hoxton -spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.enabled=true spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.config.enabled=false -spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 -spring.cloud.nacos.username='nacos' -spring.cloud.nacos.password='nacos' management.endpoint.health.show-details=always management.endpoints.web.exposure.include='*' \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/bootstrap.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/bootstrap.properties new file mode 100644 index 00000000..2d1dace8 --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-c/src/main/resources/bootstrap.properties @@ -0,0 +1,36 @@ +spring.application.name=service-c-hoxton +spring.cloud.nacos.config.server-addr=127.0.0.1:8848 + +#nacos certification information +spring.cloud.nacos.username=nacos +spring.cloud.nacos.password=nacos + +## nacos-namespace cannot user 'public',cause by 'public' has special handing inside. +#spring.cloud.nacos.config.namespace=public + +## you can specify a custom name if you don't want to use the application name. +#spring.cloud.nacos.config.name=test-aaa +#spring.cloud.nacos.config.file-extension=yaml + +## not recommended. +#spring.cloud.nacos.config.refreshable-dataids=common.properties +## not recommended. +#spring.cloud.nacos.config.shared-data-ids=common.properties,base-common.properties + +## recommended. +spring.cloud.nacos.config.shared-configs[0].data-id=data-source.yaml +spring.cloud.nacos.config.shared-configs[0].refresh=true +## the default value is 'DEFAULT_GROUP' , if not specified. +#spring.cloud.nacos.config.shared-configs[0].group=GROUP_APP1 + +## not recommended. +#spring.cloud.nacos.config.ext-config[0]=ext.properties +## recommended. +spring.cloud.nacos.config.extension-configs[0].data-id=ext-data-source.yaml +spring.cloud.nacos.config.extension-configs[0].refresh=true + +# the setting of reading config log print on stdout +logging.level.com.alibaba.cloud.nacos.client=debug + + +spring.cloud.nacos.config.refresh-enabled=true \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.properties index 9bb519a1..27a27d6b 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/application.properties @@ -1,10 +1,3 @@ -server.port=65200 - -spring.application.name=sc-gateway-hoxton -spring.cloud.nacos.discovery.enabled=false +spring.cloud.nacos.discovery.enabled=true spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.config.enabled=false -spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 -spring.cloud.nacos.username='nacos' -spring.cloud.nacos.password='nacos' spring.cloud.gateway.discovery.locator.enabled=true \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/bootstrap.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/bootstrap.properties new file mode 100644 index 00000000..7451f8ac --- /dev/null +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/gray-release-examples/service-gateway/src/main/resources/bootstrap.properties @@ -0,0 +1,7 @@ +server.port=65200 + +spring.application.name=sc-gateway-hoxton +spring.cloud.nacos.config.enabled=true +spring.cloud.nacos.config.serverAddr=127.0.0.1:8848 +spring.cloud.nacos.username='nacos' +spring.cloud.nacos.password='nacos' \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties index 64337162..4941df69 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/application.properties @@ -1,11 +1,5 @@ -#server.port=65001 -#spring.application.name=service-provider-hoxton -#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -#spring.cloud.nacos.discovery.enabled=false -#spring.cloud.nacos.config.enabled=false -#spring.cloud.gateway.discovery.locator.enabled=false -#spring.cloud.nacos.username=nacos -#spring.cloud.nacos.password=nacos -# -#management.endpoints.web.exposure.include=* -#management.endpoint.health.show-details=always \ No newline at end of file +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +spring.cloud.nacos.discovery.enabled=true + +management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/bootstrap.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/bootstrap.properties index c1b8caea..53c1d73d 100644 --- a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/bootstrap.properties +++ b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/provider/src/main/resources/bootstrap.properties @@ -1,11 +1,6 @@ server.port=65001 spring.application.name=service-provider-hoxton -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 -spring.cloud.nacos.discovery.enabled=false -spring.cloud.nacos.config.enabled=false -spring.cloud.gateway.discovery.locator.enabled=false +spring.cloud.nacos.config.server-addr=127.0.0.1:8848 +spring.cloud.nacos.config.enabled=true spring.cloud.nacos.username=nacos -spring.cloud.nacos.password=nacos - -management.endpoints.web.exposure.include=* -management.endpoint.health.show-details=always \ No newline at end of file +spring.cloud.nacos.password=nacos \ No newline at end of file diff --git a/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties b/polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/bootstrap.properties similarity index 100% rename from polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/application.properties rename to polaris-agent-examples/spring-cloud-plugins-examples/spring-cloud-hoxton-examples/quickstart-examples/scg/src/main/resources/bootstrap.properties diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java index baf0d987..d2e33564 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CircuitBreakerBeanInjector.java @@ -2,79 +2,110 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicBoolean; import cn.polarismesh.agent.core.common.utils.ClassUtils; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.polaris.circuitbreaker.config.*; import com.tencent.cloud.polaris.circuitbreaker.endpoint.PolarisCircuitBreakerEndpointAutoConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.cloud.openfeign.PolarisFeignCircuitBreakerTargeterAutoConfiguration; import org.springframework.core.env.Environment; -public class CircuitBreakerBeanInjector implements BeanInjector { - @Override - public String getModule() { - return "spring-cloud-starter-tencent-polaris-circuitbreaker"; - } +public class CircuitBreakerBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(CircuitBreakerBeanInjector.class); + + private final AtomicBoolean bootstrapLoaded = new AtomicBoolean(false); + + @Override + public String getModule() { + return "spring-cloud-starter-tencent-polaris-circuitbreaker"; + } - @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { - Object polarisCircuitBreakerBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerBootstrapConfiguration.class, "polarisCircuitBreakerBootstrapConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisCircuitBreakerBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisCircuitBreakerBootstrapConfiguration.class).getBeanDefinition()); - if (null != ClassUtils.getClazz("org.springframework.cloud.openfeign.Targeter", - Thread.currentThread().getContextClassLoader())) { - String property = environment.getProperty("feign.hystrix.enabled"); - if (Boolean.parseBoolean(property)) { - Object polarisFeignCircuitBreakerTargeterAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisFeignCircuitBreakerTargeterAutoConfiguration.class, "polarisFeignCircuitBreakerTargeterAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisFeignCircuitBreakerTargeterAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisFeignCircuitBreakerTargeterAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisFeignCircuitBreakerTargeterAutoConfiguration.class).getBeanDefinition()); - } - } - } + @Override + public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.circuitbreaker.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris circuitbreaker not enabled, skip inject bootstrap bean definitions for module {}", getModule()); + return; + } + bootstrapLoaded.set(true); + Object polarisCircuitBreakerBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerBootstrapConfiguration.class, "polarisCircuitBreakerBootstrapConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisCircuitBreakerBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisCircuitBreakerBootstrapConfiguration.class).getBeanDefinition()); + if (null != ClassUtils.getClazz("org.springframework.cloud.openfeign.Targeter", + Thread.currentThread().getContextClassLoader())) { + String property = environment.getProperty("feign.hystrix.enabled"); + if (Boolean.parseBoolean(property)) { + Object polarisFeignCircuitBreakerTargeterAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisFeignCircuitBreakerTargeterAutoConfiguration.class, "polarisFeignCircuitBreakerTargeterAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisFeignCircuitBreakerTargeterAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisFeignCircuitBreakerTargeterAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisFeignCircuitBreakerTargeterAutoConfiguration.class).getBeanDefinition()); + } + } + LOGGER.info("[PolarisJavaAgent] success to inject bootstrap bean definitions for module {}", getModule()); + } - @Override - public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { - Object polarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerAutoConfiguration.class, "polarisCircuitBreakerAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); - if (null != ClassUtils.getClazz("reactor.core.publisher.Mono", Thread.currentThread().getContextClassLoader()) - && null != ClassUtils.getClazz("reactor.core.publisher.Flux", Thread.currentThread().getContextClassLoader())) { - Object reactivePolarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, ReactivePolarisCircuitBreakerAutoConfiguration.class, "reactivePolarisCircuitBreakerAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, reactivePolarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("reactivePolarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - ReactivePolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); - } - if (null != ClassUtils.getClazz("feign.Feign", Thread.currentThread().getContextClassLoader()) - && null != ClassUtils.getClazz("org.springframework.cloud.openfeign.FeignClientFactoryBean", Thread.currentThread().getContextClassLoader())) { - Object polarisCircuitBreakerFeignClientAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerFeignClientAutoConfiguration.class, "polarisCircuitBreakerFeignClientAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerFeignClientAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisCircuitBreakerFeignClientAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisCircuitBreakerFeignClientAutoConfiguration.class).getBeanDefinition()); - } - if (null != ClassUtils.getClazz("org.springframework.web.reactive.DispatcherHandler", Thread.currentThread().getContextClassLoader()) - && null != ClassUtils.getClazz("com.tencent.cloud.polaris.circuitbreaker.config.ReactivePolarisCircuitBreakerAutoConfiguration", Thread.currentThread().getContextClassLoader()) - && null != ClassUtils.getClazz("org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory", Thread.currentThread().getContextClassLoader()) - && null != ClassUtils.getClazz("com.tencent.cloud.polaris.circuitbreaker.ReactivePolarisCircuitBreakerFactory", Thread.currentThread().getContextClassLoader()) - && null != ClassUtils.getClazz("org.springframework.cloud.gateway.config.GatewayAutoConfiguration", Thread.currentThread().getContextClassLoader())) { - String property = environment.getProperty("spring.cloud.gateway.enabled"); - if (Boolean.parseBoolean(property)) { - Object gatewayPolarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, GatewayPolarisCircuitBreakerAutoConfiguration.class, "gatewayPolarisCircuitBreakerAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, gatewayPolarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("gatewayPolarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - GatewayPolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); - } - } - Object polarisCircuitBreakerEndpointAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerEndpointAutoConfiguration.class, "polarisCircuitBreakerEndpointAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisCircuitBreakerEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisCircuitBreakerEndpointAutoConfiguration.class).getBeanDefinition()); - } + @Override + public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.circuitbreaker.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } + if (!bootstrapLoaded.get()) { + onBootstrapStartup(configurationParser, configClassCreator, processConfigurationClass, registry, environment); + } + Object polarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerAutoConfiguration.class, "polarisCircuitBreakerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); + if (null != ClassUtils.getClazz("reactor.core.publisher.Mono", Thread.currentThread().getContextClassLoader()) + && null != ClassUtils.getClazz("reactor.core.publisher.Flux", Thread.currentThread() + .getContextClassLoader())) { + Object reactivePolarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, ReactivePolarisCircuitBreakerAutoConfiguration.class, "reactivePolarisCircuitBreakerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, reactivePolarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("reactivePolarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + ReactivePolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); + } + if (null != ClassUtils.getClazz("feign.Feign", Thread.currentThread().getContextClassLoader()) + && null != ClassUtils.getClazz("org.springframework.cloud.openfeign.FeignClientFactoryBean", Thread.currentThread() + .getContextClassLoader())) { + Object polarisCircuitBreakerFeignClientAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerFeignClientAutoConfiguration.class, "polarisCircuitBreakerFeignClientAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerFeignClientAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisCircuitBreakerFeignClientAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisCircuitBreakerFeignClientAutoConfiguration.class).getBeanDefinition()); + } + if (null != ClassUtils.getClazz("org.springframework.web.reactive.DispatcherHandler", Thread.currentThread() + .getContextClassLoader()) + && null != ClassUtils.getClazz("com.tencent.cloud.polaris.circuitbreaker.config.ReactivePolarisCircuitBreakerAutoConfiguration", Thread.currentThread() + .getContextClassLoader()) + && null != ClassUtils.getClazz("org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory", Thread.currentThread() + .getContextClassLoader()) + && null != ClassUtils.getClazz("com.tencent.cloud.polaris.circuitbreaker.ReactivePolarisCircuitBreakerFactory", Thread.currentThread() + .getContextClassLoader()) + && null != ClassUtils.getClazz("org.springframework.cloud.gateway.config.GatewayAutoConfiguration", Thread.currentThread() + .getContextClassLoader())) { + String property = environment.getProperty("spring.cloud.gateway.enabled"); + if (Boolean.parseBoolean(property)) { + Object gatewayPolarisCircuitBreakerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, GatewayPolarisCircuitBreakerAutoConfiguration.class, "gatewayPolarisCircuitBreakerAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, gatewayPolarisCircuitBreakerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("gatewayPolarisCircuitBreakerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + GatewayPolarisCircuitBreakerAutoConfiguration.class).getBeanDefinition()); + } + } + Object polarisCircuitBreakerEndpointAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisCircuitBreakerEndpointAutoConfiguration.class, "polarisCircuitBreakerEndpointAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisCircuitBreakerEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisCircuitBreakerEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisCircuitBreakerEndpointAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); + } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java index b717a404..4c471b1b 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/CommonBeanInjector.java @@ -23,27 +23,32 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration; import com.tencent.cloud.common.metadata.endpoint.PolarisMetadataEndpointAutoConfiguration; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public class CommonBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(CommonBeanInjector.class); + @Override public String getModule() { return "spring-cloud-tencent-commons"; } - @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { - - } - @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!Utils.checkPolarisEnabled(environment)) { + LOGGER.warn("[PolarisJavaAgent] polaris not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } Object applicationContextAwareUtils = ReflectionUtils.invokeConstructor(configClassCreator, ApplicationContextAwareUtils.class, "applicationContextAwareUtils"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, applicationContextAwareUtils, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("applicationContextAwareUtils", BeanDefinitionBuilder.genericBeanDefinition( @@ -56,5 +61,6 @@ public void onApplicationStartup(Object configurationParser, Constructor conf ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisMetadataEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisMetadataEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisMetadataEndpointAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java index 59cda9f6..2480058a 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/ConfigBeanInjector.java @@ -19,19 +19,28 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicBoolean; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.endpoint.PolarisConfigEndpointAutoConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public class ConfigBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigBeanInjector.class); + + private final AtomicBoolean bootstrapLoaded = new AtomicBoolean(false); + @Override public String getModule() { return "spring-cloud-starter-tencent-polaris-config"; @@ -39,14 +48,27 @@ public String getModule() { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.config.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris config not enabled, skip inject bootstrap bean definitions for module {}", getModule()); + return; + } + bootstrapLoaded.set(true); Object polarisConfigBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigBootstrapAutoConfiguration.class, "polarisConfigBootstrapAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisConfigBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisConfigBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisConfigBootstrapAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject bootstrap bean definitions for module {}", getModule()); } @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.config.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris config not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } + if (!bootstrapLoaded.get()) { + onBootstrapStartup(configurationParser, configClassCreator, processConfigurationClass, registry, environment); + } Object polarisConfigAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisConfigAutoConfiguration.class, "polarisConfigAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisConfigAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisConfigAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -55,5 +77,6 @@ public void onApplicationStartup(Object configurationParser, Constructor conf ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisConfigEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisConfigEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisConfigEndpointAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java index bdebf8bf..2f70f39c 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LoadbalancerBeanInjector.java @@ -23,28 +23,33 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public class LoadbalancerBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(LoadbalancerBeanInjector.class); @Override public String getModule() { return "spring-cloud-tencent-polaris-loadbalancer"; } - @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { - - } - @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.loadbalancer.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris loadbalancer not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } Object polarisLoadBalancerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisLoadBalancerAutoConfiguration.class, "polarisLoadBalancerAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisLoadBalancerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisLoadBalancerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisLoadBalancerAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java index b27d5688..109533f2 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/LosslessBeanInjector.java @@ -19,19 +19,28 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicBoolean; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.plugin.lossless.config.LosslessAutoConfiguration; import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesAutoConfiguration; import com.tencent.cloud.plugin.lossless.config.LosslessPropertiesBootstrapConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public class LosslessBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(LosslessBeanInjector.class); + + private final AtomicBoolean bootstrapLoaded = new AtomicBoolean(false); + @Override public String getModule() { return "spring-cloud-tencent-lossless-plugin"; @@ -39,14 +48,27 @@ public String getModule() { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.lossless.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris lossless not enabled, skip inject bootstrap bean definitions for module {}", getModule()); + return; + } + bootstrapLoaded.set(true); Object losslessPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessPropertiesBootstrapConfiguration.class, "losslessPropertiesBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, losslessPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("losslessPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( LosslessPropertiesBootstrapConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject bootstrap bean definitions for module {}", getModule()); } @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.lossless.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris lossless not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } + if (!bootstrapLoaded.get()) { + onBootstrapStartup(configurationParser, configClassCreator, processConfigurationClass, registry, environment); + } Object losslessPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, LosslessPropertiesAutoConfiguration.class, "losslessPropertiesAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, losslessPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("losslessPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -55,5 +77,6 @@ public void onApplicationStartup(Object configurationParser, Constructor conf ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, losslessAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("losslessAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( LosslessAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java index 26252c99..5115e255 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/MetadataTransferBeanInjector.java @@ -23,7 +23,10 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.metadata.config.MetadataTransferAutoConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -31,22 +34,24 @@ public class MetadataTransferBeanInjector implements BeanInjector { + private static final Logger LOGGER = LoggerFactory.getLogger(MetadataTransferBeanInjector.class); + @Override public String getModule() { return "spring-cloud-starter-tencent-metadata-transfer"; } - @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { - - } - @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment))) { + LOGGER.warn("[PolarisJavaAgent] polaris not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } Object metadataTransferAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, MetadataTransferAutoConfiguration.class, "metadataTransferAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, metadataTransferAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("metadataTransferAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( MetadataTransferAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java index 892c0b9e..8a7f3e2f 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/PolarisContextBeanInjector.java @@ -23,20 +23,29 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.context.config.PolarisContextBootstrapAutoConfiguration; import com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration; import com.tencent.cloud.polaris.context.logging.PolarisLoggingApplicationListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public class PolarisContextBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(PolarisContextBeanInjector.class); + + private final AtomicBoolean bootstrapLoaded = new AtomicBoolean(false); + @Override public String getModule() { return "spring-cloud-tencent-polaris-context"; @@ -52,17 +61,28 @@ public Map> getClassNameForType() { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment))) { + LOGGER.warn("[PolarisJavaAgent] polaris not enabled, skip inject bootstrap bean definitions for module {}", getModule()); + return; + } + bootstrapLoaded.set(true); Object polarisContextBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisContextBootstrapAutoConfiguration.class, "polarisContextBootstrapAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisContextBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisContextBootstrapAutoConfiguration.class).getBeanDefinition()); - - + LOGGER.info("[PolarisJavaAgent] success to inject bootstrap bean definitions for module {}", getModule()); } @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment))) { + LOGGER.warn("[PolarisJavaAgent] polaris not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } + if (!bootstrapLoaded.get()) { + onBootstrapStartup(configurationParser, configClassCreator, processConfigurationClass, registry, environment); + } Object polarisContextAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisContextAutoConfiguration.class, "polarisContextAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisContextAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -71,5 +91,6 @@ public void onApplicationStartup(Object configurationParser, ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisContextPostConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisContextPostConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisContextPostConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java index ad46e3ba..8e46520a 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RateLimitBeanInjector.java @@ -2,20 +2,28 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicBoolean; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitAutoConfiguration; import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesAutoConfiguration; import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitPropertiesBootstrapConfiguration; import com.tencent.cloud.polaris.ratelimit.endpoint.PolarisRateLimitRuleEndpointAutoConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public class RateLimitBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(RateLimitBeanInjector.class); + + private final AtomicBoolean bootstrapLoaded = new AtomicBoolean(false); @Override public String getModule() { return "spring-cloud-starter-tencent-polaris-ratelimit"; @@ -23,14 +31,27 @@ public String getModule() { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.ratelimit.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris ratelimit not enabled, skip inject bootstrap bean definitions for module {}", getModule()); + return; + } + bootstrapLoaded.set(true); Object polarisRateLimitPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitPropertiesBootstrapConfiguration.class, "polarisRateLimitPropertiesBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisRateLimitPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisRateLimitPropertiesBootstrapConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject bootstrap bean definitions for module {}", getModule()); } @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.ratelimit.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris ratelimit not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } + if (!bootstrapLoaded.get()) { + onBootstrapStartup(configurationParser, configClassCreator, processConfigurationClass, registry, environment); + } Object polarisRateLimitAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisRateLimitAutoConfiguration.class, "polarisRateLimitAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisRateLimitAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -43,5 +64,6 @@ public void onApplicationStartup(Object configurationParser, Constructor conf ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisRateLimitRuleEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisRateLimitRuleEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisRateLimitRuleEndpointAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java index 4ac870e8..ddb8cfb5 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RegistryBeanInjector.java @@ -19,22 +19,31 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicBoolean; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.DiscoveryPropertiesBootstrapAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.endpoint.PolarisDiscoveryEndpointAutoConfiguration; import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration; import com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public class RegistryBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(RegistryBeanInjector.class); + + private final AtomicBoolean bootstrapLoaded = new AtomicBoolean(false); + @Override public String getModule() { return "spring-cloud-starter-tencent-polaris-discovery"; @@ -42,14 +51,27 @@ public String getModule() { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.discovery.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris discovery not enabled, skip inject bootstrap bean definitions for module {}", getModule()); + return; + } + bootstrapLoaded.set(true); Object discoveryPropertiesBootstrapAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, DiscoveryPropertiesBootstrapAutoConfiguration.class, "discoveryPropertiesBootstrapAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, discoveryPropertiesBootstrapAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("discoveryPropertiesBootstrapAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( DiscoveryPropertiesBootstrapAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject bootstrap bean definitions for module {}", getModule()); } @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.discovery.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris discovery not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } + if (!bootstrapLoaded.get()) { + onBootstrapStartup(configurationParser, configClassCreator, processConfigurationClass, registry, environment); + } Object discoveryPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, DiscoveryPropertiesAutoConfiguration.class, "discoveryPropertiesAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, discoveryPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("discoveryPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -70,5 +92,6 @@ public void onApplicationStartup(Object configurationParser, Constructor conf ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisDiscoveryEndpointAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisDiscoveryEndpointAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( PolarisDiscoveryEndpointAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java index ceebc757..e4dc9cf4 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RouterBeanInjector.java @@ -25,9 +25,12 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.polaris.router.config.FeignAutoConfiguration; import com.tencent.cloud.polaris.router.config.RouterAutoConfiguration; import com.tencent.cloud.polaris.router.endpoint.PolarisRouterEndpointAutoConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -35,19 +38,20 @@ import org.springframework.core.io.ClassPathResource; public class RouterBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(RouterBeanInjector.class); @Override public String getModule() { return "spring-cloud-starter-tencent-polaris-router"; } - @Override - public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { - - } - @Override @SuppressWarnings("unchecked") public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.polaris.router.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris router not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } Object routerAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RouterAutoConfiguration.class, "routerAutoConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, routerAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("routerAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( @@ -80,5 +84,6 @@ public void onApplicationStartup(Object configurationParser, Constructor conf if (null != targetConfigClass) { configurationClasses.put(targetConfigClass, targetConfigClass); } + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java index 8463ba1c..dc4d44f0 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/inject/RpcEnhancementBeanInjector.java @@ -19,21 +19,30 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicBoolean; import cn.polarismesh.agent.core.common.utils.ClassUtils; import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.plugin.spring.cloud.common.BeanInjector; import cn.polarismesh.agent.plugin.spring.cloud.common.Constant; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration; import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementBootstrapConfiguration; import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration; import com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesBootstrapConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.core.env.Environment; public class RpcEnhancementBeanInjector implements BeanInjector { + + private static final Logger LOGGER = LoggerFactory.getLogger(RpcEnhancementBeanInjector.class); + + private final AtomicBoolean bootstrapLoaded = new AtomicBoolean(false); + @Override public String getModule() { return "spring-cloud-tencent-rpc-enhancement"; @@ -41,25 +50,39 @@ public String getModule() { @Override public void onBootstrapStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.tencent.rpc-enhancement.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris rpc-enhancement not enabled, skip inject bootstrap bean definitions for module {}", getModule()); + return; + } + bootstrapLoaded.set(true); Object polarisStatPropertiesBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesBootstrapConfiguration.class, "polarisStatPropertiesBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisStatPropertiesBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("polarisStatPropertiesBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisStatPropertiesAutoConfiguration.class).getBeanDefinition()); + PolarisStatPropertiesBootstrapConfiguration.class).getBeanDefinition()); Object rpcEnhancementBootstrapConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RpcEnhancementBootstrapConfiguration.class, "rpcEnhancementBootstrapConfiguration"); ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, rpcEnhancementBootstrapConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); registry.registerBeanDefinition("rpcEnhancementBootstrapConfiguration", BeanDefinitionBuilder.genericBeanDefinition( RpcEnhancementBootstrapConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject bootstrap bean definitions for module {}", getModule()); } @Override public void onApplicationStartup(Object configurationParser, Constructor configClassCreator, Method processConfigurationClass, BeanDefinitionRegistry registry, Environment environment) { - Object polarisStatPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesAutoConfiguration.class, "polarisStatPropertiesAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisStatPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("polarisStatPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - PolarisStatPropertiesBootstrapConfiguration.class).getBeanDefinition()); - Object rpcEnhancementAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RpcEnhancementAutoConfiguration.class, "rpcEnhancementAutoConfiguration"); - ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, rpcEnhancementAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); - registry.registerBeanDefinition("rpcEnhancementAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( - RpcEnhancementAutoConfiguration.class).getBeanDefinition()); + if (!(Utils.checkPolarisEnabled(environment) && Utils.checkKeyEnabled(environment, "spring.cloud.tencent.rpc-enhancement.enabled"))) { + LOGGER.warn("[PolarisJavaAgent] polaris rpc-enhancement not enabled, skip inject application bean definitions for module {}", getModule()); + return; + } + if (!bootstrapLoaded.get()) { + onBootstrapStartup(configurationParser, configClassCreator, processConfigurationClass, registry, environment); + } + Object polarisStatPropertiesAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, PolarisStatPropertiesAutoConfiguration.class, "polarisStatPropertiesAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, polarisStatPropertiesAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("polarisStatPropertiesAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + PolarisStatPropertiesAutoConfiguration.class).getBeanDefinition()); + Object rpcEnhancementAutoConfiguration = ReflectionUtils.invokeConstructor(configClassCreator, RpcEnhancementAutoConfiguration.class, "rpcEnhancementAutoConfiguration"); + ReflectionUtils.invokeMethod(processConfigurationClass, configurationParser, rpcEnhancementAutoConfiguration, Constant.DEFAULT_EXCLUSION_FILTER); + registry.registerBeanDefinition("rpcEnhancementAutoConfiguration", BeanDefinitionBuilder.genericBeanDefinition( + RpcEnhancementAutoConfiguration.class).getBeanDefinition()); + LOGGER.info("[PolarisJavaAgent] success to inject application bean definitions for module {}", getModule()); } } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java index 0f116f23..b444ed27 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/interceptor/ConfigurationPostProcessorInterceptor.java @@ -22,6 +22,7 @@ import cn.polarismesh.agent.core.common.utils.ReflectionUtils; import cn.polarismesh.agent.core.extension.interceptor.Interceptor; import cn.polarismesh.agent.plugin.spring.cloud.common.PropertiesProvider; +import cn.polarismesh.agent.plugin.spring.cloud.common.Utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,6 +36,10 @@ public class ConfigurationPostProcessorInterceptor implements Interceptor { @Override public void before(Object target, Object[] args) { ConfigurableEnvironment environment = (ConfigurableEnvironment) ReflectionUtils.getObjectByFieldName(target, "environment"); + if (!Utils.checkSpringApplicationNameExists(environment)) { + LOGGER.warn("[PolarisJavaAgent] skip inject polaris java agent configuration for no spring application name"); + return; + } List propertySources = PropertiesProvider.loadPropertiesSource(); MutablePropertySources mutablePropertySources = environment.getPropertySources(); if (mutablePropertySources.contains(propertySources.get(0).getName())) { @@ -42,7 +47,7 @@ public void before(Object target, Object[] args) { } for (PropertiesPropertySource propertiesPropertySource : propertySources) { LOGGER.info("[PolarisJavaAgent] start to add propertiesPropertySource {}", propertiesPropertySource.getName()); - environment.getPropertySources().addFirst(propertiesPropertySource); + environment.getPropertySources().addLast(propertiesPropertySource); } LOGGER.info("[PolarisJavaAgent] successfully injected agent properties into environment, size is " + propertySources.size()); } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf index bfcf6a63..f37d0470 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-hoxton-plugin/src/main/resources/default-plugin.conf @@ -1,5 +1,4 @@ # 配置北极星服务端地址 -spring.cloud.polaris.address=grpc\://127.0.0.1\:8091 spring.cloud.polaris.enabled=true # 启用 Java Agent 的 Spring Cloud Tencent 注册发现能力 @@ -11,21 +10,19 @@ spring.cloud.polaris.discovery.enabled=true spring.cloud.polaris.discovery.register=true # 是否启用北极星的 LoadBalancer -spring.cloud.polaris.loadbalancer.enabled=true -#spring.cloud.polaris.loadbalancer.discoveryType=POLARIS -#spring.cloud.polaris.loadbalancer.strategy=random +spring.cloud.polaris.loadbalancer.enabled=false # 是否启用北极星服务路由能力 -spring.cloud.polaris.router.enabled=true +spring.cloud.polaris.router.enabled=false # 是否启用北极星的自定义路由能力 -spring.cloud.polaris.router.rule-router.enabled=true +spring.cloud.polaris.router.rule-router.enabled=false # 是否启用北极星的元数据路由能力 -spring.cloud.polaris.router.metadata-router.enabled=true +spring.cloud.polaris.router.metadata-router.enabled=false # 是否启用北极星的就近路由能力 -spring.cloud.polaris.router.nearby-router.enabled=true +spring.cloud.polaris.router.nearby-router.enabled=false # 是否启用北极星的服务限流能力 -spring.cloud.polaris.ratelimit.enabled=true +spring.cloud.polaris.ratelimit.enabled=false # 设置触发限流时的提示信息 # spring.cloud.polaris.ratelimit.rejectRequestTips="" # 设置触发限流时的响应码 @@ -34,23 +31,29 @@ spring.cloud.polaris.ratelimit.rejectHttpCode=429 spring.cloud.polaris.ratelimit.maxQueuingTime=1000 # 是否启用北极星的服务熔断能力 -spring.cloud.polaris.circuitbreaker.enabled=true +spring.cloud.polaris.circuitbreaker.enabled=false ## RPC 调用增强 -spring.cloud.tencent.rpc-enhancement.enabled=true +spring.cloud.tencent.rpc-enhancement.enabled=false + # 开启 RPC 调用结果上报 -spring.cloud.tencent.rpc-enhancement.reporter.enabled=true +spring.cloud.tencent.rpc-enhancement.reporter.enabled=false # 配置北极星监控指标上报 -spring.cloud.polaris.stat.enabled=true +spring.cloud.polaris.stat.enabled=false # 指标上报监听端口 spring.cloud.polaris.stat.port=0 # 指标上报暴露的 http path spring.cloud.polaris.stat.path=/metrics -## 启用北极星配置中心能力 +# 启用北极星配置中心能力 spring.cloud.polaris.config.enabled=true # 是否开启配置监听刷新 spring.cloud.polaris.config.auto-refresh=true +# 启用优雅上下线功能 +spring.cloud.polaris.lossless.enabled=false +spring.cloud.polaris.lossless.port=28080 + +# 日志级别 logging.level.root=INFO \ No newline at end of file diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java index e970716f..1b0745e5 100644 --- a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/PropertiesProvider.java @@ -54,8 +54,6 @@ public static List loadPropertiesSource() { throw new IllegalStateException("fail to load file " + fileName, e); } List propertySources = new ArrayList<>(); - propertySources.add(new PropertiesPropertySource("__default_polaris_agent_spring_cloud_tencent__", defaultProperties)); - String configPath = Paths.get(System.getProperty(Constant.AGENT_CONF_PATH), "conf").toString(); LOGGER.info("load property sources from config path " + configPath); Properties properties = new Properties(); @@ -71,6 +69,7 @@ public static List loadPropertiesSource() { throw new IllegalStateException("fail to load config from " + configPath, e); } propertySources.add(new PropertiesPropertySource("__polaris_agent_spring_cloud_tencent__", properties)); + propertySources.add(new PropertiesPropertySource("__default_polaris_agent_spring_cloud_tencent__", defaultProperties)); return propertySources; }); } diff --git a/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Utils.java b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Utils.java new file mode 100644 index 00000000..34a2c6f1 --- /dev/null +++ b/polaris-agent-plugins/spring-cloud-plugins/spring-cloud-plugin-common/src/main/java/cn/polarismesh/agent/plugin/spring/cloud/common/Utils.java @@ -0,0 +1,60 @@ +/* + * 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.common; + +import org.springframework.core.env.Environment; + +public class Utils { + + public static boolean checkKeyExists(Environment environment, String key) { + if (null == environment) { + return false; + } + String property = environment.getProperty(key); + return null != property && !property.isEmpty(); + } + + public static boolean checkKeyEnabled(Environment environment, String key) { + if (null == environment) { + return false; + } + String property = environment.getProperty(key); + return Boolean.parseBoolean(property); + } + + /** + * check properties has spring.application.name + * @param environment spring cloud context environment + * @return application name + */ + public static boolean checkSpringApplicationNameExists(Environment environment) { + return checkKeyExists(environment, "spring.application.name"); + } + + + + /** + * check properties has spring.cloud.polaris.enabled=true + * @param environment spring cloud context environment + * @return application name + */ + public static boolean checkPolarisEnabled(Environment environment) { + return checkKeyEnabled(environment, "spring.cloud.polaris.enabled"); + } + +}