-
借助zookeeper注册中心进一步改正 直连式 + 接口工程的不足,更好的管理服务者提供的功能以及消费者对服务的申请访问
-
需要用到3个相互独立的maven工程,工程1为maven的java工程作为接口工程,工程2,3为maven的web工程
工程1:o8-zoo-interface 作为接口工程 工程2:o9-zoo-userservice-mutil-provider 作为服务的提供者 工程3:olo-zoo-mutil-consumer 作为使用服务的消费者
简单配置zookeeper
-
首先在win10中配置JAVA_HOM,因为zookeeper是基于java的,需要java运行环境
-
下载apache的zookeeper,解压后的文件夹内容如下,自己新增data文件夹
-
进入conf目录:复制zoo_sample.cfg文件并改名为zoo.cfg
-
编辑zoo.cfg:暂时只要修改两处,1.dataDir的路径改成自己新建的data目录的路径,2.在clientPort下新增admin.serverPort=8888
-
进入bin目录,双击运行zkService.cmd
-
运行结果如下,表示JAVA_HOME以及zookeeper的配置暂时没有问题:此时2181和8888端口都正常工作
工程1
-
结构
-
pom文件
<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.example.dubbo</groupId> <artifactId>o8-zoo-interface</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <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>
-
实体类:注意要实现序列化接口,数据需要通过socket网络传输
package com.example.dubbo.model; import java.io.Serializable; public class User implements Serializable { private String id; private String name; private String age; @Override public String toString() { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age='" + age + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public User(String id, String name, String age) { this.id = id; this.name = name; this.age = age; } public User() { } }
-
服务接口:
package com.example.dubbo.service; import com.example.dubbo.model.User; public interface UserService { /** * 根据用户id,获取用户信息 */ User queryUserById(String id); }
工程2
-
结构
-
pom文件:需要新增对zookeeper注册中心的依赖
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.dubbo</groupId> <artifactId>o9-zoo-userservice-mutil-provider</artifactId> <packaging>war</packaging> <version>1.0.0</version> <dependencies> <!-- Spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.16.RELEASE</version> </dependency> <!-- dubbo依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 接口工程 --> <dependency> <groupId>com.example.dubbo</groupId> <artifactId>o8-zoo-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> <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>
-
提供的服务实现1
package com.example.dubbo.service.impl; import com.example.dubbo.model.User; import com.example.dubbo.service.UserService; public class UserService1 implements UserService { @Override public User queryUserById(String id) { User user = new User(); user.setId(id); user.setName("橘子 --- 1"); user.setAge("18 --- 1"); return user; } }
-
提供的服务实现2
package com.example.dubbo.service.impl; import com.example.dubbo.model.User; import com.example.dubbo.service.UserService; public class UserService2 implements UserService { @Override public User queryUserById(String id) { User user = new User(); user.setId(id); user.setName("饺子 --- 2"); user.setAge("21 --- 2"); return user; } }
-
dubbo的服务提供者的配置文件:dubbo-zoo-userservice-mutil-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:application name="o9-zoo-userservice-mutil-provider"/> <!-- 服务基于的协议以及端口 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 注册中心--> <!--使用本机中的zookeeper服务--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 提供的服务 --> <dubbo:service ref="userService1" interface="com.example.dubbo.service.UserService" version="0.0.1"/> <dubbo:service ref="userService2" interface="com.example.dubbo.service.UserService" version="0.0.2"/> <!-- 服务的实现类--> <bean id="userService1" class="com.example.dubbo.service.impl.UserService1"/> <bean id="userService2" class="com.example.dubbo.service.impl.UserService2"/> </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-zoo-userservice-mutil-provider.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
工程3
-
结构
-
pom文件:需要新增对zookeeper注册中心的依赖
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.dubbo</groupId> <artifactId>olo-zoo-mutil-consumer</artifactId> <packaging>war</packaging> <version>1.0.0</version> <dependencies> <!--Spring依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.16.RELEASE</version> </dependency> <!--dubbo依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 接口工程 --> <dependency> <groupId>com.example.dubbo</groupId> <artifactId>o8-zoo-interface</artifactId> <version>1.0.0</version> </dependency> <!--注册中心依赖--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.1.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里消费者的配置文件:dubbo-zoo-mutil-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:application name="olo-zoo-mutil-consumer"/> <!-- 引用的远程服务 --> <dubbo:reference id="userService1" interface="com.example.dubbo.service.UserService" version="0.0.1"/> <dubbo:reference id="userService2" interface="com.example.dubbo.service.UserService" version="0.0.2"/> <!-- 注册中心--> <!--使用本机中的zookeeper服务--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> </beans>
-
spring核心配置文件:application.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.example.dubbo.web"/> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
-
Controller层
package com.example.dubbo.web; import com.example.dubbo.model.User; import com.example.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 UserService userService1; @Autowired UserService userService2; /** * 响应前端请求,返回用户的详细信息 */ @RequestMapping("/getUserDetail.do") public String getUserDetail(String id, Model model){ //获取数据 User user1 = userService1.queryUserById(id); User user2 = userService2.queryUserById(id); //存储数据 model.addAttribute("user1", user1); model.addAttribute("user2", user2); //跳转到响应页面 return "userDetail"; } }
-
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:application.xml classpath:dubbo-zoo-mutil-consumer.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
-
返回给前端的响应页面:userDetail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户详情页面</title> </head> <body> <h3>用户1</h3> <div>用户标识:${user1.id}</div> <div>用户名称:${user1.name}</div> <div>用户年龄:${user1.age}</div> <h3>用户2</h3> <div>用户标识:${user2.id}</div> <div>用户名称:${user2.name}</div> <div>用户年龄:${user2.age}</div> </body> </html>
测试
-
启动顺序:先启动本机的zookeeper服务
-
接着将服务提供者工程和消费者工程部署到tomcat上并运行
-
运行结果
分析
-
在引入了zookeeper注册中心后,可以对要提供的服务进行注册登记,很好的统计和管理可提供的服务以及消费者对服务的申请访问情况
-
例如上面所演示的:当一个服务接口有多个实现类,也即一个服务接口可以对外提供多个服务时,可以通过服务的版本号,清晰合理的管理和提供用户所需的服务