详解-手写RPC框架(2)-使用java的方式运行tomcat

引言

介绍RPC框架和Tomcat的基本概念,并解释为什么我们在处理分布式系统时需要将它们结合起来。概述本文将介绍的主要内容。

RPC框架就是通过不同应用之间的依赖关系进行跨应用方法调用

本文文件目录

在这里插入图片描述

准备工作

  1. 在Provider-Common应用中创建HelloService接口类,作为调用方法的中转站
public class Provider {
    public static void main(String[] args) {
        //希望启动时可以接收网络请求,可以用Netty、tomcat
        //rpc框架负责启动网络
        HttpServer httpServer=new HttpServer();
        httpServer.start("localhost",8080);
    }
}
  1. 在Provider应用中创建Provider类,用于启动tomcat、接收网络请求
public class Provider {
    public static void main(String[] args) {
        //希望启动时可以接收网络请求,可以用Netty、tomcat
        //rpc框架负责启动网络
        HttpServer httpServer=new HttpServer();
        httpServer.start("localhost",8080);

    }
}
  1. 在Provider应用中创建HelloServiceImpl类,用于实现HelloService类的接口,并在Provider的pom.xml中添加依赖
public class HelloServiceImpl implements HelloService{

    @Override
    public String sayHello(String name) {
        return "hello:"+name;
    }
}
   <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Provider-Common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>rpc</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
  1. 创建Consumer类,用于调用方法,并在pom.xml中添加依赖
public class Consumer {
    public static void main(String[] args) {
        //希望可以不用HelloService实现类就可以调用HelloService的方法
        //希望可以得到helloService的对象
        HelloService helloService=?;
        //然后调用对象的sayHello方法
        String result=helloService.sayHello("lpl");
        //当然,现在是不能运行的,我们希望可以在Consumer中调到Provider中的sayHello方法
        System.out.println(result);
    }
}
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Provider-Common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>rpc</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
  1. 在rpc应用下创建HttpServer类,用于读取用户的网络服务配置(如netty、tomcat等,本文使用tomcat)
package com.lpl.protocol;
import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
public class HttpServer {
    public void start(String hostname,Integer port){
        //读取用户的配置 server.name=tomcat 那么启动的就是tomcat
        Tomcat tomcat =new Tomcat();

        Server server =tomcat.getServer();
        Service service = server.findService("Tomcat");

        Connector connector=new Connector();
        connector.setPort(port);

        Engine engine=new StandardEngine();
        engine.setDefaultHost(hostname);

        Host host=new StandardHost();
        host.setName(hostname);

        String contextPath="";
        Context context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new Tomcat.FixContextListener());

        host.addChild(context);
        engine.addChild(host);

        service.setContainer(engine);
        service.addConnector(connector);

        try{
            tomcat.start();
            tomcat.getServer().await();
        }catch (LifecycleException e){
            e.printStackTrace();
        }
    }
}
  1. 在rpc应用下的pom.xml中的properties下添加tomcat依赖
    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.57</version>
        </dependency>
    </dependencies>

7.在Provider中进行tomcat启动测试

public class Provider {
    public static void main(String[] args) {
        //希望启动时可以接收网络请求,可以用Netty、tomcat
        //rpc框架负责启动网络
        HttpServer httpServer=new HttpServer();
        httpServer.start("localhost",8080);
    }
}

执行结果:(tomcat启动成功)
在这里插入图片描述

结论

总结本文介绍的内容,并强调将RPC框架与Tomcat结合使用的好处和应用场景。鼓励读者进一步探索和实践。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值