当前位置: 首页 > news >正文

承德城乡建设委员会网站宿州百度seo排名软件

承德城乡建设委员会网站,宿州百度seo排名软件,如何制作钓鱼网站,做自己的网站的作用目录 简介 服务提供者 服务注册:注册中心 HttpServerHandler处理远程调用请求 consumer服务消费端 简介 RPC(Remote Procedure Call)——远程过程调用,它是一种通过网络从远程计算机程序上请求服务, 而不需要了解…

目录

简介

服务提供者

服务注册:注册中心

HttpServerHandler处理远程调用请求

consumer服务消费端


简介

RPC(Remote Procedure Call)——远程过程调用,它是一种通过网络从远程计算机程序上请求服务, 而不需要了解底层网络技术的协议,在面向对象的编程语言中,远程过程调用即是远程方法调用

基本实现思路如下:

项目结构:

  • provider服务提供
  • consumer服务消费
  • registry注册
  • protocol协议

服务提供者

  • 定义服务接口

接口HelloService

public interface HelloService {String sayHello(String message);
}
  • 实现类HelloServiceImpl
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {return name+ "调用了myRPC的服务";}
}

服务注册:注册中心

此处注册中心我们将服务注册在map集合中,结构:Map<String,Map<URL,Class>> 外边map的key存储 服务接口的全类名,URL封装了调用服务的ip和port,里边value指定指定具体实现类 注册中心类提供注册服务并暴露服务和发现服务功能:

public class URL {
​private String hostname;private Integer port;@Overridepublic boolean equals(Object obj) {if(obj==null){return false;}if(!(obj instanceof  URL)){return false;}URL url = (URL) obj;if(hostname.equals(((URL) obj).getHostname())  && port.intValue() == url.port.intValue()){return true;}return false;}
​@Overridepublic int hashCode() {return hostname.hashCode();}
}
​
public class NativeRegistry {
​
​private static Map<String, Map<URL,Class>> registCenter = new HashMap<>();
​
​/*** 注册服务* @param url* @param interfaceName* @param implClass*/public static void regist(URL url,String interfaceName,Class implClass){
​Map<URL,Class> map = new HashMap<>();map.put(url,implClass);registCenter.put(interfaceName,map);}
​/*** 从注册中心获取服务* @param url* @param interfaceName* @return*/public static Class get(URL url,String interfaceName){return registCenter.get(interfaceName).get(url);}
​
​
}
  • 注册服务
public class ServiceProvider {
​public static void main(String[] args) {
​//创建URLURL url = new URL("localhost", 8080);
​//注册中心中注册服务NativeRegistry.regist(url, HelloService.class.getName(), HelloServiceImpl.class);
​//启动并暴露服务HttpServer httpServer = new HttpServer();httpServer.start(url.getHostname(),url.getPort());
​}
}
  • 暴露服务

服务之间调用的通信协议采用http协议,所以在服务provider中启动tomcat暴露服务

添加内嵌tomcat的依赖

  <!--内嵌tomcat--><dependencies><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId><version>9.0.12</version></dependency></dependencies>
  • 创建HttpServer
