本篇实验dubbo的去中心化服务治理能力
dubbo介绍
服务治理开源项目,具备服务自动伸缩能力。当有部分dubbo服务实例不可用时,其通过注册中心(本实验是zookeeper注册中心),将不可用的服务在客户端调用层删除。
实验主机
localhostdubbo 服务实例
localhost casdemo web示例,允当dubbo服务消费者
localhost zookeeper实例
zookeeper启动
./zkServer.sh start
可以配置多个zookeeper实例
服务端
发布demoService.sayHello()服务
运行JAVA main函数
</pre></div><div>DubboProviderMain</div><div><pre name="code" class="java">package casdemo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboProviderMain { /** * @Title main * @Description TODO * @Author weizhi2018 * @param args * @throws */ public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"remote-provider.xml"}); context.start(); System.out.println("Press any key to exit."); System.in.read(); } }
运行JAVA main函数
remote-provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="casdemo" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" />--> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:registry protocol="zookeeper" address="192.168.161.73:21818"/> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="casdemo.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="casdemo.DemoServiceImpl" /> </beans>
客户端
客户端为web工程
webconsumer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--注册中心地址配置--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:dubbo.properties</value> </list> </property> </bean> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <!-- 应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。 --> <dubbo:application name="consumer-of-casdemo"/> <!-- 使用zookeeper注册中心暴露发现服务地址 --> <dubbo:registry protocol="zookeeper" address="192.168.161.73:21818" /> <!-- 使用multicast广播注册中心暴露发现服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="casdemo.DemoService" /> </beans>
Login
调用dubbo服务
WEB-INF\web.xml
demoService.sayHello()
</pre></div><div><pre name="code" class="java">package casdemo; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; /** * Servlet implementation class Login */ public class Login extends HttpServlet { private static final long serialVersionUID = 1L; //@Autowired DemoService demoService; /** * @see HttpServlet#HttpServlet() */ public Login() { super(); // TODO Auto-generated constructor stub } private ApplicationContext applicationContext; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); session.invalidate(); response.sendRedirect("login.jsp"); } public void init(ServletConfig config) throws ServletException { // TODO Auto-generatedmethod stub super.init(config); applicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext()); demoService=(DemoService)applicationContext.getBean("demoService"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); String username=request.getParameter("username"); System.out.println(demoService.sayHello("testman")); session.setAttribute("user",username); response.sendRedirect("usr/index.jsp"); } }
WEB-INF\web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>casdemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/spring-servlet.xml,classpath*:webconsumer.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>CheckLoginFilter</filter-name> <filter-class>casdemo.CheckLoginFilter</filter-class> </filter> <listener> <listener-class>casdemo.DebugSessionListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>login</servlet-name> <servlet-class>casdemo.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <filter-mapping> <filter-name>CheckLoginFilter</filter-name> <url-pattern>/usr/*</url-pattern> </filter-mapping> </web-app>
WEB-INF\spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="true"> <context:component-scan base-package="casdemo"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="20971520" /> </bean> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>image/jpeg</value> <value>image/png</value> <value>image/svg+xml</value> <value>application/pdf</value> </list> </property> </bean> </beans>
测试
访问 http://localhost:8080/casdemo/login.jsp 登录成功后,会在控制台日志中打印Hellotestman 。
搜索
复制
相关推荐
Dubbo是阿里巴巴开源的分布式服务化治理框架(微服务框架),久经阿里巴巴电商平台的大规模复杂业务的高并发考验,到目前为止Dubbo仍然是开源界中体系最完善的服务化治理框架,因此Dubbo被国内大量的的互联网公司和...
品优购电商系统--springboot+dubbo版.zip品优购电商系统--springboot+dubbo版.zip品优购电商系统--springboot+dubbo版.zip品优购电商系统--springboot+dubbo版.zip品优购电商系统--springboot+dubbo版.zip品优购电商...
微服务架构面试专题系列:Dubbo+Spring Boot+Spring Cloud.zip
微服务架构面试题系列:Dubbo+Spring Boot+Spring Cloud+Nginx+ActiveMQ+RabbitMQ+Kafka
dubbo服务监控 目录包含: dubbo-admin dubbo-monitor-simple dubbo-registry-simple pom.xml README.md
面试专题-面试人员必看-微服务架构面试专题系列:Dubbo+Spring Boot+Spring Cloud
文件是网盘链接地址,2018.7.4上传,链接一直可用 本套视频为dubbo分布式系统架构实战教学视频
Java基于SOA架构的分布式电商购物商城 前后端分离 前台商城:Vue全家桶 后台管理系统:Dubbo/SSM/Elasticsearch/Redis/MySQL/ActiveMQ/Shiro/Zookeeper等。 Java基于SOA架构的分布式电商购物商城 前后端分离 前台商城...
swagger-dubbo,:带卷曲的页面:dubbo的招摇.zip
Maven坐标:com.alibaba:dubbo:2.5.8; 标签:alibaba、dubbo、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构...
技术分享:Dubbo详细教程
服务治理:Dubbo 提供了完整的服务治理解决方案,包括服务注册发现、路由、负载均衡、容错机制等,可以很好地管理分布式系统中的各种服务。 智能容错:Dubbo 提供了多种容错机制,包括失败自动切换、失败重试、失败...
Maven坐标:com.alibaba:dubbo:2.6.1; 标签:alibaba、dubbo、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持...
微服务 spring dubbo项目:dubbo rpc;druid数-spring-dubbo-service
Maven坐标:com.alibaba:dubbo:2.6.1; 标签:alibaba、dubbo、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构...
赠送jar包:dubbo-2.5.6.jar; 赠送原API文档:dubbo-2.5.6-javadoc.jar; 赠送源代码:dubbo-2.5.6-sources.jar; 包含翻译后的API文档:dubbo-2.5.6-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId...
第三课:Dubbo 企业级应用进阶1
所以我们了解了架构演变之后,就可以更好的理解分布式,分布式其实就是一种可以实现不同进程之间通讯的架构,然后进程之间怎么通讯的?一般都是通过RPC框架实现。比如Java方面的,Dubbo框架或者Spring Cloud。 3.2) ...
基于dubbo的分布式系统架构视频教程,教你学会怎么搭建分布式系统以及管理