Skip to content

Commit

Permalink
fix:fix javaagent2023 could not start by bootstrap.yml (#189)
Browse files Browse the repository at this point in the history
* fix agent2023 could not start by bootstrap.yml

* fix:fix the bug that agent2023 could not start by bootstrap.yml

* fix class not found for routerEndpoint

* fix class not found for routerEndpoint

* Modify the overall code format

---------

Co-authored-by: fredrikliu <[email protected]>
  • Loading branch information
fredrikliu and fredrikliu authored May 30, 2024
1 parent a7c6afb commit ed7f752
Show file tree
Hide file tree
Showing 37 changed files with 1,252 additions and 1,432 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

<dependencies>
<!-- 简单的 Spring Cloud Web 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ public EchoController(RestTemplate restTemplate, Registration registration) {
this.registration = registration;
}

// @GetMapping("/echo/{str}")
// public ResponseEntity<String> rest(@PathVariable String str) {
// String content = String.format("%s[%d] -> ", registration.getServiceId(), port);
// ResponseEntity<String> response = template.getForEntity("http://service-provider-2023/echo/" + str +"123",
// String.class);
// content += response.getBody();
// return new ResponseEntity<>(content, HttpStatus.OK);
// }

@GetMapping("/echo/{str}")
public ResponseEntity<String> rest(@PathVariable String str) {
String content = String.format("%s[%d] -> ", registration.getServiceId(), port);
ResponseEntity<String> response = template.getForEntity("http://service-provider-2023/echo/" + str +"123",
ResponseEntity<String> response = template.getForEntity("http://service-provider-2023/echo/" + str,
String.class);
content += response.getBody();
return new ResponseEntity<>(content, HttpStatus.OK);
return response;
}

@GetMapping("/custom/config")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ 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'
config:
import:
- nacos:service-consumer-2023.properties?refreshEnabled=true
main:
allow-bean-definition-overriding: true
# config:
# import:
# - nacos:service-consumer-2023.properties?refreshEnabled=true
management:
endpoint:
health:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
consumer:
serviceRouter:
plugin:
# 设置就近路由插件配置
nearbyBasedRouter:
# 描述:就近路由的最小匹配级别,需要显示设置
# 范围: zone(腾讯云地域信息,eg: ap-guangzhou)、campus(腾讯云可用区, eg: ap-guangzhou-3)
matchLevel: campus
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

<dependencies>
<!-- 简单的 Spring Cloud Web 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ 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'
config:
import:
- nacos:service-provider-2023.properties?refreshEnabled=true
# config:
# import:
# - nacos:service-provider-2023.properties?refreshEnabled=true
management:
endpoint:
health:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-plugins</artifactId>
<groupId>com.tencent.polaris</groupId>
Expand Down Expand Up @@ -34,13 +34,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring Cloud Dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
Expand All @@ -63,6 +56,16 @@
</dependencyManagement>

<dependencies>
<!-- <dependency>-->
<!-- <groupId>com.google.protobuf</groupId>-->
<!-- <artifactId>protobuf-java</artifactId>-->
<!-- <version>3.21.7</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>spring-cloud-plugin-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
Expand All @@ -84,12 +87,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>

<!-- Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
Expand Down Expand Up @@ -125,10 +122,10 @@
<artifactId>polaris-all</artifactId>
</dependency>


<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.0.1-jre</version>
</dependency>

<!-- Spring Cloud Tencent -->
Expand Down Expand Up @@ -158,10 +155,10 @@
<artifactId>polaris-plugin-api</artifactId>
</exclusion>

<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-polaris-grpc</artifactId>
</exclusion>
<!-- <exclusion>-->
<!-- <groupId>com.tencent.polaris</groupId>-->
<!-- <artifactId>connector-polaris-grpc</artifactId>-->
<!-- </exclusion>-->

<exclusion>
<groupId>com.tencent.polaris</groupId>
Expand Down Expand Up @@ -257,6 +254,11 @@
</exclusions>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-contract</artifactId>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
Expand Down Expand Up @@ -367,6 +369,56 @@
</exclusions>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
<version>${spring.cloud.tencent.version}</version>
<exclusions>
<exclusion>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-rpc-enhancement</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-circuitbreaker-factory</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>healthchecker-http</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>healthchecker-udp</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>healthchecker-tcp</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
<version>${spring.cloud.tencent.version}</version>
<exclusions>
<exclusion>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-rpc-enhancement</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-ratelimit-factory</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>spring-cloud-plugin-2023-common</artifactId>
<version>1.7.0-RC2</version>
<scope>compile</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# the application name for spring cloud, register as the service name in registry center
spring.application.name=application-name
spring.application.name=service-provider-2023
# polaris server address
spring.cloud.polaris.address=grpc\://127.0.0.1\:8091
# switch for spring cloud polaris all features
Expand All @@ -26,6 +26,8 @@ spring.cloud.polaris.router.nearby-router.enabled=true
spring.cloud.polaris.ratelimit.enabled=false
# rejectHttpCode for polaris ratelimit, will be returned as limited
spring.cloud.polaris.ratelimit.rejectHttpCode=429
# switch for circuitbreaker
spring.cloud.polaris.circuitbreaker.enabled=false
# maxQueuingTime for polaris ratelimit
spring.cloud.polaris.ratelimit.maxQueuingTime=1000
# switch for rpc-enhancement feature
Expand All @@ -38,3 +40,5 @@ spring.cloud.polaris.stat.enabled=false
spring.cloud.polaris.stat.port=0
# path for polaris stat
spring.cloud.polaris.stat.path=/metrics

logging.level.root=INFO
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@
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.ApplicationContextAwareInterceptor;
import cn.polarismesh.agent.plugin.spring.cloud.interceptor.ConfigurationInjectInterceptor;

import org.springframework.context.ApplicationContext;
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 2021 Plugin
* Polaris Spring Cloud hoxton Plugin
*
* @author zhuyuhan
* @author shuhanliu
*/
public class MainPlugin implements AgentPlugin {

Expand All @@ -52,42 +51,69 @@ public void init(PluginContext context) {
*/
private void addPolarisTransformers(TransformOperations operations) {

// 在 agent 中注入 Spring 的 ApplicationContext
operations.transform(ClassNames.APPLICATION_CONTEXT_AWARE, ApplicationContextAwareTransform.class);
// 注入默认配置
operations.transform(Constant.CONFIGURATION_CLAZZ_POST_PROCESSOR, ConfigurationPostProcessorTransform.class);

// 注入bootstrap的bean定义
operations.transform(Constant.CONFIGURATION_CLAZZ_PARSER, ConfigurationParserTransform.class);

// EnvironmentPostProcessor 处理
// operations.transform(ClassNames.ENVIRONMENT_POST_PROCESSOR, ConfigurationInjectTransform.class);
// 注入bean定义的调整设置
operations.transform(Constant.BEAN_DEFINITION_REGISTRY, RegisterBeanDefinitionTransform.class);

// 注入JNI定义
operations.transform(Constant.SPRING_FACTORIES_LOADER, SpringFactoriesLoaderTransform.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.getDeclaredMethod("parse", "java.util.Set");
if (constructMethod != null) {
constructMethod.addInterceptor(ConfigurationParserInterceptor.class);
}

return target.toBytecode();
}
}

public static class ConfigurationPostProcessorTransform 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("processConfigBeanDefinitions", "org.springframework.beans.factory.support.BeanDefinitionRegistry");
if (constructMethod != null) {
constructMethod.addInterceptor(ConfigurationPostProcessorInterceptor.class);
}

return target.toBytecode();
}
}

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.getConstructor("org.springframework.context.ConfigurableApplicationContext");
InstrumentMethod constructMethod = target.getDeclaredMethod("registerBeanDefinition", "java.lang.String", "org.springframework.beans.factory.config.BeanDefinition");
if (constructMethod != null) {
constructMethod.addInterceptor(ApplicationContextAwareInterceptor.class);
constructMethod.addInterceptor(RegisterBeanInterceptor.class);
}

return target.toBytecode();
}
}

public static class ConfigurationInjectTransform implements TransformCallback {
public static class SpringFactoriesLoaderTransform 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("loadSpringFactories", "java.lang.ClassLoader");
if (constructMethod != null) {
constructMethod.addInterceptor(ConfigurationInjectInterceptor.class);
constructMethod.addInterceptor(SpringFactoriesLoaderInterceptor.class);
}

return target.toBytecode();
Expand Down
Loading

0 comments on commit ed7f752

Please sign in to comment.