赞
踩
介绍RPC框架和Tomcat的基本概念,并解释为什么我们在处理分布式系统时需要将它们结合起来。概述本文将介绍的主要内容。
RPC框架就是通过不同应用之间的依赖关系进行跨应用方法调用
public class Provider {
public static void main(String[] args) {
//希望启动时可以接收网络请求,可以用Netty、tomcat
//rpc框架负责启动网络
HttpServer httpServer=new HttpServer();
httpServer.start("localhost",8080);
}
}
public class Provider {
public static void main(String[] args) {
//希望启动时可以接收网络请求,可以用Netty、tomcat
//rpc框架负责启动网络
HttpServer httpServer=new HttpServer();
httpServer.start("localhost",8080);
}
}
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>
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>
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(); } } }
<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结合使用的好处和应用场景。鼓励读者进一步探索和实践。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。