Dubbo

1.什么是SOA架构?

SOAService-Oriented Architecture的首字母简称,它是一种支持面向服务的架构样式。

2.什么是Dubbo?

Dubbo是一个基于RPC的分布式服务框架【只有在分布式

3.什么是RPC?

RPC是远程过程调用的简称,广泛应用在大规模分布式应用中,作用是有助于系统的垂直拆分,使系统更易拓展。Java中的RPC框架比较多,各有特色,广泛使用的有RMI、Hessian、Dubbo等。RPC还有一个特点就是能够跨语言。

4.Dubbo运行原理

节点角色说明:

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。

Monitor: 统计服务的调用次调和调用时间的监控中心。

Container: 服务运行容器。

调用关系说明:

0. 服务容器负责启动,加载,运行服务提供者。

1. 服务提供者在启动时,向注册中心注册自己提供的服务。

2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计

数据到监控中心。

5.什么是Zookeeper?

Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbox 服务的注册中心,工业强度较高,可用于生产环境

6.在Linux系统中安装Zookeeper

安装步骤:

第一步:安装 Linux(此步省略)

设置虚拟机网络连接方式为仅主机。

第2步:下载zookeeper安装包【zookeeper-3.4.6.tar.gz】

第3步:把下载好的zookeeper安装包【zookeeper-3.4.6.tar.gz】通过SecureCRT工具上传到 linux 系统。

3.1 打开Linux

3.2 查看Linux ip地址

3.3 打开SecureCRT工具,连接Linux

3.4  Alt+P 进入SFTP 输入put zookeeper安装包路径上传

3.5 设置zookeeper-3.4.6.tar.gz 的访问权限

3.6 解压缩zookeeper-3.4.6.tar.gz压缩包

[wxrj@wxrj-linux ~]$ tar -zxvf zookeeper-3.4.6.tar.gz

运行结果:

drwxr-xr-x. 10 wxrj wxrj     4096 2? 20 2014 zookeeper-3.4.6

-rwxrwxrwx.  1 wxrj wxrj 17699306 4鏈? 11 2017 zookeeper-3.4.6.tar.gz

3.7进入 zookeeper-3.4.6 目录,创建 data文件夹。

[wxrj@wxrj-linux ~]$ cd zookeeper-3.4.6

[wxrj@wxrj-linux zookeeper-3.4.6]$ mkdir data

3.8进入conf目录 ,把 zoo_sample.cfg 改名为 zoo.cfg

[wxrj@wxrj-linux zookeeper-3.4.6]$ cd conf

[wxrj@wxrj-linux conf]$ mv zoo_sample.cfg   zoo.cfg

运行结果:

-rw-rw-r--. 1 wxrj wxrj  922 2鏈? 20 2014 zoo.cfg

3.9打开zoo.cfg ,  修改 data 属性:dataDir=/home/wxrj/zookeeper-3.4.6/data

-rw-rw-r--. 1 wxrj wxrj  922 2鏈? 20 2014 zoo.cfg

[wxrj@wxrj-linux conf]$ vi  zoo.cfg

点击“i”进入插入模式

 修改 data 属性:dataDir=/home/wxrj/zookeeper-3.4.6/data

按“Esc”,输入“:wq”

3.10 进入bin目录,启动服务输入命令

进入bin目录,启动服务输入命令

[wxrj@wxrj-linux zookeeper-3.4.6]$ cd bin

[wxrj@wxrj-linux bin]$ ./zKServer.sh start

输出以下内容表示启动成功

3.11关闭服务输入命令

[wxrj@wxrj-linux bin]$ ./zKServer.sh stop

输出以下提示信息

7.Dubbo的使用用法

Eclipse中使用dubbo框架

1.配置离线约束

地址:http://code.alibabatech.com/schema/dubbo/dubbo.xsd

配置这个离线约束文件以后我们在开发工程中配置配置文件的时候就可以有dubbo提示

2.服务提供者开发

  1. 创建一个空的项目作为dubbo的工作空间
  2. 删除src目录
  3. 选中空项目右键新建module[新建一个子模块{war}],没有继承关系
  4. 打开dubboxdemoservice子模块的pom导入依赖包,配置tomcat插件

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>dubbo</artifactId>
  <version>2.5.3</version>
</dependency>
<dependency>
  <groupId>org.apache.zookeeper</groupId>
  <artifactId>zookeeper</artifactId>
  <version>3.4.6</version>
