zoukankan      html  css  js  c++  java
  • 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

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

  • 相关阅读:
    #2051:Bitset(进制转化)
    #2054:A == B ?(水题坑人)
    #2045:不容易系列之三LELE的RPG难题(dp递推)
    #2037:今年暑假不AC
    #2036:改革春风吹满地
    OJ中的语言选项里G++ 与 C++的区别
    如何在CSDN上如何快速转载博客
    Python之路(第八篇)Python内置函数、zip()、max()、min()
    Python编程笔记(第一篇)Python基础语法
    Python之路(第七篇)Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数
  • 原文地址:https://www.cnblogs.com/asenyang/p/15506548.html
Copyright © 2011-2022 走看看