Dubbo-Zookeeper注册中心;监控中心

一、ZooKeeper注册中心

对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也 不断膨胀;对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统,需要管理大量的服务调用。

而且,对于服务提供方和服务消费方来说,他们还有可能兼具这两种角色,即需要提供服务,有需要消费服务。 通过将服务统一管理起来,可以有效地优化内部应用对服务发布使用的流程和管理。服务注册中心可以通过特定协议来完成服务对外的统一

Dubbo 提供的注册中心有如下几种类型可供选:

Multicast 注册中心:组播方式

Redis 注册中心:使用 Redis 作为注册中心

Simple 注册中心:就是一个 dubbo 服务。作为注册中心。提供查找服务的功能。

Zookeeper 注册中心:使用 Zookeeper 作为注册中心

Dubbo推荐使用 Zookeeper 注册中心

Zookeeper是Apache下一个高性能的,分布式的,开放源码的分布式应用程序协调服务。简称 zk

ZooKeeper主要服务于分布式系统可以用ZooKeeper来做:统一配置管理、统一命名服务、分布式锁、集群管理

ZooKeeper官网

Apache 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 配置文件

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_18_color_FFFFFF_t_70_g_se_x_16

配置文件主要参数如下:

  1. #ZK中的时间配置最小但域,其他时间配置以整数倍tickTime计算
  2. tickTime=2000
  3. #Leader允许Follower启动时在initLimit时间内完成数据同步,单位:tickTime
  4. initLimit=10
  5. #Leader发送心跳包给集群中所有Follower,若Follower在syncLimit时间内没有响应,那么Leader就认为该follower已经挂掉了,单位:tickTime
  6. syncLimit=5
  7. #配置ZK的数据目录
  8. dataDir=/usr/local/zookeeper/data
  9. #用于接收客户端请求的端口号
  10. clientPort=2181
  11. #配置ZK的日志目录
  12. dataLogDir=/usr/local/zookeeper/logs
  13. #ZK集群节点配置,端口号2888用于集群节点之间数据通信,端口号3888用于集群中Leader选举
  14. server.1=192.168.123.100:2888:3888
  15. server.2=192.168.123.101:2888:3888

进入到 bin 目录下,点击 zkServer.cmd

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_16_color_FFFFFF_t_70_g_se_x_16

或者 在 bin 目录下 输入 “cmd”命令

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_17_color_FFFFFF_t_70_g_se_x_16

看到绑定成功即可

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_20_color_FFFFFF_t_70_g_se_x_16

使用ZooKeeper示例

1、创建maven Java模块

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_8_color_FFFFFF_t_70_g_se_x_16

IDEA下执行install命令,打成jar包

2、创建 maven web 服务提供者

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_8_color_FFFFFF_t_70_g_se_x_16 1

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.mycompany</groupId>
  6. <artifactId>dubbo-3-zk-userservice-provider</artifactId>
  7. <version>1.0.0</version>
  8. <packaging>war</packaging>
  9. <dependencies>
  10. <!--Spring依赖-->
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-context</artifactId>
  14. <version>5.1.4.RELEASE</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-webmvc</artifactId>
  19. <version>5.1.4.RELEASE</version>
  20. </dependency>
  21. <!--dubbo依赖-->
  22. <dependency>
  23. <groupId>com.alibaba</groupId>
  24. <artifactId>dubbo</artifactId>
  25. <version>2.6.2</version>
  26. </dependency>
  27. <!--接口工程依赖-->
  28. <dependency>
  29. <groupId>com.mycompany.dubbo</groupId>
  30. <artifactId>dubbo-3-zk-interface</artifactId>
  31. <version>1.0.0</version>
  32. </dependency>
  33. <!-- zookeeper依赖 -->
  34. <dependency>
  35. <groupId>org.apache.curator</groupId>
  36. <artifactId>curator-framework</artifactId>
  37. <version>4.3.0</version>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <plugins>
  42. <!--JDK1.8编译插件-->
  43. <plugin>
  44. <artifactId>maven-compiler-plugin</artifactId>
  45. <version>3.1</version>
  46. <configuration>
  47. <source>1.8</source>
  48. <target>1.8</target>
  49. </configuration>
  50. </plugin>
  51. </plugins>
  52. </build>
  53. </project>

