Dubbo-Zookeeper注册中心;监控中心
Posted MinggeQingchun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo-Zookeeper注册中心;监控中心相关的知识,希望对你有一定的参考价值。
一、ZooKeeper注册中心
对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也 不断膨胀;对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统,需要管理大量的服务调用。
而且,对于服务提供方和服务消费方来说,他们还有可能兼具这两种角色,即需要提供服务,有需要消费服务。 通过将服务统一管理起来,可以有效地优化内部应用对服务发布使用的流程和管理。服务注册中心可以通过特定协议来完成服务对外的统一
Dubbo 提供的注册中心有如下几种类型可供选:
Multicast 注册中心:组播方式
Redis 注册中心:使用 Redis 作为注册中心
Simple 注册中心:就是一个 dubbo 服务。作为注册中心。提供查找服务的功能。
Zookeeper 注册中心:使用 Zookeeper 作为注册中心
Dubbo推荐使用 Zookeeper 注册中心
Zookeeper是Apache下一个高性能的,分布式的,开放源码的分布式应用程序协调服务。简称 zk
ZooKeeper主要服务于分布式系统,可以用ZooKeeper来做:统一配置管理、统一命名服务、分布式锁、集群管理
ZooKeeper官网
注:
Zookeeper 运行需要 java 环境
(一)Windows下安装ZooKeeper
1、zookeeper官网 https://zookeeper.apache.org/index.html
https://zookeeper.apache.org/releases.html#download
2、下载之后解压到目标路径即可
(二)zoo.cfg配置文件
1、进入conf目录下,复制 zoo_sample.cfg文件,并将其改名为 zoo.cfg
2、修改zoo.cfg 配置文件
配置文件主要参数如下:
#ZK中的时间配置最小但域,其他时间配置以整数倍tickTime计算
tickTime=2000
#Leader允许Follower启动时在initLimit时间内完成数据同步,单位:tickTime
initLimit=10
#Leader发送心跳包给集群中所有Follower,若Follower在syncLimit时间内没有响应,那么Leader就认为该follower已经挂掉了,单位:tickTime
syncLimit=5
#配置ZK的数据目录
dataDir=/usr/local/zookeeper/data
#用于接收客户端请求的端口号
clientPort=2181
#配置ZK的日志目录
dataLogDir=/usr/local/zookeeper/logs
#ZK集群节点配置,端口号2888用于集群节点之间数据通信,端口号3888用于集群中Leader选举
server.1=192.168.123.100:2888:3888
server.2=192.168.123.101:2888:3888
进入到 bin 目录下,点击 zkServer.cmd
或者 在 bin 目录下 输入 “cmd”命令
看到绑定成功即可
使用ZooKeeper示例
1、创建maven Java模块
IDEA下执行install命令,打成jar包
2、创建 maven web 服务提供者
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>dubbo-3-zk-userservice-provider</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<!--Spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--接口工程依赖-->
<dependency>
<groupId>com.mycompany.dubbo</groupId>
<artifactId>dubbo-3-zk-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!-- zookeeper依赖 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--JDK1.8编译插件-->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
dubbo-zk-userservice-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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服务提供者声明名称:必须保证服务名称的唯一性(dubbo内部使用的唯一标识符)-->
<dubbo:application name="dubbo-3-zk-userservice-provider" />
<!-- 访问服务协议的名称以及端口号,dubbo官方推荐使用dubbo协议,端口号默认20880
name:指定协议名称
port:指定协议的端口号(默认为20880)
-->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 使用zookeeper注册中心
指定注册中心地址和端口号
-->
<!--使用本机中的 zookeeper注册中心-->
<dubbo:registry address="zookeeper://localhost:2181" />
<!--使用linux系统中的zookeeper服务-->
<!-- <dubbo:registry address="zookeeper://192.168.133.128:2181"/>-->
<!-- 暴露服务接口
dubbo:service
interface:暴露服务接口的全限定类名
ref:接口引用的实现类在spring容器中的标识
registry:如果不使用注册中心,值为:N/A
-->
<dubbo:service interface="com.mycompany.dubbo.service.UserService" ref="userService" />
<!-- 将接口实现类加载到spring容器 -->
<bean id="userService" class="com.mycompany.dubbo.service.impl.UserServiceImpl" />
</beans>
如果有多个服务提供者
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 服务提供者声明名称:必须保证服务名称的唯一性(dubbo内部使用的唯一标识符)-->
<dubbo:application name="dubbo-3-zk-userservice-provider" />
<!-- 访问服务协议的名称以及端口号,dubbo官方推荐使用dubbo协议,端口号默认20880
name:指定协议名称
port:指定协议的端口号(默认为20880)
-->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 使用zookeeper注册中心
指定注册中心地址和端口号
-->
<!--使用本机中的 zookeeper注册中心-->
<dubbo:registry address="zookeeper://localhost:2181" />
<!--使用linux系统中的zookeeper服务-->
<!-- <dubbo:registry address="zookeeper://192.168.133.128:2181"/>-->
<!-- 暴露服务接口
dubbo:service
interface:暴露服务接口的全限定类名
ref:接口引用的实现类在spring容器中的标识
registry:如果不使用注册中心,值为:N/A
-->
<dubbo:service interface="com.mycompany.dubbo.service.UserService" ref="userService" version="1.0.0" timeout="15000"/>
<dubbo:service interface="com.mycompany.dubbo.service.UserService" ref="userService2" version="2.0.0" />
<!-- 将接口实现类加载到spring容器 -->
<bean id="userService" class="com.mycompany.dubbo.service.impl.UserServiceImpl" />
<bean id="userService2" class="com.mycompany.dubbo.service.impl.UserServiceImpl2" />
</beans>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-zk-userservice-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、创建maven web 消费者
pom.xml
<groupId>com.mycompany.dubbo</groupId>
<artifactId>dubbo-3-zk-consumer</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<!--Spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--接口工程依赖-->
<dependency>
<groupId>com.mycompany.dubbo</groupId>
<artifactId>dubbo-3-zk-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!-- zookeeper依赖 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--JDK1.8编译插件-->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
UserController
@Controller
public class UserController
@Autowired
private UserService userService;
@RequestMapping(value = "/userDetail")
public String userDetail(Model model,Integer id)
//根据用户标识获取用户详情
User user = userService.queryUserById(id);
//获取用户总人数
Integer allUserCount = userService.queryAllUserCount();
model.addAttribute("user",user);
model.addAttribute("allUserCount",allUserCount);
return "userDetail";
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: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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.mycompany.dubbo.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
dubbo-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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--声明dubbo服务消费者名称:保证唯一性-->
<dubbo:application name="dubbo-3-zk-consumer"/>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用linux系统中的zookeeper服务-->
<!-- <dubbo:registry address="zookeeper://192.168.133.128:2181"/>-->
<!--引用远程接口服务-->
<dubbo:reference id="userService" interface="com.mycompany.dubbo.service.UserService"/>
</beans>
如果有多个消费者
<?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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--声明dubbo服务消费者名称:保证唯一性-->
<dubbo:application name="dubbo-4-zk-multi-consumer"/>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用linux系统中的zookeeper服务-->
<!-- <dubbo:registry address="zookeeper://192.168.133.128:2181"/>-->
<!--引用远程接口服务-->
<dubbo:reference id="userService" interface="com.mycompany.dubbo.service.UserService" version="1.0.0" />
<dubbo:reference id="userService2" interface="com.mycompany.dubbo.service.UserService" version="2.0.0" />
</beans>
web.xml
<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.xml,classpath:dubbo-zk-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
配置Tomcat访问
二、监控中心
dubbo 的使用,其实只需要有注册中心,消费者,提供者这三个就可以使用了,但是并不能看到有哪些消费者和提供者,为了更好的调试,发现问题,解决问题,因此引入dubbo-admin
通过 dubbo-admin 可以对消费者和提供者进行管理。可以在 dubbo 应用部署做动态的调整,服务的管理。
1、下载监控中心,https://github.com/alibaba/dubbo
2、通过IDEA将其打包war
3、把 dubbo-admin.war 文件拷贝到 tomcat 的 webapps
4、通过压缩工具打开dubbo-admin-2.5.10.war包 应用的 WEB-INF/dubbo-properties 文件,内容如下
5、在浏览器地址栏输入 http://localhost:8080/dubbo-admin ;访问监控中心-控制台
以上是关于Dubbo-Zookeeper注册中心;监控中心的主要内容,如果未能解决你的问题,请参考以下文章