dubbo使用zookeeper版本号案例
1.为什么需要版本号?
因为一个接口可能有多个实现类,如果我们不用版本号来进行区分,dubbo根本就不知道我们要调用哪一个接口实现类的方法!
2.接口工程
1.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.yl</groupId>
<artifactId>zk-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>zk-interface Maven Webapp</name>
</project>
2.model
package com.yl.model;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.接口
package com.yl.service;
import com.yl.model.User;
public interface UserService {
User getUserById(Integer id);
}
3.提供者
1.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.yl</groupId>
<artifactId>zk-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>zk-provider Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<dependencies>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--注册中心依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<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>
<!--接口工程-->
<dependency>
<groupId>com.yl</groupId>
<artifactId>zk-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.接口实现类
package com.yl.service.impl;
import com.yl.model.User;
import com.yl.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public User getUserById(Integer id) {
User user = new User();
user.setId(id);
user.setUsername("admin");
user.setPassword("123456");
return user;
}
}
package com.yl.service.impl;
import com.yl.model.User;
import com.yl.service.UserService;
public class EmployeeServiceImpl implements UserService {
@Override
public User getUserById(Integer id) {
User user = new User();
user.setId(id);
user.setUsername("employee-username:tom");
user.setPassword("employee-password:123456789");
return user;
}
}
3.dubbo的配置
<?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"
xmlns:dubbbo="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="zk-provider"/>
<!--指定协议以及端口号-->
<dubbbo:protocol name="dubbo" port="20880"/>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--暴露服务-->
<dubbo:service interface="com.yl.service.UserService" ref="userServiceImpl" version="0.0.1" timeout="50000"/>
<dubbo:service interface="com.yl.service.UserService" ref="employeeServiceImpl" version="0.0.2" timeout="50000"/>
<!--加载实现类-->
<bean id="userServiceImpl" class="com.yl.service.impl.UserServiceImpl"/>
<bean id="employeeServiceImpl" class="com.yl.service.impl.EmployeeServiceImpl"/>
</beans>
4.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">
<!--spring的监听器配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
4.消费者
1.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.yl</groupId>
<artifactId>zk-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>zk-consumer Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<dependencies>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--注册中心依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<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>
<!--接口工程-->
<dependency>
<groupId>com.yl</groupId>
<artifactId>zk-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.controller
package com.yl.controller;
import com.yl.model.User;
import com.yl.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.GetMapping;
@Controller
public class UserController {
@Autowired
UserService userService;
@Autowired
UserService employeeService;
@GetMapping("/getUserById")
public String getUserById(Model model,Integer id) {
User user = userService.getUserById(id);
model.addAttribute("user",user);
User employee = employeeService.getUserById(id);
model.addAttribute("employee",employee);
return "userInfo";
}
}
3.dubbo的配置
<?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="zk-consumer"/>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--引用远程服务接口-->
<dubbo:reference id="userService" interface="com.yl.service.UserService" version="0.0.1"/>
<dubbo:reference id="employeeService" interface="com.yl.service.UserService" version="0.0.2"/>
</beans>
4.springmvc的配置
<?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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.yl.controller" />
<!--注解驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5.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-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>
6.userInfo.jsp
<%--
Created by IntelliJ IDEA.
User: mirror
Date: 2022/5/1
Time: 22:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>用户</h2>
<table>
<tr>
<td>用户id</td>
<td>${user.id}</td>
</tr>
<tr>
<td>用户名</td>
<td>${user.username}</td>
</tr>
<tr>
<td>密码</td>
<td>${user.password}</td>
</tr>
</table>
<h2>员工</h2>
<table>
<tr>
<td>员工id</td>
<td>${employee.id}</td>
</tr>
<tr>
<td>员工名</td>
<td>${employee.username}</td>
</tr>
<tr>
<td>员工密码</td>
<td>${employee.password}</td>
</tr>
</table>
</body>
</html>
5.测试
1.启动zookeeper
2.启动提供者和消费者
3.测试接口