Dubbo常用功能01--version版本

version版本:

version版本号用处是对于同一接口,具有不同的服务实现。

1、服务端代码:

 1 package com.yas.serviceprovider.impl;
 2 
 3 import com.yas.api.SiteService;
 4 import org.apache.dubbo.config.annotation.Service;
 5 
 6 @Service(version = "default")
 7 public class SiteServiceImpl implements SiteService {
 8     @Override
 9     public String getName(String name) {
10         return "default:" + name;
11     }
12 }
 1 package com.yas.serviceprovider.impl;
 2 
 3 import com.yas.api.SiteService;
 4 import org.apache.dubbo.config.annotation.Service;
 5 
 6 @Service(version = "async")
 7 public class AsyncSiteServiceImpl implements SiteService {
 8     @Override
 9     public String getName(String name) {
10         return "async:" + name;
11     }
12 }

启动服务端,可以在监控管理后台看到两个服务:

2、客户端代码:

 1 package com.example.serviceconsumer.controller;
 2 
 3 import com.yas.api.SiteService;
 4 import org.apache.dubbo.config.annotation.Reference;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestParam;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @RestController
10 public class SiteController {
11 
12     @Reference(version = "default")
13     SiteService siteService;
14 
15     @RequestMapping("/default")
16     public String getName(@RequestParam("name") String name){
17         return siteService.getName(name);
18     }
19 
20     @Reference(version = "async")
21     SiteService asyncSiteService;
22 
23     @RequestMapping("/async")
24     public String getNameByAsync(@RequestParam("name") String name){
25         return asyncSiteService.getName(name);
26     }
27 }

客户端提供了两个Action,分别调用标注了default的服务和async的服务。

3、测试:

使用postman请求:http://localhost:8000/default?name=zhangsan

得到响应为:default:zhangsan

使用postman请求:http://localhost:8000/async?name=zhangsan

得到响应为:async:zhangsan

实现了从客户端向服务端有指向性的调用。

原文地址:https://www.cnblogs.com/asenyang/p/15506548.html