Centos7 简单 搭建Dubbo服务

1 篇文章 0 订阅
1 篇文章 0 订阅

 Dubbo框架图

由上图可见,Dubbo框架分类提供者(Provider)、消费者(Consumer)、注册中心(Registry)、监视器(Monitor可以不要)组成;

搭建步骤可以分为四步:

  1. 使用zookeeper创建注册中心;
  2. 创建提供者服务;
  3. 创建消费者服务;
  4. 创建监视器;

 第一步:安装zookeeper

  • 下载zookeeper

下载地址:https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/

下载文件:zookeeper-3.4.14.tar.gz

  • 上传到服务器安装
  1. 服务器上创建一个固定目录 mkdir zookeeper创建;
  2. 使用rz命令将安装包上传到该目录;
  3. 解压 tar zxvf zookeeper-3.4.14.tar.gz;
  4. 进入解压目录的conf目录;mv zoo_sample.cfg   zoo.cfg
  5. 在解压目录下创建 data目录;mkdir data;

  1. 修改zoo.cfg配置文件

注意几个重要位置:

dataDir=./   临时数据存储的目录(可写相对路径)改成我们刚建立的data路径

clientPort=2181   zookeeper的默认端口号

bin目录启zookeeper服务 

  • 1. 启动ZK服务: bin/zkServer.sh start
  • 2. 查看ZK服务状态: bin/zkServer.sh status
  • 3. 停止ZK服务: bin/zkServer.sh stop
  • 4. 重启ZK服务: bin/zkServer.sh restart
  • 5. 连接服务器: zkCli.sh -server 127.0.0.1:2181

第二步:创建提供者服务端

项目结构:

项目内容直接下载,不再展示

DubboWeb.rar

ApplicationContext.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://code.alibabatech.com/schema/dubbo
	http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
   <!-- bean definitions go here--> 
   <!-- 配置dubbo的应用名称 -->
   <dubbo:application name="student_service"></dubbo:application>
   <!-- 配置dubbo的配置中心  192.168.234.118-->
   <!--  dubbo:registry  address="zookeeper://122.51.144.197:2181" ></dubbo:registry>-->
   <dubbo:registry  protocol="zookeeper" address="zookeeper://122.51.144.197:2181" ></dubbo:registry>
   <!-- 配置dubbo的扫描包 将@service注解的类放入dubbo扫描包,供dubbo远程调用(RPC)时使用-->
   <dubbo:annotation package="com.test.impl"></dubbo:annotation>
	<!-- 注入到spring的 ioc容器中 -->
   <context:component-scan base-package="com.test.impl"></context:component-scan>
	 
</beans>

启动服务端

eclipse  创建maven命令:右键项目 -》run as -》创建maven build-》Goals ->tomcat7:run

启动服务:

 第三步 :创建消费者

项目目录架构:

代码直接下载:

dubbo-client.rar

控制层代码如下:

package org.controller;

//import org.server.StudentServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.test.api.StudentServer;

@Controller
@ResponseBody
@RequestMapping("StudentController")
public class StudentController {
	@Reference StudentServer studentServer;	
	@RequestMapping("rpcServer")
	public String rpcServer() {
		String respmsg=studentServer.server("你好");
		System.out.println("respmsg:"+respmsg);
		return respmsg;
	}
}

spring-mvc.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://code.alibabatech.com/schema/dubbo
	http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
   <dubbo:annotation package="org.controller"></dubbo:annotation>
   <!--  <context:annotation-config/>-->
   <!-- bean definitions go here--> 
   <!-- 配置dubbo的应用名称 -->
   <dubbo:application name="student_consumer"></dubbo:application>
   <!-- 配置dubbo的配置中心 -->
   <dubbo:registry  protocol="zookeeper" address="zookeeper://122.51.144.197:2181" ></dubbo:registry>
   <!--  <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.234.118:2181" ></dubbo:registry>-->
   <!-- 配置dubbo的扫描包 将@service注解的类放入dubbo扫描包,供dubbo远程调用(RPC)时使用-->
   
	<!-- 要引用的服务,生成远程服务代理,可以像使用本地bean一样使用 -->
	<dubbo:reference id="StudentServer" interface="com.test.api.StudentServer" /> 
   <!-- 注入到spring的 ioc容器中 -->
   <context:component-scan base-package="org.controller"></context:component-scan>
	
</beans>

eclipse  创建maven命令:右键项目 -》run as -》创建maven build-》Goals ->tomcat7:run

此时项目已经可以运行了,为了可视化管理服务,我们创建监管者,监管服务情况

 第四步:安装dubbo-admin

github下载dubbo-admin,打成war包,放在tomcat容器中webapp目录中,重启tomcat服务;

下载地址:https://github.com/apache/dubbo-admin

  • 进入目录,修改dubbo-admin配置

修改 src\main\resources\application.properties 指定zookeeper地址

我打好的war包:

dubbo-admin.war

bin目录重新启动tomcat startup.sh

默认登录用户 admin 密码guest

 

 登录dubbo-admin可以查看服务情况如下

访问消费方服务地址:

 http://localhost:8882/StudentController/rpcServer.action

效果如下:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值