JavaAgent如何实现http接口发布

其他教程   发布日期:2023年07月27日   浏览次数:468

这篇文章主要介绍“JavaAgent如何实现http接口发布”,在日常操作中,相信很多人在JavaAgent如何实现http接口发布问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”JavaAgent如何实现http接口发布”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    需求

    公司运维系统想要监控服务是否正常启动,这些服务是k8s部署的,运维人员的要求业务服务提供一个http接口用于监控服务健康监测,要求所有的接口请求的URL,参数等都是相同的,这么做的目的是不需要通过规范来约束开发人员去开一个服务健康监测的接口。

    使用服务接口来检测服务我觉得相比较监控进程启动,端口监听等方式更准确一些。所以,为了满足运维同学的要求,起初想到的方案是提供一个jar,专门集成到项目中用于发布监控接口,但是想了一下,这么做需要涉及到服务的改造,我理想的方式是对应用无侵入的方式实现。

    初步方案

    说到对应用无入侵,首先想到的就是

    1. javaagent
    技术,此前使用该技术实现了无入侵增强程序日志的工具,所以对使用
    1. javaagent
    已经没有问题,此时需要考虑的是如何发布接口了。

    基础技术 JavaAgent

    支持的技术 SpringBoot和DubboX发布的rest服务

    公司服务大致分为两类,一个是使用

    1. springboot
    发布的Spring MVC rest接口,另一种是基于
    1. DubboX
    发布的rest接口,因为公司在向服务网格转,所以按要求是去dubbo化的,没办法还是有其他小组由于一些其他原因没有或者说短期内不想进行服务改造的项目,这些项目比较老,不是springboot的,是使用spring+DubboX发布的rest服务。所以这个agent要至少能支持这两种技术。

    支持SpringBoot

    想要支持SpringBoot很简单,因为SpringBoot支持自动装配,所以,我要写一个

    1. spring.factories
    来进行自动装配。

    支持DubboX

    业务系统是传统spring+DubboX实现的,并不支持自动装配,这是个问题点,还有个问题点就是如何也发布一个DubboX的rest接口,这两个问题实际上就需要对SpringBean生命周期和Dubbo接口发布的流程有一定的了解了,这个一会儿再说。

    技术实现

    pom文件依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-web</artifactId>
    5. <version>2.3.6.RELEASE</version>
    6. <optional>true</optional>
    7. </dependency>
    8. <dependency>
    9. <groupId>org.springframework.boot</groupId>
    10. <artifactId>spring-boot-autoconfigure</artifactId>
    11. <version>2.3.6.RELEASE</version>
    12. <optional>true</optional>
    13. </dependency>
    14. <dependency>
    15. <groupId>org.springframework.boot</groupId>
    16. <artifactId>spring-boot-configuration-processor</artifactId>
    17. <version>2.3.6.RELEASE</version>
    18. <optional>true</optional>
    19. </dependency>
    20. <dependency>
    21. <groupId>com.alibaba</groupId>
    22. <artifactId>fastjson</artifactId>
    23. <version>1.2.70</version>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.apache.zookeeper</groupId>
    27. <artifactId>zookeeper</artifactId>
    28. <version>3.4.6</version>
    29. <exclusions>
    30. <exclusion>
    31. <groupId>log4j</groupId>
    32. <artifactId>log4j</artifactId>
    33. </exclusion>
    34. </exclusions>
    35. </dependency>
    36. <dependency>
    37. <groupId>com.101tec</groupId>
    38. <artifactId>zkclient</artifactId>
    39. <version>0.7</version>
    40. </dependency>
    41. <dependency>
    42. <groupId>com.alibaba</groupId>
    43. <artifactId>dubbo</artifactId>
    44. <version>2.8.4</version>
    45. <exclusions>
    46. <exclusion>
    47. <groupId>org.springframework</groupId>
    48. <artifactId>spring</artifactId>
    49. </exclusion>
    50. </exclusions>
    51. </dependency>
    52. <dependency>
    53. <groupId>javax.ws.rs</groupId>
    54. <artifactId>javax.ws.rs-api</artifactId>
    55. <version>2.0.1</version>
    56. </dependency>
    57. </dependencies>

    实现一个JavaAgent

    实现一个JavaAgent很容易,以下三步就可以了,这里不细说了。

    定义JavaAgent入口

    1. public class PreAgent {
    2. public static void premain(String args, Instrumentation inst) {
    3. System.out.println("输入参数:" + args);
    4. // 通过参数控制,发布的接口是DubboX还是SpringMVC
    5. Args.EXPORT_DUBBOX = args;
    6. }
    7. }

    Maven打包配置

    1. <plugins>
    2. <plugin>
    3. <artifactId>maven-deploy-plugin</artifactId>
    4. <configuration>
    5. <skip>true</skip>
    6. </configuration>
    7. </plugin>
    8. <plugin>
    9. <groupId>org.apache.maven.plugins</groupId>
    10. <artifactId>maven-shade-plugin</artifactId>
    11. <version>1.4</version>
    12. <executions>
    13. <execution>
    14. <phase>package</phase>
    15. <goals>
    16. <goal>shade</goal>
    17. </goals>
    18. <configuration>
    19. <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
    20. <promoteTransitiveDependencies>false</promoteTransitiveDependencies>
    21. <createDependencyReducedPom>true</createDependencyReducedPom>
    22. <minimizeJar>false</minimizeJar>
    23. <createSourcesJar>true</createSourcesJar>
    24. <transformers>
    25. <transformer
    26. implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    27. <manifestEntries>
    28. <Premain-Class>com.ruubypay.agent.PreAgent</Premain-Class>
    29. </manifestEntries>
    30. </transformer>
    31. </transformers>
    32. </configuration>
    33. </execution>
    34. </executions>
    35. </plugin>
    36. </plugins>

    MANIFEST.MF编写

    注:该文件在

    1. resource/META-INF/
    目录下

    Manifest-Version: 1.0
    Can-Redefine-Classes: true
    Can-Retransform-Classes: true
    Premain-Class: com.ruubypay.agent.PreAgent

    支持SpringBoot发布的Http接口

    编写Controller

    接口很简单就发布一个

    1. get
    接口,响应
    1. pong
    即可。
    1. @RestController
    2. public class PingServiceController {
    3. @GetMapping(value = "/agentServer/ping")
    4. public String ping() {
    5. return "pong";
    6. }
    7. }

    创建spring.factories

    通过这个配置文件可以实现SpringBoot自动装配,这里不细说SpringBoot自动装配的原理了,该文件的配置内容就是要自动装配的

    1. Bean
    的全路径,代码如下:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    com.ruubypay.config.WebConfiguration

    WebConfiguration配置类

    这个配置配置类很简单,

    1. @Configuration
    声明这是个配置类,
    1. @ComponentScan
    扫描包。
    1. @Configuration
    2. @ComponentScan(value = "com.ruubypay")
    3. public class WebConfiguration {
    4. }

    支持DubboX发布的rest接口

    定义API

    使用的是

    1. DubboX
    发布
    1. rest接口
    需要
    1. javax.ws.rs
    包的注解,
    1. @Produces({ContentType.APPLICATION_JSON_UTF_8})
    声明序列化方式,
    1. @Path
    rest接口的路径,
    1. @GET
    声明为get接口。
    1. @Produces({ContentType.APPLICATION_JSON_UTF_8})
    2. @Path("/agentServer")
    3. public interface IPingService {
    4. /**
    5. * ping接口
    6. * @return
    7. */
    8. @GET
    9. @Path("/ping")
    10. String ping();
    11. }

    编写API实现类

    1. @Component("IPingService")
    2. public class IPingServiceImpl implements IPingService {
    3. @Override
    4. public String ping() {
    5. return "pong";
    6. }
    7. }

    实现发布Dubbo接口

    如何实现发布接口是实现的难点;首先程序并不支持自动装配了,我们就要考虑如何获取到

    1. Spring
    上下文,如果能够注册
    1. Bean
    1. Spring容器
    中,如何触发发布
    1. Dubbo接口
    等问题。

    Spring上下文获取及注册Bean到Spring容器中

    触发Bean注册,获取Spring上下文我们通过Spring的Aware接口可以实现,我这里使用的是

    1. ApplicationContextAware
    ;注册
    1. Bean
    1. Spring容器
    中可以使用
    1. BeanDefinition
    先创建Bean然后使用
    1. DefaultListableBeanFactory
    1. registerBeanDefinition
    1. BeanDefinition
    注册到Spring上下文中。
    1. @Component
    2. public class AgentAware implements ApplicationContextAware {
    3. private static final String DUBBOX = "1";
    4. private ApplicationContext applicationContext;
    5. @Override
    6. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    7. this.applicationContext = applicationContext;
    8. // 如果不是DubboX,不用发布接口
    9. if (DUBBOX.equals(Args.EXPORT_DUBBOX)) {
    10. // 注册配置Bean WebConfiguration
    11. webConfiguration();
    12. // 发布DubboX接口
    13. exportDubboxService();
    14. }
    15. }
    16. public void webConfiguration() {
    17. System.out.println("创建WebConfiguration的bean");
    18. ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
    19. DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getAutowireCapableBeanFactory();
    20. // 创建WebConfiguration的bean
    21. BeanDefinition webConfigurationBeanDefinition = new RootBeanDefinition(WebConfiguration.class);
    22. // 注册到集合beanFactory中
    23. System.out.println("注册到集合beanFactory中");
    24. listableBeanFactory.registerBeanDefinition(WebConfiguration.class.getName(), webConfigurationBeanDefinition);
    25. }
    26. }

    发布Dubbo接口

    通过

    1. ApplicationContextAware
    我们已经能够获取Spring上下文了,也就是说应用程序的Dubbo注册中心,发布接口协议,Dubbo Application等配置都已经存在Spring容器中了,我们只要拿过来使用即可,拿过来使用没问题,我们接下来就需要考虑,如何发布接口,这需要对Dubbo服务发布的流程有一定的了解,这里我不细说了,感兴趣的可以自己了解下,或者看我以前发布的文章;

    首先Dubbo接口的Provider端的核心Bean是

    1. com.alibaba.dubbo.config.spring.ServiceBean
    ,使用Spring配置文件中的标签
    1. <dubbo:service
    标签生成的Bean就是
    1. ServiceBean
    ,所以,这里我们只需要创建
    1. ServiceBean
    对象并且初始化对象中的必要数据,然后调用
    1. ServiceBean#export()
    方法就可以发布Dubbo服务了。

    这里需要的对象直接通过依赖查找的方式从Spring容器获取就可以了

    1. ApplicationConfig
    ,
    1. ProtocolConfig
    ,
    1. RegistryConfig
    ,
    1. IPingService
    1. public void exportDubboxService() {
    2. try {
    3. System.out.println("开始发布dubbo接口");
    4. // 获取ApplicationConfig
    5. ApplicationConfig applicationConfig = applicationContext.getBean(ApplicationConfig.class);
    6. // 获取ProtocolConfig
    7. ProtocolConfig protocolConfig = applicationContext.getBean(ProtocolConfig.class);
    8. // 获取RegistryConfig
    9. RegistryConfig registryConfig = applicationContext.getBean(RegistryConfig.class);
    10. // 获取IPingService接口
    11. IPingService iPingService = applicationContext.getBean(IPingService.class);
    12. // 创建ServiceBean
    13. ServiceBean<IPingService> serviceBean = new ServiceBean<>();
    14. serviceBean.setApplicationContext(applicationContext);
    15. serviceBean.setInterface("com.ruubypay.api.IPingService");
    16. serviceBean.setApplication(applicationConfig);
    17. serviceBean.setProtocol(protocolConfig);
    18. serviceBean.setRegistry(registryConfig);
    19. serviceBean.setRef(iPingService);
    20. serviceBean.setTimeout(12000);
    21. serviceBean.setVersion("1.0.0");
    22. serviceBean.setOwner("rubby");
    23. // 发布dubbo接口
    24. serviceBean.export();
    25. System.out.println("dubbo接口发布完毕");
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. }
    29. }

    使用方式

    • DubboX: java -javaagent:ruubypay-ping-agent.jar=1 -jar 服务jar包

    • springboot的http接口:java -javaagent:ruubypay-ping-agent.jar -jar 服务jar包

    以上就是JavaAgent如何实现http接口发布的详细内容,更多关于JavaAgent如何实现http接口发布的资料请关注九品源码其它相关文章!