dubbo-zk-userservice-provider.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  4. 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">
  5. <!-- 服务提供者声明名称:必须保证服务名称的唯一性(dubbo内部使用的唯一标识符)-->
  6. <dubbo:application name="dubbo-3-zk-userservice-provider" />
  7. <!-- 访问服务协议的名称以及端口号,dubbo官方推荐使用dubbo协议,端口号默认20880
  8. name:指定协议名称
  9. port:指定协议的端口号(默认为20880)
  10. -->
  11. <dubbo:protocol name="dubbo" port="20880"/>
  12. <!-- 使用zookeeper注册中心
  13. 指定注册中心地址和端口号
  14. -->
  15. <!--使用本机中的 zookeeper注册中心-->
  16. <dubbo:registry address="zookeeper://localhost:2181" />
  17. <!--使用linux系统中的zookeeper服务-->
  18. <!-- <dubbo:registry address="zookeeper://192.168.133.128:2181"/>-->
  19. <!-- 暴露服务接口
  20. dubbo:service
  21. interface:暴露服务接口的全限定类名
  22. ref:接口引用的实现类在spring容器中的标识
  23. registry:如果不使用注册中心,值为:N/A
  24. -->
  25. <dubbo:service interface="com.mycompany.dubbo.service.UserService" ref="userService" />
  26. <!-- 将接口实现类加载到spring容器 -->
  27. <bean id="userService" class="com.mycompany.dubbo.service.impl.UserServiceImpl" />
  28. </beans>

如果有多个服务提供者

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  4. 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">
  5. <!-- 服务提供者声明名称:必须保证服务名称的唯一性(dubbo内部使用的唯一标识符)-->
  6. <dubbo:application name="dubbo-3-zk-userservice-provider" />
  7. <!-- 访问服务协议的名称以及端口号,dubbo官方推荐使用dubbo协议,端口号默认20880
  8. name:指定协议名称
  9. port:指定协议的端口号(默认为20880)
  10. -->
  11. <dubbo:protocol name="dubbo" port="20880"/>
  12. <!-- 使用zookeeper注册中心
  13. 指定注册中心地址和端口号
  14. -->
  15. <!--使用本机中的 zookeeper注册中心-->
  16. <dubbo:registry address="zookeeper://localhost:2181" />
  17. <!--使用linux系统中的zookeeper服务-->
  18. <!-- <dubbo:registry address="zookeeper://192.168.133.128:2181"/>-->
  19. <!-- 暴露服务接口
  20. dubbo:service
  21. interface:暴露服务接口的全限定类名
  22. ref:接口引用的实现类在spring容器中的标识
  23. registry:如果不使用注册中心,值为:N/A
  24. -->
  25. <dubbo:service interface="com.mycompany.dubbo.service.UserService" ref="userService" version="1.0.0" timeout="15000"/>
  26. <dubbo:service interface="com.mycompany.dubbo.service.UserService" ref="userService2" version="2.0.0" />
  27. <!-- 将接口实现类加载到spring容器 -->
  28. <bean id="userService" class="com.mycompany.dubbo.service.impl.UserServiceImpl" />
  29. <bean id="userService2" class="com.mycompany.dubbo.service.impl.UserServiceImpl2" />
  30. </beans>

web.xml

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:dubbo-zk-userservice-provider.xml</param-value>
  4. </context-param>
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  7. </listener>

3、创建maven web 消费者

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_8_color_FFFFFF_t_70_g_se_x_16 2

pom.xml

  1. <groupId>com.mycompany.dubbo</groupId>
  2. <artifactId>dubbo-3-zk-consumer</artifactId>
  3. <version>1.0.0</version>
  4. <packaging>war</packaging>
  5. <dependencies>
  6. <!--Spring依赖-->
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-context</artifactId>
  10. <version>5.1.4.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-webmvc</artifactId>
  15. <version>5.1.4.RELEASE</version>
  16. </dependency>
  17. <!--dubbo依赖-->
  18. <dependency>
  19. <groupId>com.alibaba</groupId>
  20. <artifactId>dubbo</artifactId>
  21. <version>2.6.2</version>
  22. </dependency>
  23. <!--接口工程依赖-->
  24. <dependency>
  25. <groupId>com.mycompany.dubbo</groupId>
  26. <artifactId>dubbo-3-zk-interface</artifactId>
  27. <version>1.0.0</version>
  28. </dependency>
  29. <!-- zookeeper依赖 -->
  30. <dependency>
  31. <groupId>org.apache.curator</groupId>
  32. <artifactId>curator-framework</artifactId>
  33. <version>4.3.0</version>
  34. </dependency>
  35. </dependencies>
  36. <build>
  37. <plugins>
  38. <!--JDK1.8编译插件-->
  39. <plugin>
  40. <artifactId>maven-compiler-plugin</artifactId>
  41. <version>3.1</version>
  42. <configuration>
  43. <source>1.8</source>
  44. <target>1.8</target>
  45. </configuration>
  46. </plugin>
  47. </plugins>
  48. </build>

