首页 >> 知识 >> Dubbo

Dubbo

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工程中服务接口方法的实现。但是草莓视频在线观看APP这里为服务接口提供两个实现类,来体现对版本号version的使用。

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 + "-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 { @Override public 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配置文件。

org.springframework.web.context.ContextLoaderListener contextConfiglocations classpath:dubbo-userservice-multi-provider.xml

pom文件中的相关依赖。

org.springframework spring-context 5.2.5.RELEASE org.springframework spring-webmvc 5.2.5.RELEASE com.alibaba dubbo 2.6.2 com.szh.dubbo 006-zk-interface 1.0.0 org.apache.curator curator-framework 4.1.0 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;/** * */@Controllerpublic class UserController { @Autowired private UserService userService1; @Autowired private 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配置文件。

最后是web.xml和控制层方法对应的jsp页面。

DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfiglocations classpath:applicationContext.xml,classpath:dubbo-multi-consumer.xml DispatcherServlet / $ 用户1的信息 用户编号:${user1.id} 用户姓名:${user1.username} 用户2的信息 用户编号:${user2.id} 用户姓名:${user2.username} 2.4 启动测试!!!

步骤在

网站地图