likes
comments
collection
share

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

作者站长头像
站长
· 阅读数 28

大家好,我是小趴菜,接下来我会从0到1手写一个RPC框架,该专题包括以下专题,有兴趣的小伙伴就跟着我一起学习吧

本章源码地址:gitee.com/baojh123/se…

自定义注解 -> opt-01
服务提供者收发消息基础实现 -> opt-01
自定义网络传输协议的实现 -> opt-02
自定义编解码实现 -> opt-03
服务提供者调用真实方法实现 -> opt-04
完善服务消费者发送消息基础功能 -> opt-05
注册中心基础功能实现 -> opt-06
服务提供者整合注册中心 -> opt-07
服务消费者整合注册中心 -> opt-08
完善服务消费者接收响应结果 -> opt-09
服务消费者,服务提供者整合SpringBoot -> opt-10
动态代理屏蔽RPC服务调用底层细节 -> opt-10
SPI机制基础功能实现 -> opt-11
SPI机制扩展随机负载均衡策略 -> opt-12
SPI机制扩展轮询负载均衡策略 -> opt-13
SPI机制扩展JDK序列化 -> opt-14
SPI机制扩展JSON序列化 -> opt-15
SPI机制扩展protustuff序列化 -> opt-16

目标

虽然我们已经实现了一个RPC框架具备的基础功能,但是现在它还不能与SpringBoot整合在一起。在之前很多配置我们都是手动写死在类中,但是在整合SpringBoot之后,这些配置就应该从配置文件中读取,接下来我们就将我们的服务提供者与SpringBoot整合

实现

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

首先我们将服务提供者进行改造,新增二个子模块,将原先的 xpc-rpc-provider 的代码全都移到 xpc-rpc-provider-common 模块中

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

xpc-rpc-spring模块主要负责的是读取配置,扫描注解并解析

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

  • com.xpc.rpc.provider.spring.config.RpcProviderAutoConfiguration
package com.xpc.rpc.provider.spring.config;

import com.xpc.rpc.provider.server.base.BaseServer;
import com.xpc.rpc.provider.spring.scanner.DubboServiceScanner;
import com.xpc.rpc.register.common.config.RegisterConfig;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties(RegisterConfig.class)
public class RpcProviderAutoConfiguration implements InitializingBean {

    @Resource
    private RegisterConfig registerConfig;

    @Override
    public void afterPropertiesSet() throws Exception {
        DubboServiceScanner dubboServiceScanner = new DubboServiceScanner();
        //扫描注解并解析
        dubboServiceScanner.doScanDubboServiceByPackages(registerConfig);
        
        BaseServer baseServer = new BaseServer(registerConfig);
        //启动Netty服务
        baseServer.startNettyServer();
    }
}

至此,服务提供者的代码改造就已经完成了,接下来就是改造服务消费者模块了

将服务消费者也改造成二个子模块,原先的代码移动到 xpc-rpc-consumer-common 模块中

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

  • com.xpc.consumer.config.RpcConsumerAutoConfig
package com.xpc.consumer.config;

import com.xpc.consumer.scanner.reference.ReferenceScanner;
import com.xpc.rpc.consumer.config.RpcConsumerConfig;
import com.xpc.rpc.consumer.cache.RpcConsumerConfigCache;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;


@Configuration
@EnableConfigurationProperties(RpcConsumerConfig.class)
public class RpcConsumerAutoConfig  implements InitializingBean, ApplicationContextAware {


    private static ApplicationContext applicationContext;

    @Resource
    private RpcConsumerConfig rpcConsumerConfig;

    @Override
    public void afterPropertiesSet() throws Exception {
         ReferenceScanner referenceScanner = new ReferenceScanner();
        
        //@DubboReference注解的扫描与解析
         referenceScanner.doScanDubboReferenceByPackage(rpcConsumerConfig.getPackageName());
         
         //将配置缓存起来后续使用
         new RpcConsumerConfigCache(rpcConsumerConfig);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RpcConsumerAutoConfig.applicationContext = applicationContext;
    }
 
 
    //从Spring的Bean容器中根据class获取Bean
    public static Object getObject(Class clazz) {
        return applicationContext.getBean(clazz);
    }

    public static ApplicationContext getSpringContext() {
        return applicationContext;
    }
}

测试

先启动服务提供者服务 xpc-rpc-web-provider

创建一个服务接口实现类:com.xpc.com.xpc.impl.UserServiceImpl

package com.xpc.com.xpc.impl;

import com.xpc.interfaces.UserService;
import com.xpc.rpc.annotation.DubboReference;
import com.xpc.rpc.annotation.DubboService;

@DubboService(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService{

    @Override
    public String hello(String name) {
        return "hello " + name;
    }
}

启动服务提供者服务:

package com.xpc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Provider {

    public static void main(String[] args) {
        SpringApplication.run(Provider.class,args);
    }
}

可以看到在服务启动中以后,Netty服务也跟着一起启动了

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

接下来启动服务消费者

package com.xpc.consumer;

import com.xpc.interfaces.UserService;

import com.xpc.rpc.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class Consumer {

    private static final Logger LOGGER = LoggerFactory.getLogger(Consumer.class);

    @DubboReference
    private UserService userService;


    public static void main(String[] args) {
        SpringApplication.run(Consumer.class,args);
    }

    @GetMapping("/consumer")
    public String test() throws Exception{
        String coco = userService.hello("coco");
        return coco;
    }

}

启动之后,在浏览器输入:http://localhost:8081/consumer

手撸RPC框架 -服务提供者,服务消费者整合SpringBoot实现

在这里我们不仅实现了服务提供者与服务消费者与SpringBoot的整合,而且还通过代理来屏蔽了RPC底层服务调用的具体细节,关于代理的功能会在下一章实现

转载自:https://juejin.cn/post/7254792532637302844
评论
请登录