Dubbo
Dubbo是轻量级开源的Java RPC(远程过程调用)分布式框架,它的核心主要有三个,比如注册服务和发现服务,远程服务的调用,还有负载均衡
流程解释
1.Container是spring容器开始启动然后装载Provider提供者
2.将提供者注册到注册中心Registry
3.消费者Consumer到Registry获取地址,返回给Consumer
4.Consumer根据地址执行提供者提供的接口
5.Monitor监控中心监控提供者和消费者的状态
----------------------------------------------------------
使用注册中心,分布式开发
实际开发中不是用的直连方式,而是通过注册中心管理服务,注册中心有很多,比如Nacos,Zookeeper等等,官方推荐使用Zookeeper,Zookeeper它其实就是一个一个的节点,每个节点下面就是提供者和服务者
下载zookeeper使用
Zookeeper官网下载==>点击跳转
在windows系统安装Zookeeper的步骤
解压压缩包到当前文件夹,在Zookeeper目录下创建data目录,然后找到conf目录下的zoo_sample.cfg文件,复制一个副本改名为zoo.cfg
打开zoo.cfg文件,dataDir表示临时存放的数据位置,改为新建的data目录的全路径,在clientPort=2181下面新建参数admin.serverPort=8888,然后保存文件
打开bin目录,找到zkServer.cmd双击运行
如果有binding to port 0.0.0.0/0.0.0.0:2181的字样表示启动成功!
在linux系统上安装Zookeeper的步骤
通过Xftp将gz压缩包传输到linux系统上/opt/gz_package,通过命令tar -zxvf zookeeper… -C /opt,解压压缩包到目标目录下
进入conf目录,执行强制递归复制目录命令cp -r zoo_sample.cfg zoo.cfg
进入zoo.cfg,修改dataDir=/opt/zookeeper-3.7.0/data,在clientPort=2181下面新建参数admin.serverPort=8888,然后保存文件
之后进入bin目录下,执行命令./zkServer.sh start,启动zookeeper,执行命名./zkServer.sh stop,停止zookeeper
---------------------------------------------------
第一步:创建好三个工程的基本结构,在接口工程,编写实现类和业务接口
实体类代码如下
public class User implements Serializable {
private int id;
private String name;
private int age;
private char sex;
@Override
public String toString() {
return "用户{" +
"id=" + id +
", 姓名='" + name + '\'' +
", 年龄=" + age +
", 性别=" + sex +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
}
业务接口如下
public interface UserService {
String hello();
User selecetById(Integer id);
}
第二步:在提供者工程下添加相关的依赖
<dependencies>
<!--测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.16</version>
</dependency>
<!--接口工程依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!--Zookeeper注册中心依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
第三步:在提供者工程下编写接口工程下的接口实现类
public class UserServiceImpl implements UserService {
@Override
public String hello() {
return "Hello World!";
}
@Override
public User selecetById(Integer id) {
User user = new User();
user.setId(id);
user.setName("张三");
user.setAge(18);
user.setSex('男');
return user;
}
}
第四步:在提供者工程下创建spring配置文件,配置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">
<!--声明提供者服务的名称,保证唯一性,推荐使用工程名-->
<dubbo:application name="dubbo-provider"></dubbo:application>
<!--指定dubbo的协议和端口号,协议推荐dubbo,端口号默认为20880-->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
<!--配置暴露的接口
interface:接口工程下的全接口名
registry:N/A表示不使用注册中心
ref:指定接口实现类
-->
<dubbo:service interface="com.alibaba.dubbo.service.UserService"
ref="userServiceImpl">
</dubbo:service>
<!--接口实现类-->
<bean id="userServiceImpl" class="com.alibaba.dubbo.service.serviceImpl.UserServiceImpl"></bean>
</beans>
第五步:编写web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--spring的监听器,创建spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--指定需要加载的spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-provider.xml</param-value>
</context-param>
</web-app>
第六步:在消费者工程下添加依赖,跟提供者依赖一模一样
第七步:编写控制层代码,注入依赖,将暴露的接口数据添加到model中,返回给界面
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/hello")
public String hello(Model model){
String hello = userService.hello();
model.addAttribute("hello",hello);
return "hello";
}
@RequestMapping("/user")
public String selectById(Model model,Integer id){
User user = userService.selecetById(id);
model.addAttribute("user",user);
return "user";
}
}
第八步:编写spring配置文件,关于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">
<!--声明提供者服务的名称,保证唯一性,推荐使用工程名-->
<dubbo:application name="dubbo-consumer"></dubbo:application>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
<!--引用远程接口服务-->
<dubbo:reference interface="com.alibaba.dubbo.service.UserService"
id="userService">
</dubbo:reference>
</beans>
第九步:编写spring配置文件,关于springmvc的,这里需要注意配置注解驱动导入的是mvc结尾的约束文件,否则无效!
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.alibaba.dubbo.controller"></context:component-scan>
<!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--视图解析器-->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第十步:编写消费者的web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--创建springmvc容器和加载spring配置文件-->
<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:dubbo-consumer.xml,classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!--所有的请求都走DispatcherServlet这个servlet-->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第十一步:编写hello的jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello页面</title>
</head>
<body>
<h3>hello jsp</h3>
Java 第一课: <div>${hello}</div>
</body>
</html>
第十二步:编写user的jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>user页面</title>
</head>
<body>
编号:<div>${user.id}</div>
姓名:<div>${user.name}</div>
年龄:<div>${user.age}</div>
性别:<div>${user.sex}</div>
</body>
</html>
第十三步,启动zookeeper注册中心,在bin目录下zkserver.cmd命令
第十四步,启动提供者和消费的服务器,注意两个服务的端口号不能一样
出现的问题:
1.dubbo依赖自带的有spring的依赖,会造成版本冲突,
解决办法,在dubbo依赖里面加入以下依赖即可
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
问题二:访问hello.jsp的时候,注意是在consumer项目下,路径的根是consumer,而不是provider!
问题三:如果启动失败可能是依赖的原因,dubbo依赖的版本2.6.0之前,注册中心zookeeper的依赖不是curator-framework,将dubbo依赖版本改成2.6.0之后,比如2.6.2
第十五步:打开浏览器,访问数据成功!
----------------------------------------------
使用监控中心
网上下载dubbo监控中心的jar包
比如下载的dubbo-admin-0.0.1-SNAPSHOT.jar
可以使用rar压缩文件打开jar包,找到BOOT-INF下的classes下的有一个文件application.properties,里面是核心配置的信息,内容如下,主要是配置zookeeper注册中心的协议 + ip + 端口号,网上下载的已经配置好了
server.port=7001
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.layout-url=/templates/default.vm
spring.messages.fallback-to-system-locale=false
spring.messages.basename=i18n/message
spring.root.password=root
spring.guest.password=guest
dubbo.registry.address=zookeeper://127.0.0.1:2181
使用监控中心的步骤
首先启动zookeeper注册中心,进入zookeeper的bin目录下启动zkservice.cmd命令,然后进入jar包所在目录,输入cmd打开dos窗口,输入命令java -jar dubbo…(jar包名字,快捷键tab),回车启动jar包
启动提供者和消费者服务,打开浏览器输入127.0.0.1:7001访问监控中心,账户密码默认是root,最后访问成功!