1.写在前面
有关Zookeeper注册中心的相关内容,可以参考我的这篇博客:
https://blog.csdn.net/weixin_43823808/article/details/117339044
2.案例分析
这里使用注册中心仍然需要三个maven工程,第一个是maven java工程,后两个是maven web工程,创建步骤不再多说了。
2.1 第一个是maven java工程
这其中提供的是服务模型(实体Bean)、服务接口(对外提供的方法),这个工程不需要添加任何依赖。
package com.szh.dubbo.model; import java.io.Serializable; /** * */ public class User implements Serializable { private Integer id; private String username; //getter and setter }
package com.szh.dubbo.service; import com.szh.dubbo.model.User; /** * */ public interface UserService { User queryUserById(Integer id,String username); }
2.2 第二个是maven web工程
这个代表的是服务提供者,其中包含对第一个maven java工程中服务接口方法的实现。
package com.szh.dubbo.service.impl; import com.szh.dubbo.model.User; import com.szh.dubbo.service.UserService; /** * */ public class UserServiceImpl implements UserService { @Override public User queryUserById(Integer id, String username) { User user=new User(); user.setId(id); user.setUsername(username); return user; } }
dubbo服务提供者的配置文件。之前我们使用的直连方式实现dubbo,所以地址端口号是 dubbo://localhost:20880。
现在使用了zookeeper注册中心,这个地址和端口号要做修改:zookeeper://localhost:2181。
<?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="007-zk-userservice-provider"/> <!-- 声明dubbo使用的协议和端口号 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 使用zookeeper注册中心,指定注册中心的地址、端口号 --> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 暴露接口服务 --> <dubbo:service interface="com.szh.dubbo.service.UserService" ref="userServiceImpl"/> <!-- 加载接口实现类 --> <bean id="userServiceImpl" class="com.szh.dubbo.service.impl.UserServiceImpl"/> </beans>
由于在服务提供者中,需要加载之前服务接口的实现类,所以这里需要用到Spring的监听器,同时读取dubbo配置文件。
<?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-userservice-provider.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
pom文件中的相关依赖。
<!-- Spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- SpringMVC依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- Dubbo依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 接口工程依赖 --> <dependency> <groupId>com.szh.dubbo</groupId> <artifactId>006-zk-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>
2.3 第三个是maven web工程
这个代表的是服务消费者,其中包含一个控制层方法的实现,去响应之前的服务接口。
package com.szh.dubbo.controller; import com.szh.dubbo.model.User; import com.szh.dubbo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * */ @Controller public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/userDetail") public String userDetail(Integer id, String username, Model model) { User user=userService.queryUserById(id,username); model.addAttribute("user",user); return "userDetail"; } }
然后是dubbo服务消费者的配置文件、Spring配置文件。
<?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="008-zk-consumer"/> <!-- 指定注册中心 --> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 引用远程接口服务 --> <dubbo:reference id="userService" interface="com.szh.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: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.szh.dubbo.controller"/> <!-- 开启springmvc注解驱动 --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
最后是web.xml和控制层方法对应的jsp页面。
<?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: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> </web-app>
<%@ page contentType="text/html;charset=utf-8" language="java" %> <html> <head> <title>$</title> </head> <body> <h3>用户详情</h3> <div>用户编号:${user.id}</div> <div>用户姓名:${user.username}</div> </body> </html>
2.4 启动测试!!!
为后两个maven web工程配置各自的tomcat服务器(配置过程在这里就不多说了。。。)。
由于之前使用的是直连方式实现dubbo,所以配置完tomcat,先启动dubbo服务提供者的tomcat,再启动dubbo服务消费者的tomcat,直接去浏览器输入url就可以访问了。
现在使用了zookeepr注册中心,所以配置完tomcat之后,我们需要到zookeeper的根目录的bin目录下,找到zkServer.cmd先启动注册中心,然后启动dubbo服务提供者的tomcat,再启动dubbo服务消费者的tomcat,再到浏览器输入url就可以访问了。