UserController

  1. @Controller
  2. public class UserController {
  3. @Autowired
  4. private UserService userService;
  5. @RequestMapping(value = "/userDetail")
  6. public String userDetail(Model model,Integer id){
  7. //根据用户标识获取用户详情
  8. User user = userService.queryUserById(id);
  9. //获取用户总人数
  10. Integer allUserCount = userService.queryAllUserCount();
  11. model.addAttribute("user",user);
  12. model.addAttribute("allUserCount",allUserCount);
  13. return "userDetail";
  14. }
  15. }

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!--扫描组件-->
  13. <context:component-scan base-package="com.mycompany.dubbo.controller"/>
  14. <!--配置注解驱动-->
  15. <mvc:annotation-driven/>
  16. <!--视图解析器-->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="prefix" value="/"/>
  19. <property name="suffix" value=".jsp"/>
  20. </bean>
  21. </beans>

dubbo-zk-consumer.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  4. 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">
  5. <!--声明dubbo服务消费者名称:保证唯一性-->
  6. <dubbo:application name="dubbo-3-zk-consumer"/>
  7. <!--指定注册中心-->
  8. <dubbo:registry address="zookeeper://localhost:2181"/>
  9. <!--使用linux系统中的zookeeper服务-->
  10. <!-- <dubbo:registry address="zookeeper://192.168.133.128:2181"/>-->
  11. <!--引用远程接口服务-->
  12. <dubbo:reference id="userService" interface="com.mycompany.dubbo.service.UserService"/>
  13. </beans>

如果有多个消费者

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  4. 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">
  5. <!--声明dubbo服务消费者名称:保证唯一性-->
  6. <dubbo:application name="dubbo-4-zk-multi-consumer"/>
  7. <!--指定注册中心-->
  8. <dubbo:registry address="zookeeper://localhost:2181"/>
  9. <!--使用linux系统中的zookeeper服务-->
  10. <!-- <dubbo:registry address="zookeeper://192.168.133.128:2181"/>-->
  11. <!--引用远程接口服务-->
  12. <dubbo:reference id="userService" interface="com.mycompany.dubbo.service.UserService" version="1.0.0" />
  13. <dubbo:reference id="userService2" interface="com.mycompany.dubbo.service.UserService" version="2.0.0" />
  14. </beans>

web.xml

  1. <servlet>
  2. <servlet-name>dispatcherServlet</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:applicationContext.xml,classpath:dubbo-zk-consumer.xml</param-value>
  7. </init-param>
  8. </servlet>
  9. <servlet-mapping>
  10. <servlet-name>dispatcherServlet</servlet-name>
  11. <url-pattern>/</url-pattern>
  12. </servlet-mapping>

配置Tomcat访问

643beb5e681f434d9f6b464dafda6d6e.png

二、监控中心

dubbo 的使用,其实只需要有注册中心,消费者,提供者这三个就可以使用了,但是并不能看到有哪些消费者和提供者,为了更好的调试,发现问题,解决问题,因此引入dubbo-admin

通过 dubbo-admin 可以对消费者和提供者进行管理。可以在 dubbo 应用部署做动态的调整,服务的管理。

1、下载监控中心,https://github.com/alibaba/dubbo

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_20_color_FFFFFF_t_70_g_se_x_16 1

2、通过IDEA将其打包war

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_20_color_FFFFFF_t_70_g_se_x_16 2

3、把 dubbo-admin.war 文件拷贝到 tomcat 的 webapps

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_13_color_FFFFFF_t_70_g_se_x_16

4、通过压缩工具打开dubbo-admin-2.5.10.war包 应用的 WEB-INF/dubbo-properties 文件,内容如下

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_12_color_FFFFFF_t_70_g_se_x_16

5、在浏览器地址栏输入 http://localhost:8080/dubbo-admin ;访问监控中心-控制台

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBATWluZ2dlUWluZ2NodW4_size_20_color_FFFFFF_t_70_g_se_x_16 3

发表评论

表情:
评论列表 (有 0 条评论,79人围观)

还没有评论,来说两句吧...

相关阅读

    相关 注册中心zookeeper

    概念 Zookeeper是一个分布式、可靠的、可扩展的协调服务,为分布式应用提供一致性服务。它是树型结构,能做到集群管理数据,当提供者出现断电等异常停机时,zk能自动删除