0
点赞
收藏
分享

微信扫一扫

Dubbo、Spring、Zookeeper、集成基础案例(多版本兼容发布)


摘要:最近抽时间系统的学习了Dubbo的一些内容,趁有时间,整理下,顺便记录下,以防以后回顾。前面我们学校了Dubbo的参数回调,本次我们学习下Dubbo的多版本兼容发布、当一个接口实现、,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。可以按照以下的步骤进行版本迁移:

1>.在低压力时间段,先升级一半提供者为新版本

2>.再将所有消费者升级为新版本

3>.然后将剩下的一半提供者升级为新版本

一:运行环境

1>:JDK 1.8

2>:IDEA 2018.1

3>:Zookeeper 3.x

4>:Maven 3.2

5>:Dubbo 2.8.4

二:项目结构

Dubbo、Spring、Zookeeper、集成基础案例(多版本兼容发布)_spring

三:服务提供者、pom.xml和上一篇一样,这里就不再贴出来了


VersionService.java


package com.micai.dubbo.provider;

/**
* @Auther: zhaoxinguo
* @Date: 2018/9/13 17:44
* @Description:
*/
public interface VersionService {

String sayHello(String name);
}


VersionServiceImpl.java


package com.micai.dubbo.provider;

import com.alibaba.dubbo.config.annotation.Service;

/**
* @Auther: zhaoxinguo
* @Date: 2018/9/13 17:44
* @Description:
*/
@Service(version = "1.0.0")
public class VersionServiceImpl implements VersionService {

@Override
public String sayHello(String name) {
return "hello, " + name;
}
}


VersionServiceImpl2.java


package com.micai.dubbo.provider;

import com.alibaba.dubbo.config.annotation.Service;

/**
* @Auther: zhaoxinguo
* @Date: 2018/9/13 17:45
* @Description:
*/
@Service(version = "2.0.0")
public class VersionServiceImpl2 implements VersionService {

@Override
public String sayHello(String name) {
return "hello2, " + name;
}
}


Provider.java


package com.micai.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
* @Auther: zhaoxinguo
* @Date: 2018/9/10 14:13
* @Description:
*/
public class Provider {

public static void main(String [] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
classPathXmlApplicationContext.start();
try {
System.in.read();//按任意键退出
} catch (IOException e) {
e.printStackTrace();
}
}
}


Provider2.java


package com.micai.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
* @Auther: zhaoxinguo
* @Date: 2018/9/10 14:13
* @Description:
*/
public class Provider2 {

public static void main(String [] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
classPathXmlApplicationContext.start();
try {
System.in.read();//按任意键退出
} catch (IOException e) {
e.printStackTrace();
}
}
}

applicationContext.xml

<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-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--提供方应用信息,用于计算依赖关系-->
<dubbo:application name="version-provider"/>

<!--使⽤zookeeper注册中⼼暴露服务地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

<!--用dubbo协议在20880端口暴露服务-->
<dubbo:protocol name="dubbo" port="20880"/>

<!--<!–声明需要暴露的服务接口–>
<dubbo:service interface="com.micai.dubbo.provider.DemoService" ref="demoService"/>
<!–和本地bean一样实现服务–>
<bean id="demoService" class="com.micai.dubbo.provider.DemoServiceImpl"/>-->

<!--扫描注解包路径,多个包⽤逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类-->
<dubbo:annotation package="com.micai.dubbo.provider"/>

</beans>

applicationContext2.xml

<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-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--提供方应用信息,用于计算依赖关系-->
<dubbo:application name="version-provider2"/>

<!--使⽤zookeeper注册中⼼暴露服务地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

<!--用dubbo协议在20882端口暴露服务-->
<dubbo:protocol name="dubbo" port="20882"/>

<!--<!–声明需要暴露的服务接口–>
<dubbo:service interface="com.micai.dubbo.provider.DemoService" ref="demoService"/>
<!–和本地bean一样实现服务–>
<bean id="demoService" class="com.micai.dubbo.provider.DemoServiceImpl"/>-->

<!--扫描注解包路径,多个包⽤逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类-->
<dubbo:annotation package="com.micai.dubbo.provider"/>

</beans>

四:服务消费者、pom.xml和上一篇一样,这里就不再贴出来了


VersionAction.java


package com.micai.dubbo.consumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.micai.dubbo.provider.VersionService;
import org.springframework.stereotype.Component;

/**
* @Auther: zhaoxinguo
* @Date: 2018/9/13 17:54
* @Description:
*/
@Component
public class VersionAction {

@Reference(version = "1.0.0")
private VersionService versionService;

@Reference(version = "2.0.0")
private VersionService versionService2;

public String sayHello() {
String result = versionService.sayHello("world");
return result;
}

public String sayHello2() {
String result = versionService2.sayHello("world");
return result;
}

}


Consumer.java


package com.micai.dubbo;

import com.micai.dubbo.consumer.VersionAction;
import com.micai.dubbo.provider.VersionService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
* @Auther: zhaoxinguo
* @Date: 2018/9/11 14:36
* @Description:
*/
public class Consumer {

public static void main(String [] args) {
try {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
classPathXmlApplicationContext.start();
VersionAction versionAction = (VersionAction) classPathXmlApplicationContext.getBean("versionAction");
String result = versionAction.sayHello();
System.out.println("版本1调用返回结果:" + result);
Thread.sleep(2000);
String result2 = versionAction.sayHello2();
System.out.println("版本2调用返回结果:" + result2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

applicationContext.xml

<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-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--消费方应用名、用于计算依赖关系,不是匹配条件,不要与提供方一样-->
<dubbo:application name="version-consumer"/>

<!--使用zookeeper注册中心暴露发现服务地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

<!--<!–生成远程服务代理,可以和本地bean一样使用demoService–>
<dubbo:reference id="demoService" interface="com.micai.dubbo.provider.DemoService"/>-->

<!--扫描注解包路径,多个包⽤逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类-->
<dubbo:annotation package="com.micai.dubbo.consumer"/>

</beans>

五:运行结果

Dubbo、Spring、Zookeeper、集成基础案例(多版本兼容发布)_spring_02


举报

相关推荐

0 条评论