</dependency>
<dependency>
  <groupId>com.github.sgroschupf</groupId>
  <artifactId>zkclient</artifactId>
  <version>0.1</version>
</dependency>
配置tomcat插件
<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<configuration>
		<!-- 指定端口 -->
		<port>8080</port>
		<!-- 请求路径 -->
		<path>/</path>
	</configuration>
</plugin>

5.完善子模块的项目结构
6.在web.xml文件中配置加载spring配置文件【applicationContext-service.xml】
<!-- 配置加载spring配置文件 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
7.在resources目录下创建spring配置文件【applicationContext-service.xml】,添加dubbo命名空间地址
<?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">
    
</beans>
8.创建一个业务访问接口
package com.click369.dubbodemo.service;
/**
 * 用户信息的服务层访问接口
 */
public interface UserService {
    //得到用户名
    public String getUserName();
}
9.	创建接口实现类
package com.click369.dubbodemo.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.click369.dubbodemo.service.UserService;
@Service
public class UserServiceImpl implements UserService{
    @Override
    public String getUserName() {
        return "zhangsan";
    }
}
注意:Service注解与原来不同,需要引入com.alibaba包下的
10.	在spring的配置文件中注册服务
<dubbo:application name="dubboxdemoservice"/>
<dubbo:registry address="zookeeper://192.168.137.128:2181"/>
<dubbo:annotation package="com.click369.dubbodemo.service.impl"/>
11.	启动注册中心
12.	测试dubboxdemoservice服务
 


5.3服务消费者开发
1.	选中空项目右键新建module[新建一个子模块{war}],没有继承关系
2.打开dubboxdemoweb子模块的pom导入依赖包,配置tomcat插件,把tomcat插件的运行端口改为9090 。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>dubbo</artifactId>
  <version>2.5.3</version>
</dependency>
<dependency>
  <groupId>org.apache.zookeeper</groupId>
  <artifactId>zookeeper</artifactId>
  <version>3.4.6</version>
</dependency>
<dependency>
  <groupId>com.github.sgroschupf</groupId>
  <artifactId>zkclient</artifactId>
  <version>0.1</version>
</dependency>
配置tomcat插件
<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<configuration>
		<!-- 指定端口 -->
		<port>9090</port>
		<!-- 请求路径 -->
		<path>/</path>
	</configuration>
</plugin>
3.完善子模块的项目结构
4.配置web.xml文件
<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
  </init-param>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置中央处理器-->
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-web.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <!--http://127.0.0.1:8080/springmvc1/add.do-->
  <url-pattern>*.do</url-pattern>
</servlet-mapping>
4.	resources下创建applicationContext-web.xml,添加dubbo命名空间
<?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">

</beans>
5.	创建业务访问接口
package com.click369.dubbodemo.service;
/**
 * 用户信息的服务层访问接口
 */
public interface UserService {
    //得到用户名
    public String getUserName();
}
6.	创建控制器
package com.click369.dubbodemo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.click369.dubbodemo.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Reference
    private UserService userService;
    /**
     *得到用户名的请求处理方法
     */
    @RequestMapping("/show.do")
    @ResponseBody
    public  String  showUserName(){
        return userService.getUserName();
    }
}
7.	在spring的配置文件中订阅服务
<mvc:annotation-driven >
		<mvc:message-converters register-defaults="false">
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">  
				<constructor-arg value="UTF-8" />
			</bean>  
		</mvc:message-converters>	
	</mvc:annotation-driven>
<!-- 消费者订阅服务 -->
<dubbo:application name="dubboxdemoweb" />
<dubbo:registry address="zookeeper://192.168.137.128:2181"/>
<dubbo:annotation package="com.click369.dubbodemo.controller" />
8.启动注册中心
9.启动dubboxdemoservice服务
10.启动dubboxdemoweb服务

连接zookeeper失败
运行dubboxdemo-service以后,控制输出的提示信息的最后一行中没有出现下面语句
“信息: Starting ProtocolHandler ["http-bio-8080"]”
,说明dubboxdemo-service程序没有正常连接到注册中心所在的计算机。
原因:可能是注册中心所在的计算机【Linux ContOS6.5】的防火墙未关闭。
解决方法:
计算机【Linux ContOS6.5】
切换至root账号下
[wxrj@wxrj-linux ~]$ su root
密码:
[root@wxrj-linux ~]#
2.永久性关闭防火墙
[root@wxrj-linux ~]# chkconfig   iptables  off
3.查看防火墙状态
[root@wxrj-linux ~]# service iptables status
iptables:未运行防火墙。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值