zookeeper安装以及dubbo学习笔记

dubbo

dubbo3.0中文文档
链接:https://pan.baidu.com/s/1lxlNFQev19RKjXZKxxegEA
提取码:bbab

一个高性能javaRPC框架。
R:Registry 注册中心
P:Provider 服务提供者
C:Consumer 服务消费者
三大核心能力:
1、面向接口的远程方法调用
2、智能容错和均衡负载
3、服务自动注册和发现

在这里插入图片描述
dubbo运行流程:
1、服务支持者将所有服务的URL地址注册到注册中心
2、服务消费者向注册中心订阅需要的方法
3、注册中心提供给服务消费者它需求的方法
4、服务消费者调用服务提供者的方法。
5、Monitor监控中心。服务消费者和提供者在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

<!--指定dubbo的协议名称和端口号-->
    <dubbo:protocol name="dubbo" port="20880"/>

zookeeper安装

官网网址https://zookeeper.apache.org/releases.html
在这里插入图片描述在这里插入图片描述
点击之后即可下载。
下载完成后,解压
在这里插入图片描述
将 conf 目录下的 zoo_sample.cfg 文件,复制一份,重命名为 zoo.cfg:
在这里插入图片描述

在安装目录下面新建一个空的 data 文件夹和 log 文件夹:
在这里插入图片描述

修改 zoo.cfg 配置文件,将 dataDir=/tmp/zookeeper 修改成 zookeeper 安装目录所在的 data 文件夹,再添加一条添加数据日志的配置(需要根据自己的安装路径修改)。这里路径要注意是\或者/
在这里插入图片描述

双击 zkServer.cmd 启动程序:
在这里插入图片描述

控制台显示 bind to port 0.0.0.0/0.0.0.0:2181,表示服务端启动成功!
在这里插入图片描述

双击zkCli.cmd 启动客户端
在这里插入图片描述
出现 Welcome to Zookeeper!,表示我们成功启动客户端

dubbo配置原则:

    1、在服务提供者配置访问参数
    2、关闭检查。
    3、重试次数,默认重试1次。可以通过retries=“” 来设置重试次数(不包括默认次数)。
例如:<dubbo:service retries=“2”/>或者<dubbo:refrence retries=“2”/>
    4、超时时间。服务提供者dubbo:service中可以设置timeout=“毫秒数”
    5、版本号。每个接口都应定义版本号,为后续不兼容升级提供可能。当一个接口有不同的实现,项目早期使用的一个实现类,之后创建接口新的实现类,两者之间通过version区分

interface

只提供模型和接口。
在这里插入图片描述

public interface SomeService {
    String hello(String msg);

    /**
     * 根据用户标识,获取用户详情
     * @param id
     * @return
     */
    User queryUserById(Integer id);
}

multi-zk-provider

服务提供者。依赖interface,实现接口。
在这里插入图片描述

dubbo-mul-zk-provider.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"
       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="multi-zk-provider"/>
	//官方推荐使用dubbo协议,端口号默认20880
    <dubbo:protocol id="dubbo" port="20880"/>
	//指定zookeeper注册中心,zookeeper端口号默认2181
    <dubbo:registry address="zookeeper://localhost:2181"/>
	//暴露服务 因为一个接口存在多个实现类,因此指定其版本号。
	//timeout:指定服务超时的时间
    <dubbo:service interface="com.xycf.dubbo.service.SomeService" ref="someServiceImpl" version="1.0" timeout="35000"/>
    <dubbo:service interface="com.xycf.dubbo.service.SomeService" ref="newSomeServiceImpl" version="2.0" timeout="35000"/>
	//加载实现接口的类
    <bean id="someServiceImpl" class="com.xycf.dubbo.service.impl.SomeServiceImpl"/>

    <bean id="newSomeServiceImpl" class="com.xycf.dubbo.service.impl.NewSomeServiceImpl"/>
</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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:dubbo-zk-zookeeper.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app >

multi-zk-consumer

提供web接口供客户端访问。
在这里插入图片描述

SomeController

@Controller
public class SomeController {
    @Resource
    private SomeService someService;
    @Resource
    private SomeService newSomeService;

    @RequestMapping("/hello")
    public String hello(Model model){
        String hello = someService.hello(null);
        model.addAttribute("hello",hello);
        String hello2 = newSomeService.hello(null);
        model.addAttribute("hello2",hello2);
        return "hello";
    }
    @RequestMapping("/user/detail")
    public String userDetail(Model model,Integer id){
        User user = someService.queryUserById(id);
        model.addAttribute("user",user);
        User user2 = newSomeService.queryUserById(id);
        model.addAttribute("user2",user2);
        return "userDetail";
    }
}

dubbo-mul-zk-consumer.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"
       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="multi-zk-consumer"/>

    <!--指定注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--引用远程接口-->
    <!--id:远程接口服务的代理对象,接口的全限定名,url:调用远程接口服务的地址,registry:直连方式,不适用注册中心N/A-->
<!--    <dubbo:reference id="someService" interface="com.xycf.dubbo.service.SomeService" url="dubbo://localhost:20880" registry="N/A"/>-->
    <dubbo:reference id="someService" interface="com.xycf.dubbo.service.SomeService" version="1.0"/>
    <dubbo:reference id="newSomeService" interface="com.xycf.dubbo.service.SomeService" version="2.0"/>
</beans>

springmvc.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: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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.xycf.dubbo.web"/>

    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </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">
    <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-mul-zk-consumer.xml,classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值