Netty 实战篇:Netty RPC 框架整合 Spring Boot,迈向工程化

本文将基于前面构建的 RPC 能力,尝试将其与 Spring Boot 整合,借助注解、自动扫描、依赖注入等机制,打造"开箱即用"的 Netty RPC 框架,提升开发效率与工程规范。


一、为什么要整合 Spring Boot?

手动 new 实例、写注册逻辑、写接口代理代码太繁琐,不利于实际项目使用。我们希望:

  • 通过注解快速暴露服务(类似 @RestController)

  • 通过注解快速引用远程服务(类似 @FeignClient)

  • 自动初始化注册中心、Netty 客户端、服务端

✅ 本文目标:打造注解驱动、自动装配的 RPC 框架


二、目标效果

java 复制代码
// 暴露远程服务
@RpcService
public class HelloServiceImpl implements HelloService {
    public String hello(String name) {
        return "Hello " + name;
    }
}

// 注入远程调用代理
@RpcReference
private HelloService helloService;

三、自定义注解

java 复制代码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RpcService {
    String name() default "";
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RpcReference {
    String name() default "";
}

四、定义配置类与自动扫描器

我们通过 Spring Boot 的 @Import 加载注册逻辑:

java 复制代码
@Configuration
@ComponentScan("com.example.rpc")
@Import(RpcBeanPostProcessor.class)
public class RpcAutoConfiguration {
}

RpcBeanPostProcessor 扫描所有 @RpcService 和 @RpcReference 注解:

java 复制代码
public class RpcBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
    private ApplicationContext context;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if (bean.getClass().isAnnotationPresent(RpcService.class)) {
            // 注册服务到注册中心 + 启动 Netty 服务端
            RpcServer.register(bean);
        }

        for (Field field : bean.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(RpcReference.class)) {
                Object proxy = RpcClient.createProxy(field.getType());
                field.setAccessible(true);
                try {
                    field.set(bean, proxy);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return bean;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.context = applicationContext;
    }
}

五、Spring Boot 项目结构建议

java 复制代码
rpc-core/
    ├── RpcClient.java
    ├── RpcServer.java
    ├── RpcProxy.java
    ├── annotation/
    ├── processor/
rpc-spring-boot-starter/
    ├── RpcAutoConfiguration.java
    ├── META-INF/spring.factories
demo-provider/
    ├── HelloServiceImpl.java
demo-consumer/
    ├── HelloController.java

只需在 pom.xml 引入:

XML 复制代码
<dependency>
    <groupId>com.example</groupId>
    <artifactId>rpc-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

六、使用示例

服务端:

java 复制代码
@SpringBootApplication
public class RpcProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(RpcProviderApp.class, args);
    }
}
java 复制代码
@RpcService
public class HelloServiceImpl implements HelloService {
    public String hello(String name) {
        return "Hello from Provider: " + name;
    }
}

客户端:

java 复制代码
@SpringBootApplication
public class RpcConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(RpcConsumerApp.class, args);
    }

    @RpcReference
    private HelloService helloService;

    @PostConstruct
    public void init() {
        System.out.println(helloService.hello("Netty"));
    }
}

七、总结

通过本文,我们将 Netty RPC 框架成功整合进了 Spring Boot:

✅ 实现了服务自动注册

✅ 注解式远程调用

✅ 自动代理 + 自动注入

✅ 框架模块化、可复用、可发布

相关推荐
Derek_Smart11 天前
基于Netty与Spring Integration的高并发工业物联网网关架构设计与实现
spring boot·物联网·netty
迢迢星万里灬12 天前
Java求职者面试指南:微服务技术与源码原理深度解析
java·spring cloud·微服务·dubbo·netty·分布式系统·面试指南
Y_3_714 天前
Netty实战:从核心组件到多协议实现(超详细注释,udp,tcp,websocket,http完整demo)
linux·运维·后端·ubuntu·netty
安徽杰杰21 天前
能源即服务:智慧移动充电桩的供给模式创新
netty
安徽杰杰22 天前
新基建浪潮下:中国新能源汽车充电桩智慧化建设与管理实践
netty
迢迢星万里灬23 天前
Java求职者面试:微服务技术与源码原理深度解析
java·spring cloud·微服务·dubbo·netty·分布式系统
触角云科技23 天前
掌上充电站:基于APP/小程序的新能源汽车智慧充电管理
netty
安徽杰杰23 天前
智慧充电:新能源汽车智慧充电桩的发展前景受哪些因素影响?
netty
漫步者TZ1 个月前
【Netty系列】解决TCP粘包和拆包:LengthFieldBasedFrameDecoder
java·网络协议·tcp/ip·netty
安徽杰杰1 个月前
智慧赋能:移动充电桩的能源供给革命与便捷服务升级
netty