1. 程式人生 > >Dubbo、Spring、Zookeeper、整合基礎案例(多版本相容釋出)

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

二:專案結構

三:服務提供者、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"/>

    <!--&lt;!&ndash;宣告需要暴露的服務介面&ndash;&gt;
    <dubbo:service interface="com.micai.dubbo.provider.DemoService" ref="demoService"/>
    &lt;!&ndash;和本地bean一樣實現服務&ndash;&gt;
    <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"/>

    <!--&lt;!&ndash;宣告需要暴露的服務介面&ndash;&gt;
    <dubbo:service interface="com.micai.dubbo.provider.DemoService" ref="demoService"/>
    &lt;!&ndash;和本地bean一樣實現服務&ndash;&gt;
    <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"/>

    <!--&lt;!&ndash;生成遠端服務代理,可以和本地bean一樣使用demoService&ndash;&gt;
    <dubbo:reference id="demoService" interface="com.micai.dubbo.provider.DemoService"/>-->

    <!--掃描註解包路徑,多個包⽤逗號分隔,不填pacakge表示掃描當前ApplicationContext中所有的類-->
    <dubbo:annotation package="com.micai.dubbo.consumer"/>

</beans>

五:執行結果

六:下載原始碼請加群下載

QQ交流群