sofa-rpc源码阅读(1)-环境搭建

1. 环境搭建

1.1 拷贝源码

git clone https://github.com/sofastack/sofa-rpc.git

1.2 编译源码

在源码目录执行

mvn clean install -DskipTests

注意,安装java的版本为1.8,用java11编译会报错

1.3 运行样例

1.3.1 启动服务器

找到类 com.alipay.sofa.rpc.bolt.start.BoltServerMain 运行main方法

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-server");

    ServerConfig serverConfig = new ServerConfig()
        .setPort(22000)
        .setDaemon(false);

    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
        .setInterfaceId(HelloService.class.getName())
        .setApplication(application)
        .setRef(new HelloServiceImpl())
        .setServer(serverConfig)
        .setRegister(false);

    ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>()
        .setInterfaceId(EchoService.class.getName())
        .setApplication(application)
        .setRef(new EchoServiceImpl())
        .setServer(serverConfig)
        .setRegister(false);

    providerConfig.export();
    providerConfig2.export();

    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}

结果如下

2020-03-06 21:13:29,032 main INFO [com.alipay.sofa.rpc.context.RpcRuntimeContext:info:102] - Welcome! Loading SOFA RPC Framework : 5.5.9_20191011104639, PID is:26068
2020-03-06 21:13:29,079 main INFO [com.alipay.sofa.rpc.module.ModuleFactory:info:102] - Install Module: fault-tolerance
2020-03-06 21:13:29,157 main INFO [com.alipay.sofa.rpc.module.ModuleFactory:info:102] - Install Module: sofaTracer-resteasy
2020-03-06 21:13:29,781 main INFO [com.alipay.sofa.rpc.module.ModuleFactory:info:97] - The module lookout does not need to be loaded.
2020-03-06 21:13:29,781 main INFO [com.alipay.sofa.rpc.module.ModuleFactory:info:102] - Install Module: sofaTracer
2020-03-06 21:13:29,813 main INFO [com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap:infoWithApp:122] - [test-server]Export provider config : com.alipay.sofa.rpc.test.HelloService::bolt with bean id rpc-cfg-0
2020-03-06 21:13:31,812 main INFO [com.alipay.sofa.rpc.server.bolt.BoltServer:info:102] - Bolt server has been bind to 0.0.0.0:22000
2020-03-06 21:13:31,812 main INFO [com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap:infoWithApp:122] - [test-server]Export provider config : com.alipay.sofa.rpc.test.EchoService::bolt with bean id rpc-cfg-1
2020-03-06 21:13:31,812 main WARN [com.alipay.sofa.rpc.bolt.start.BoltServerMain:warn:142] - started at pid 26068
2020-03-06 21:15:29,281 SOFA-SEV-BOLT-BIZ-22000-3-T1 INFO [com.alipay.sofa.rpc.codec.sofahessian.SofaHessianSerializer:info:97] - Version of sofa-hessian is v3.x
2020-03-06 21:15:29,562 SOFA-SEV-BOLT-BIZ-22000-3-T1 INFO [com.alipay.sofa.rpc.tracer.Tracers:info:102] - Load tracer impl success: sofaTracer, com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer@715472d0
2020-03-06 21:15:29,578 SOFA-SEV-BOLT-BIZ-22000-3-T1 INFO [com.alipay.sofa.rpc.test.HelloServiceImpl:info:97] - name:xxx, age:22
2020-03-06 21:15:31,655 SOFA-SEV-BOLT-BIZ-22000-3-T2 INFO [com.alipay.sofa.rpc.test.HelloServiceImpl:info:97] - name:xxx, age:22

1.3.2 启动客户端

找到类 com.alipay.sofa.rpc.bolt.start.BoltClientMain 并运行main方法

public static void main(String[] args) throws InterruptedException {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-client");

    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
        .setApplication(application)
        .setInterfaceId(HelloService.class.getName())
        .setDirectUrl("bolt://127.0.0.1:22000")
        .setRegister(false)
        .setTimeout(3000);
    HelloService helloService = consumerConfig.refer();

    ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>()
        .setInterfaceId(EchoService.class.getName())
        .setApplication(application)
        .setDirectUrl("bolt://127.0.0.1:22000")
        .setRegister(false)
        .setTimeout(3000);
    EchoService echoService = consumerConfig2.refer();

    consumerConfig2.unRefer();

    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);

    while (true) {
        try {
            String s = helloService.sayHello("xxx", 22);
            LOGGER.warn("{}", s);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
        }
    }
}

结果如下:

2020-03-06 21:15:25,749 main INFO [com.alipay.sofa.rpc.context.RpcRuntimeContext:info:102] - Welcome! Loading SOFA RPC Framework : 5.5.9_20191011104639, PID is:24356
2020-03-06 21:15:25,780 main INFO [com.alipay.sofa.rpc.module.ModuleFactory:info:102] - Install Module: fault-tolerance
2020-03-06 21:15:25,858 main INFO [com.alipay.sofa.rpc.module.ModuleFactory:info:102] - Install Module: sofaTracer-resteasy
2020-03-06 21:15:26,483 main INFO [com.alipay.sofa.rpc.module.ModuleFactory:info:97] - The module lookout does not need to be loaded.
2020-03-06 21:15:26,483 main INFO [com.alipay.sofa.rpc.module.ModuleFactory:info:102] - Install Module: sofaTracer
2020-03-06 21:15:26,499 main INFO [com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap:infoWithApp:122] - [test-client]Refer consumer config : bolt://com.alipay.sofa.rpc.test.HelloService: with bean id rpc-cfg-0
2020-03-06 21:15:26,561 main INFO [com.alipay.sofa.rpc.client.AllConnectConnectionHolder:infoWithApp:122] - [test-client]Add provider of com.alipay.sofa.rpc.test.HelloService, size is : 1
2020-03-06 21:15:28,312 main INFO [com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap:infoWithApp:122] - [test-client]Refer consumer config : bolt://com.alipay.sofa.rpc.test.EchoService: with bean id rpc-cfg-1
2020-03-06 21:15:28,312 main INFO [com.alipay.sofa.rpc.client.AllConnectConnectionHolder:infoWithApp:122] - [test-client]Add provider of com.alipay.sofa.rpc.test.EchoService, size is : 1
2020-03-06 21:15:28,625 SOFA-CLI-CONN-com.alipay.sofa.rpc.test.EchoService-6-T1 INFO [com.alipay.sofa.rpc.client.AllConnectConnectionHolder:infoWithApp:122] - [test-client]Connect to com.alipay.sofa.rpc.test.EchoService provider:bolt://127.0.0.1:22000 success ! The connection is 127.0.0.1:22000 <-> 127.0.0.1:6975
2020-03-06 21:15:28,625 SOFA-CLI-CONN-com.alipay.sofa.rpc.test.HelloService-3-T1 INFO [com.alipay.sofa.rpc.client.AllConnectConnectionHolder:infoWithApp:122] - [test-client]Connect to com.alipay.sofa.rpc.test.HelloService provider:bolt://127.0.0.1:22000 success ! The connection is 127.0.0.1:22000 <-> 127.0.0.1:6975
2020-03-06 21:15:28,625 main INFO [com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap:infoWithApp:122] - [test-client]UnRefer consumer config : bolt://com.alipay.sofa.rpc.test.EchoService: with bean id rpc-cfg-1
2020-03-06 21:15:28,625 main WARN [com.alipay.sofa.rpc.bolt.start.BoltClientMain:warn:142] - started at pid 24356
2020-03-06 21:15:28,625 main INFO [com.alipay.sofa.rpc.tracer.Tracers:info:102] - Load tracer impl success: sofaTracer, com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer@3c0ecd4b
2020-03-06 21:15:28,969 main INFO [com.alipay.sofa.rpc.codec.sofahessian.SofaHessianSerializer:info:97] - Version of sofa-hessian is v3.x
2020-03-06 21:15:29,640 main WARN [com.alipay.sofa.rpc.bolt.start.BoltClientMain:warn:142] - hello xxx from server! age: 22
2020-03-06 21:15:31,655 main WARN [com.alipay.sofa.rpc.bolt.start.BoltClientMain:warn:142] - hello xxx from server! age: 22

猜你喜欢

转载自www.cnblogs.com/huiyao/p/12460129.html