public class HttpServer {
​
​/*** tomcat服务启动* 参考tomcat配置* <Server port="8005" shutdown="SHUTDOWN">*  <Service name="Catalina">*      <Connector port="8080" protocol="HTTP/1.1"*                connectionTimeout="20000"*                redirectPort="8443"*         URIEncoding="UTF-8"/>*      <Engine name="Catalina" defaultHost="localhost">*          <Host name="localhost"  appBase="webapps"*              unpackWARs="true" autoDeploy="true">*              <Context path="" doBase="WORKDIR" reloadable="true"/>*              </Host>*      </Engine>*   </Service>* </Server>*/
​
​/*** 启动服务* @param hostname* @param port*/public void start(String hostname,int port){// 实例一个tomcatTomcat tomcat = new Tomcat();
​// 构建serverServer server = tomcat.getServer();
​// 获取serviceService service = server.findService("Tomcat");
​// 构建ConnectorConnector connector = new Connector();connector.setPort(port);connector.setURIEncoding("UTF-8");
​// 构建EngineEngine engine = new StandardEngine();engine.setDefaultHost(hostname);
​// 构建HostHost host = new StandardHost();host.setName(hostname);
​// 构建ContextString contextPath = "";Context context = new StandardContext();context.setPath(contextPath);context.addLifecycleListener(new Tomcat.FixContextListener());// 生命周期监听器
​// 然后按照server.xml,一层层把子节点添加到父节点host.addChild(context);engine.addChild(host);service.setContainer(engine);service.addConnector(connector);// service在getServer时就被添加到server节点了
​// tomcat是一个servlet,设置路径与映射tomcat.addServlet(contextPath,"dispatcher",new DispatcherServlet());context.addServletMappingDecoded("/*","dispatcher");
​try {tomcat.start();// 启动tomcattomcat.getServer().await();// 接受请求}catch (LifecycleException e){e.printStackTrace();}}
}
​
  • DispatcherServlet
public class DispatcherServlet extends HttpServlet {
​@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {new HttpServerHandler().handle(req,resp);}
}

HttpServerHandler处理远程调用请求

public class HttpServerHandler {
​
​/***  服务的处理* @param req* @param resp* @throws ServletException* @throws IOException*/public void handle(HttpServletRequest req, HttpServletResponse resp){try {//服务请求的处理逻辑
​//1 通过请求流获取请求服务调用的参数InputStream inputStream = req.getInputStream();ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
​Invocation invocation = (Invocation) objectInputStream.readObject();
​//2 从注册中心获取服务的列表Class implCass = NativeRegistry.get(new URL("localhost", 8080), invocation.getInterfaceName());
​//3 调用服务 反射Method method = implCass.getMethod(invocation.getMethodName(),invocation.getParamTypes());
​String result = (String) method.invoke(implCass.newInstance(), invocation.getParams());
​//4 结果返回IOUtils.write(result,resp.getOutputStream());} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}
​
​}
​
}
  • 封装调用参数Invocation
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Invocation implements Serializable {
​private String interfaceName;private String methodName;private Object[] params;private Class[] paramTypes;
​
}
  • 启动服务
public class ServiceProvider {
​public static void main(String[] args) {
​//创建URLURL url = new URL("localhost", 8080);
​//注册中心中注册服务NativeRegistry.regist(url, HelloService.class.getName(), HelloServiceImpl.class);
​//启动并暴露服务HttpServer httpServer = new HttpServer();httpServer.start(url.getHostname(),url.getPort());
​}
}

consumer服务消费端

  • 封装HttpClient对象,发起远程调用
public class HttpClient {
​/*** 远程方法调用* @param hostname :远程主机名* @param port :远程端口号* @param invocation :封装远程调用的信息*/public String post(String hostname, int port, Invocation invocation) {
​
​try {URL url = new URL("http", hostname, port, "/client/");HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("POST");connection.setDoOutput(true);// 必填项
​//发送调用的信息OutputStream os = connection.getOutputStream();ObjectOutputStream oos = new ObjectOutputStream(os);oos.writeObject(invocation);oos.flush();oos.close();
​// 将输入流转为字符串(此处可是java对象) 获取远程调用的结果InputStream is = connection.getInputStream();return IOUtils.toString(is);
​} catch (IOException e) {e.printStackTrace();}return null;
​}
​
}
  • 调用测试
public class Consumer {public static void main(String[] args) {
​//封装一个invocationInvocation invocation = new Invocation(HelloService.class.getName(), "sayHello2",new Object[]{"Test"}, new Class[]{String.class});
​//远程调用服务String result = new HttpClient().post("localhost", 8080, invocation);
​System.out.println("远程调用执行的结果result="+result);}
}


文章转载自:
http://www.morning.prplf.cn.gov.cn.prplf.cn
http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn
http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn
http://www.morning.dskmq.cn.gov.cn.dskmq.cn
http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn
http://www.morning.fpryg.cn.gov.cn.fpryg.cn
http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn
http://www.morning.phechi.com.gov.cn.phechi.com
http://www.morning.zlxrg.cn.gov.cn.zlxrg.cn
http://www.morning.crsnb.cn.gov.cn.crsnb.cn
http://www.morning.mrttc.cn.gov.cn.mrttc.cn
http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn
http://www.morning.ghjln.cn.gov.cn.ghjln.cn
http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn
http://www.morning.slmbg.cn.gov.cn.slmbg.cn
http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn
http://www.morning.rgpbk.cn.gov.cn.rgpbk.cn
http://www.morning.zlgr.cn.gov.cn.zlgr.cn
http://www.morning.bgpch.cn.gov.cn.bgpch.cn
http://www.morning.pjrql.cn.gov.cn.pjrql.cn
http://www.morning.hdqqr.cn.gov.cn.hdqqr.cn
http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn
http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn
http://www.morning.xhftj.cn.gov.cn.xhftj.cn
http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn
http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn
http://www.morning.tzcr.cn.gov.cn.tzcr.cn
http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn
http://www.morning.fycjx.cn.gov.cn.fycjx.cn
http://www.morning.mswkd.cn.gov.cn.mswkd.cn
http://www.morning.qddtd.cn.gov.cn.qddtd.cn
http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn
http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn
http://www.morning.nnqrb.cn.gov.cn.nnqrb.cn
http://www.morning.mzhh.cn.gov.cn.mzhh.cn
http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn
http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn
http://www.morning.ngcbd.cn.gov.cn.ngcbd.cn
http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn
http://www.morning.qsmch.cn.gov.cn.qsmch.cn
http://www.morning.znqmh.cn.gov.cn.znqmh.cn
http://www.morning.plkrl.cn.gov.cn.plkrl.cn
http://www.morning.xptkl.cn.gov.cn.xptkl.cn
http://www.morning.wwthz.cn.gov.cn.wwthz.cn
http://www.morning.grzpc.cn.gov.cn.grzpc.cn
http://www.morning.rkbly.cn.gov.cn.rkbly.cn
http://www.morning.qnywy.cn.gov.cn.qnywy.cn
http://www.morning.hphrz.cn.gov.cn.hphrz.cn
http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn
http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn
http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn
http://www.morning.rdkgw.cn.gov.cn.rdkgw.cn
http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn
http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn
http://www.morning.mxhgy.cn.gov.cn.mxhgy.cn
http://www.morning.gmztd.cn.gov.cn.gmztd.cn
http://www.morning.prprj.cn.gov.cn.prprj.cn
http://www.morning.wgtr.cn.gov.cn.wgtr.cn
http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn
http://www.morning.gynls.cn.gov.cn.gynls.cn
http://www.morning.tkcz.cn.gov.cn.tkcz.cn
http://www.morning.wjtxt.cn.gov.cn.wjtxt.cn
http://www.morning.xmttd.cn.gov.cn.xmttd.cn
http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn
http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn
http://www.morning.gnfkl.cn.gov.cn.gnfkl.cn
http://www.morning.rzsxb.cn.gov.cn.rzsxb.cn
http://www.morning.bwxph.cn.gov.cn.bwxph.cn
http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn
http://www.morning.pcxgj.cn.gov.cn.pcxgj.cn
http://www.morning.bdfph.cn.gov.cn.bdfph.cn
http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn
http://www.morning.nclps.cn.gov.cn.nclps.cn
http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn
http://www.morning.clqpj.cn.gov.cn.clqpj.cn
http://www.morning.kspfq.cn.gov.cn.kspfq.cn
http://www.morning.spfh.cn.gov.cn.spfh.cn
http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn
http://www.morning.nclbk.cn.gov.cn.nclbk.cn
http://www.morning.weiwt.com.gov.cn.weiwt.com
http://www.tj-hxxt.cn/news/14382.html

相关文章:

  • 佛山北京网站建设semester是什么意思
  • 沈阳专业做网站方案百度seo怎么操作
  • 关于做网站的问卷调查衡阳百度推广
  • 东莞人才信息网郑州seo团队
  • 沈阳 网站制作报价商家怎么入驻百度
  • 世界服装鞋帽网免费做网站营销网站设计
  • 甘肃省建设厅招标办网站贵阳百度seo点击软件
  • wordpress有哪些弹窗插件网站推广的优化
  • 遵义住房城乡建设厅网站汕头seo网站建设
  • 网站页面设计的特色推广策略
  • 禅城网站设计培训机构需要哪些证件
  • 类似知乎可以做推广的网站百度浏览器手机版
  • 企业网站最下面的那栏叫啥郑州seo排名工具
  • 做h5的网站哪个好拼多多关键词排名查询软件
  • wordpress做学校网站百度热度榜搜索趋势
  • 做网站的公司网络视频营销
  • 怎么搜才能搜到网站推广引流渠道
  • 商务网站的特点北京网络推广公司wyhseo
  • 想招聘员工去哪个网站微信公众号怎么创建
  • 网站数据模版生活中的网络营销有哪些
  • 淮南矿业集团廉政建设网站优化推广关键词
  • 绍兴做网站公司哪家好百度seo费用
  • 淄博网站设计策划方案维护重庆seo薪酬水平
  • 做t恤网站 一件也可以做seo基础教程
  • 聊城专业做网站的公司搜索seo怎么优化
  • wordpress 中文商城主题大连seo外包平台
  • 武汉哪家做网站网页推广链接怎么做
  • 水产养殖网站模板源码杭州网站
  • 论述网站推广的方法与技巧治疗腰椎间盘突出的特效药
  • 一个电信ip做网站卡不卡自助建站系统源码