如何把网站做成app,怎么让别人访问自己做的网站,买了虚拟主机怎么做网站,帝国cms建站实例教程4 Dubbo快速入门Dubbo作为一个RPC框架#xff0c;其最核心的功能就是要实现跨网络的远程调用。本小节就是要创建两个应用#xff0c;一个作为服务的提供方#xff0c;一个作为服务的消费方。通过Dubbo来实现服务消费方远程调用服务提供方的方法。4.1 服务提供方开发开发步骤…4 Dubbo快速入门Dubbo作为一个RPC框架其最核心的功能就是要实现跨网络的远程调用。本小节就是要创建两个应用一个作为服务的提供方一个作为服务的消费方。通过Dubbo来实现服务消费方远程调用服务提供方的方法。4.1 服务提供方开发开发步骤(1)创建maven工程(打包方式为war)dubbodemo_provider在pom.xml文件中导入如下坐标warUTF-81.81.85.0.5.RELEASEorg.springframeworkspring-context${spring.version}org.springframeworkspring-beans${spring.version}org.springframeworkspring-webmvc${spring.version}org.springframeworkspring-jdbc${spring.version}org.springframeworkspring-aspects${spring.version}org.springframeworkspring-jms${spring.version}org.springframeworkspring-context-support${spring.version}com.alibabadubbo2.6.0org.apache.zookeeperzookeeper3.4.7com.github.sgroschupfzkclient0.1javassistjavassist3.12.1.GAcom.alibabafastjson1.2.47org.apache.maven.pluginsmaven-compiler-plugin2.3.21.81.8org.apache.tomcat.maventomcat7-maven-plugin8081/(2)配置web.xml文件创建 webapp/WEB-INF/web.xmlxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsdversion3.1contextConfigLocationclasspath:applicationContext*.xmlorg.springframework.web.context.ContextLoaderListener(3)创建服务接口package com.maweiqi.service;/*** HelloService** Author: 马 伟 奇* CreateTime: 2019-07-18* Description:*/public interface HelloService {public String sayHello(String name);}(4)创建服务实现类package com.maweiqi.service.impl;import com.alibaba.dubbo.config.annotation.Service;import com.maweiqi.service.HelloService;/*** HelloServiceImpl** Author: 马 伟 奇* CreateTime: 2019-07-18* Description:*/Servicepublic class HelloServiceImpl implements HelloService {Overridepublic String sayHello(String name) {return hello name;}}注意服务实现类上使用的Service注解是Dubbo提供的用于对外发布服务(5)在src/main/resources下创建applicationContext-service.xmlxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:dubbohttp://code.alibabatech.com/schema/dubboxmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd(6)启动服务启动服务4.2 服务消费方开发开发步骤(1)创建maven工程(打包方式为war)dubbodemo_consumerpom.xml配置和上面服务提供者相同只需要将Tomcat插件的端口号改为8082即可warUTF-81.81.85.0.5.RELEASEorg.springframeworkspring-context${spring.version}org.springframeworkspring-beans${spring.version}org.springframeworkspring-webmvc${spring.version}org.springframeworkspring-jdbc${spring.version}org.springframeworkspring-aspects${spring.version}org.springframeworkspring-jms${spring.version}org.springframeworkspring-context-support${spring.version}com.alibabadubbo2.6.0org.apache.zookeeperzookeeper3.4.7com.github.sgroschupfzkclient0.1javassistjavassist3.12.1.GAcom.alibabafastjson1.2.47org.apache.maven.pluginsmaven-compiler-plugin2.3.21.81.8org.apache.tomcat.maventomcat7-maven-plugin8082/(2)配置web.xml文件xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsdversion3.1springmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:applicationContext-web.xml1springmvc/(3)将服务提供者工程中的HelloService接口复制到当前工程package com.maweiqi.service;/*** HelloService** Author: 马伟奇* CreateTime: 2019-07-18* Description:*/public interface HelloService {public String sayHello(String name);}(4)编写Controllerpackage com.maweiqi.controller;import com.alibaba.dubbo.config.annotation.Reference;import com.maweiqi.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/*** HelloController** Author: 马伟奇* CreateTime: 2019-07-19* Description:*/ControllerRequestMapping(/demo)public class HelloController {Referenceprivate HelloService helloService;RequestMapping(/hello)ResponseBodypublic String getName(String name){//远程调用String result helloService.sayHello(name);System.out.println(result);return result;}}注意Controller中注入HelloService使用的是Dubbo提供的Reference注解(5)在src/main/resources下创建applicationContext-web.xmlxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:dubbohttp://code.alibabatech.com/schema/dubboxmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd(6)运行测试运行在浏览器输入http://localhost:8082/demo/hello?nameJack查看浏览器输出结果4.3 代码重构① 创建项目dubbodemo_interface创建项目② 把 项目dubbodemo_consumer 和 项目dubbodemo_provider当中的 接口 HelloService 拷贝到dubbodemo_interface工程里面image.png③ 删除工程dubbodemo_consumer 和 工程dubbodemo_provider当中的 接口 HelloServiceimage.pngdubbodemo_consumer 工程和dubbodemo_provider添加pom文件的依赖com.maweiqidubbodemo_interface1.0-SNAPSHOT获取数据5. Dubbo管理控制台我们在开发时需要知道Zookeeper注册中心都注册了哪些服务有哪些消费者来消费这些服务。我们可以通过部署一个管理中心来实现。其实管理中心就是一个web应用部署到tomcat即可。5.1 安装安装步骤(1)将资料中的dubbo-admin-2.6.0.war文件复制到tomcat的webapps目录下image.png(2)启动tomcat此war文件会自动解压image.png(3)修改WEB-INF下的dubbo.properties文件注意dubbo.registry.address对应的值需要对应当前使用的Zookeeper的ip地址和端口号dubbo.registry.addresszookeeper://192.168.134.129:2181dubbo.admin.root.passwordrootdubbo.admin.guest.passwordguestimage.png(4)重启tomcat5.2 使用操作步骤image.png(2)启动服务提供者工程和服务消费者工程可以在查看到对应的信息image.pngimage.png