引言
介绍RPC框架和Tomcat的基本概念,并解释为什么我们在处理分布式系统时需要将它们结合起来。概述本文将介绍的主要内容。
RPC框架就是通过不同应用之间的依赖关系进行跨应用方法调用
本文文件目录
准备工作
- 在Provider-Common应用中创建HelloService接口类,作为调用方法的中转站
public class Provider {
public static void main(String[] args) {
//希望启动时可以接收网络请求,可以用Netty、tomcat
//rpc框架负责启动网络
HttpServer httpServer=new HttpServer();
httpServer.start("localhost",8080);
}
}
- 在Provider应用中创建Provider类,用于启动tomcat、接收网络请求
public class Provider {
public static void main(String[] args) {
//希望启动时可以接收网络请求,可以用Netty、tomcat
//rpc框架负责启动网络
HttpServer httpServer=new HttpServer();
httpServer.start("localhost",8080);
}
}
- 在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>
- 创建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>
- 在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();
}
}
}
- 在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结合使用的好处和应用场景。鼓励读者进一步探索和实践。