phpcms网站title营销网店推广的软文
1.Dubbo中的版本号
每个接口都应定义版本号,为后续不兼容升级提供可能。当一个接口有不同的实现,项目早期使用的一个实现类, 之后创建接口的新的实现类。区分不同的接口实现使用 version。
特别是项目需要把早期接口的实现全部换位新的实现类,也需要使用 version。
可以用版本号从早期的接口实现过渡到新的接口实现,版本号不同的服务相互间不引用。
可以按照以下的步骤进行版本迁移:
- 在低压力时间段,先升级一半提供者为新版本
- 再将所有消费者升级为新版本
- 然后将剩下的一半提供者升级为新版本
2.案例分析
最近两天一直都在学习Dubbo,说来说去,那开始依旧是三个工程(第一个是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工程中服务接口方法的实现。但是我们这里为服务接口提供两个实现类,来体现对版本号version的使用。
package com.szh.dubbo.service.impl;import com.szh.dubbo.model.User;
import com.szh.dubbo.service.UserService;/****/
public class UserServiceImpl implements UserService {@Overridepublic User queryUserById(Integer id, String username) {User user=new User();user.setId(id);user.setUsername(username + "-1");return user;}
}
package com.szh.dubbo.service.impl;import com.szh.dubbo.model.User;
import com.szh.dubbo.service.UserService;/****/
public class UserServiceImpl2 implements UserService {@Overridepublic User queryUserById(Integer id, String username) {User user=new User();user.setId(id);user.setUsername(username + "-2");return user;}
}
然后是dubbo服务提供者的配置文件。这里仍然使用zookeeper注册中心,将服务接口的两个实现类加载到spring容器中,最后在web.xml中配置spring的监听器,同时读取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://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="009-zk-userservice-multi-provider"/><dubbo:protocol name="dubbo" port="20880"/><dubbo:registry address="zookeeper://localhost:2181"/><dubbo:service interface="com.szh.dubbo.service.UserService" ref="userServiceImpl" version="1.0.0"/><dubbo:service interface="com.szh.dubbo.service.UserService" ref="userServiceImpl2" version="2.0.0"/><bean id="userServiceImpl" class="com.szh.dubbo.service.impl.UserServiceImpl"/><bean id="userServiceImpl2" class="com.szh.dubbo.service.impl.UserServiceImpl2"/></beans>
<?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"><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo-userservice-multi-provider.xml</param-value></context-param></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 {@Autowiredprivate UserService userService1;@Autowiredprivate UserService userService2;@RequestMapping(value = "/userDetail")public String userDetail(Model model,Integer id,String username) {User user1=userService1.queryUserById(id,username);User user2=userService2.queryUserById(id,username);model.addAttribute("user1",user1);model.addAttribute("user2",user2);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"xmlns:dubo="http://code.alibabatech.com/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 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><dubbo:application name="010-zk-multi-consumer"/><dubo:registry address="zookeeper://localhost:2181"/><dubbo:reference id="userService1" interface="com.szh.dubbo.service.UserService" version="1.0.0"/><dubbo:reference id="userService2" interface="com.szh.dubbo.service.UserService" version="2.0.0"/></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"/><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-multi-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>用户1的信息</h3><div>用户编号:${user1.id}</div><div>用户姓名:${user1.username}</div><hr/><h3>用户2的信息</h3><div>用户编号:${user2.id}</div><div>用户姓名:${user2.username}</div>
</body>
</html>
2.4 启动测试!!!
步骤在上一篇博文中已经说过了,链接:Dubbo——使用Zookeeper注册中心实现Dubbo_zookeeper中的dubbo-CSDN博客
下面是测试结果: