添加dubbo服务调用示例

This commit is contained in:
tanghc
2019-06-03 18:28:30 +08:00
parent fc2154567a
commit 748aa51aec
3 changed files with 14 additions and 26 deletions

View File

@@ -2,48 +2,38 @@ package com.gitee.sop.bookweb.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import com.gitee.sop.story.api.domain.Story;
import com.gitee.sop.story.api.param.DemoParam;
import com.gitee.sop.story.api.result.DemoResult;
import com.gitee.sop.story.api.service.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 调用dubbo服务provider是story见DefaultDemoService.java
* dubbo配置方式参见https://github.com/apache/dubbo-spring-boot-project/blob/0.2.x/README_CN.md
*
* <p>
* 对比SpringCloud提供的Feigndubbo会方便很多
*
* <p>
* Feign的使用方式参见com.gitee.sop.bookweb.controller.AlipayBookController#getBook2()
*/
@RestController
public class DemoConsumerController {
public class DubboConsumerController {
@Reference(version = "${demo.service.version}",
application = "${dubbo.application.id}",
url = "dubbo://localhost:12345")
private DemoService demoService;
// http://localhost:3333/sayHello?name=jim
@RequestMapping("/sayHello")
public String sayHello(@RequestParam String name) {
return demoService.sayHello(name);
}
// http://localhost:3333/dubboStory
@RequestMapping("/dubboStory")
public DemoResult dubboStory() {
DemoParam demoParam = new DemoParam();
demoParam.setId(1);
return demoService.getStory(demoParam);
}
// 作为开放接口
@ApiMapping(value = "dubbo.story.get")
public DemoResult openApi(DemoParam demoParam) {
public Story openApi(DemoParam demoParam) {
// 通过dubbo调用story提供的服务
return demoService.getStory(demoParam);
DemoResult dubboStory = demoService.getStory(demoParam);
Story story = new Story();
story.setId(dubboStory.getId());
story.setName(dubboStory.getName());
return story;
}
}

View File

@@ -22,7 +22,7 @@ public class DefaultDemoService implements DemoService {
@Override
public DemoResult getStory(DemoParam param) {
DemoResult demoResult = new DemoResult();
demoResult.setId(2);
demoResult.setId(param.getId());
demoResult.setName("dubbo 白雪公主, param=" + param);
return demoResult;
}