2019-04-11 | Dubbo | UNLOCK

Dubbo服务多版本-案例

Dubbo服务多版本

当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。

场景

可以按照以下的步骤进行版本迁移:

  1. 在低压力时间段,先升级一半提供者为新版本
  2. 再将所有消费者升级为新版本
  3. 然后将剩下的一半提供者升级为新版本

也可以配合蓝绿部署,实现更细致的接口版本

代码:

话不多说看代码

0.1 版本接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.service.IOrderService;
import org.springframework.stereotype.Component;

@Service(interfaceClass = IOrderService.class,version = "0.1")
@Component
public class OrderServiceImplA implements IOrderService {
@Override
public String orderList(String message) {
return "测试2017";
}
}

0.2 版本接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.service.IOrderService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = IOrderService.class,version = "0.2")
@Component
public class OrderServiceImplB implements IOrderService {

@Override
public String orderList(String message) {
return "测试2018";
}
}

服务调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.dubbo.consumer.quickstart;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.service.IOrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QuickstartConsumer {
@Reference(interfaceClass = IOrderService.class,timeout = 10000,version = "0.1")
private IOrderService orderService;

@GetMapping(value = "test")
public void test(){
String temp = orderService.orderList("休息下");
System.out.println(temp);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2019-04-09 23:33:12.874  INFO 16300 --- [clientConnector] org.apache.zookeeper.ZooKeeper           : Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.I0Itec.zkclient.ZkClient@6b2e991f
2019-04-09 23:33:12.894 INFO 16300 --- [clientConnector] org.I0Itec.zkclient.ZkClient : Waiting for keeper state SyncConnected
2019-04-09 23:33:12.898 INFO 16300 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2019-04-09 23:33:12.899 INFO 16300 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2019-04-09 23:33:12.913 INFO 16300 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x16a026a0377000a, negotiated timeout = 30000
2019-04-09 23:33:12.914 INFO 16300 --- [tor-EventThread] org.I0Itec.zkclient.ZkClient : zookeeper state changed (SyncConnected)
2019-04-09 23:33:13.449 INFO 16300 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-09 23:33:13.792 INFO 16300 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-04-09 23:33:13.867 INFO 16300 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 80 (http) with context path ''
2019-04-09 23:33:13.871 INFO 16300 --- [ main] com.dubbo.ConsumerApp : Started ConsumerApp in 5.537 seconds (JVM running for 5.965)
2019-04-09 23:33:20.098 INFO 16300 --- [p-nio-80-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-09 23:33:20.098 INFO 16300 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-04-09 23:33:20.118 INFO 16300 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 20 ms
测试2017

评论加载中