Dubbo:是apache提供的一个RPC框架(远程调用框架),解决分布式系统中web层和service层的调用。
Dubbo官网地址:[http://dubbo.apache.org]
dubbo的结构:
安装教程可参考:
下载地址
直接下载zookeeper-3.4.6.zip,解压后进入bin目录。双击zkServer.cmd运行如下图所示:
注意:打开后不要选择任何,不要点击里面的内容,否则会进入选择状态,就无法运行,也不能关闭窗口。
虚拟机安装启动方式如下:
第一步:安装 jdk(略)
第二步:把 zookeeper 的压缩包(zookeeper-3.4.6.tar.gz)上传到 linux 系统
第三步:解压缩压缩包
tar -zxvf zookeeper-3.4.6.tar.gz
第四步:进入zookeeper-3.4.6目录,创建data目录
mkdir data
第五步:进入conf目录 ,把zoo_sample.cfg 改名为zoo.cfg
cd conf
mv zoo_sample.cfg zoo.cfg
第六步:打开zoo.cfg文件, 修改data属性:
dataDir=/root/zookeeper-3.4.6/data
进入bin目录,启动服务命令
./zkServer.sh start
停止服务命令
./zkServer.sh stop
查看服务状态:
./zkServer.sh status
zookeeper安装成功之后暴露一个请求的地址:zookeeper://192.168.74.147:2181
192.168.74.147是虚拟机的IP地址,2181是zookeeper默认端口。
dubbo-admin:是dubbo官方提供的一个dubbo框架的监控中心,本质是一个war包。将此war包修改成压缩包zip格式解压,配置之后,部署到tomcat中即可。注意:2.6.0版只支持jdk1.8
dubbo.registry.address=zookeeper://192.168.74.147:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
注意:IP地址要与注册中心对应
在Tomcat中启动成功之后,输入http://localhost:8080/dubbo-admin-2.6.0/,默认用户名和密码都是root。
这是要发布到服务器上的引用,所以要打成war包,在idea中可使用JBLJavaToWeb插件快速转成web项目。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${
spring.version}</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
package cn.itcast.service;
public interface HelloService {
String sayHello(String name);
}
(2) 编写service接口的实现类HelloServiceImpl
package cn.itcast.service.impl;
import cn.itcast.service.HelloService;
import com.alibaba.dubbo.config.annotation.Service;
/**
* 声明:提供一个dubbo服务
* @Service :alibaba包下的注解
* 服务消费者就可以通过远程调用的形式,调用此服务中的所有方法
*/
@Service
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
System.out.println("参数name="+name);
return "hi:"+name;
}
}
注意:1. service实现类上使用dubbo提供的@Service暴露服务
2. 消费者就可以调用此服务的所有方法
3. 注册到注册中心的服务名称:当前接口的全类名
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--要求:dubbo配置文件不需要记忆(需要会改)-->
<description>spring整合dubbo的服务提供者</description>
<!--1.配置应用名称-->
<dubbo:application name="dubbodemo_provider"></dubbo:application>
<!--2.配置注册中心请求地址-->
<dubbo:registry address="zookeeper://192.168.74.147:2181"></dubbo:registry>
<!--3.配置协议类型
数据的传输协议:dubbo
端口:20881开始
-->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!--4.配置dubbo注解的包扫描-->
<dubbo:annotation package="cn.itcast.service"></dubbo:annotation>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--配置在tomcat启动的时候,加载spring配置文件-->
<!--
因为这里没有servlet容器,所以使用监听器
加载spring配置文件
1、监听器
2、springmvc的控制器servlet
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${
spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${
spring.version}</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
package cn.itcast.controller;
import cn.itcast.service.HelloService;
import com.alibaba.dubbo.config.annotation.Reference;
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;
@Controller
public class HelloController {
/**
* 在controller中发起远程调用到提供者的HelloService服务中
* 在属性上配置一个注解
* 1.根据接口属性获取全类名:cn.itcast.service.HelloService
* 2.根据类型作为服务名称从注册中心中获取信息
* 3.自动调用内部的方法
*/
@Reference
private HelloService helloService;
@RequestMapping("/hello")
public @ResponseBody String hello() {
String str = helloService.sayHello("heima127");
return str;
}
}
注意:1.如果需要调用dubbo服务提供者
2.配置接口的私有属性,在属性上通过@Reference注解配置
3.根据接口的全类名找到远程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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--常规的mvc配置-->
<!--注解扫描-->
<context:component-scan base-package="cn.itcast.controller"></context:component-scan>
<!--驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--spring整合dubbo服务消费者-->
<!--1.应用名-->
<dubbo:application name="dubbodemo_consumer"></dubbo:application>
<!--2.注册中心-->
<dubbo:registry address="zookeeper://192.168.74.147:2181"></dubbo:registry>
<!--3.dubbo包扫描-->
<dubbo:annotation package="cn.itcast.controller"></dubbo:annotation>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--配置spingmvc:dispathcerServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--当tomcat启动,初始化servlet,创建spring容器-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
方式一:入门案例
将工程部署到tomcat中运行。优势:安全可靠,缺点:不利于开发
tomcat–》web.xml–》spring配置
方式二:通过java的启动类
可以通过编写一个java类,在main方法中加载spring配置文件
缺点:不安全,不可靠,优点:适用于开发阶段
如果使用方式二,不需要配置web.xml
在工程中配一个启动类:
执行main方法即可启动服务提供者
package cn.itcast.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* 启动类:
* 加载spring配置文件,引导dubbo服务提供者启动
*/
public class HelloProvider {
public static void main(String[] args) throws IOException {
//1.加载spring配置文件
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-provider.xml");
//2.启动spring容器
ac.start();
//3.输入任意都关闭,使用read
System.in.read();
}
}
如果同时配置多个相同服务时,需要在applicationContext-provider.xml文件中修改
<!--3.配置协议类型
数据的传输协议:dubbo
端口:20881开始,累加1
-->
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
为了解决service接口的重复性问题。将service接口提供一个maven模块。提供者和消费者共同依赖于此maven模块。