开发者学堂课程【基于 Zookeeper、Dubbo 构建互联网分布式基础架构:基于 Zookeeper、Dubbo 构建互联网分布式基础架构(3) 】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/635/detail/10164
基于 Zookeeper、Dubbo 构建互联网分布式基础架构(3)
目录:
一.面向接口编程
二.插件
三.测试
四.添加一个依赖
五.Spring.MVC.xml
六.Spring.MVC.xml 相关参数
七.运行测试
一、面向接口编程
<!--扫描注解-->
<context:component-scan base-package-"com.qianfeng"/>
<!--使用dubbo的另外一个原因,可以和spring无缝整合-->
<!--目的:找到注册中心,告诉它我是谁,我的端口是多少
<!--配置别名,目的是在后台可以看到这个服务的别名,好区分到底是谁,这个参数可以随便写,但是最好是通俗易懂-->
<dubbo:application name-"test1"/>
<!--2配置注册中心
address 注册中心地址 ,protocol 指的是注册中心的协议格式<dubbo:registry address="192.168.3.224:2181"protocol-"zookeeper"/>
告诉注册中心我是谁
interfaceftan代表的就是类型
ref代表的就是到底具体发布哪个服务
timeout连接超时时间<dubbo:service interface-"com.gianfeng.testduboo.TestService"ref=" testServiceImpl"timeout="60000"/>
<!--配置我的端口,因为消费者要想连接我们,必须得通过我们的ip和端口进行访问,我们在连接注册中心的时候注册中心就知道了我们的ip,但是你这个服务的端口并不知道,所以需要设置端口.端口可以随便写,但是不能被其他占用,一个dubbo对外发布的时候必须独占一个端口
dubbo:protocol name="dubbo port=12345"
</beans>
二.插件(指定不同 tomcat)
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9000</port>
<path>/</path>
</configurat ion>
</plugin>
</plugins>
</build>
</project>
三.测试:
访问192.168.3.224:8080/dubbo,用户名与密码均为root,访问成功进入页面显示为com.qianfong.tostcuboo.TestService,提供者名称为192.168.3.84:12345,但目前没有使用者.
四.添加一个依赖
<dependency>
<groupId-org.sLf4j</groupId>
<arifactodstf4j-ai/artifactId>
<version>1.7.25</version»
</dependency>
五.Spring.MVC.xml
<?xml version-"1.0"encoding-"UTF-8"?>
<beans xmlns "http:1/ww,springfranework.org/schema/beans"
xmlns:xsi-"http://ww.v3.org/2001/XMLSchema-instance"
xmlns:nvç-"http:llww.springframework.org/schema/nyc"
xmlns:dubbo-"http://code,dlibabatech,com/5chema/dubbo "
xmlns:context="http://ww.springframework.org/schema/context"
xsi:schemaLocation-"http:/Www.5pring franework.org/schema/evc
http://ww.springframework.org/schema/mvcsrhttp://www,springframework.org/schena/beans
http://ww.springfranework,org/schema/beans/spring-beans-4.3
http://ww,spring framework,org/schema/context
http://www.springt
framework.org/schema context/spring-contex
http://code.alibabatech,con/schema/dubbo
http://code.alibabatech,com/schema/dubbo/dubbo.xsd">
<cantext:component-scan base -package -"com.g1anfeng"/>
<!查找远程服务,找到对应的注册中心告诉它我需要个什么类型的服务
<1配置我是谁--><dubbo:application name"xiaofeizhe/>
<2配置注册中心--><dubbo:registry address="192.168.3.224.2181"protocol-"zookeeper"/>
<3告诉他我要什么--><dubbo:reference interface="con,uiantengLestdubog.TestService"id=" testService"/>
</beans>
六.spring.MVC 相关参数
web-app xmlns-"http:/7xmlns.cp.org/xmL/ns/)avace"
xmlns:xs="http://ww-w3,rg/2001/XMLSchema-instance"
xs1:schemaLocation-"http://xmlns.cp.org/xml/ns/avaee
http://xmlns.jcp.org/xml/ns/javaee/web
app_3.1.xsd"
version="3.1"
metadata-complete-"trua"s
<servlet>
<servlet-name>springmvcservlet</servlet-name>
<servlet-class>org.springframework.web.servlet DispatcherServlet
</servlet-class><init-param
<param-name>contextConfigLocation</param-name>
<param-value>cl
asspath:Spring-HVc.xml</param-value>
<1init-param>
<!-启动项目的时候就创建 servlet 的对象,这个参数必须在后面出现<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvcservlet</servlet-name>
<url-pattern>*.actionk/url-pattern>
</servlet-mapping>
</web-app>
七、运行测试
运行结果为有一个 zookeeper 没有连接上,注意不要在开热点的情况下运行,容易断开无连接。
成功则显示Successed conmect to server/192.168.3.84:12345 from NettycLient.192.168.3.84 using dubbo version 2.0.1.channel is NettyChannel.channele 11d: 0x3600898c,/192.168.384:49969 D/192.168.3.84:12345.dubbo version:2.0.1,current host: 192.168.3.84.DNFO AbstractCLlent:91.
修改代码为
@Resource
private TestService
@RequestMapping"/getData" )
@ResponseBody
public String getData(String name)
Stringata = serv1ce.getData(name);
return data;
成功访问显示为