添加dubbo服务调用示例

1.x
tanghc 5 years ago
parent fc2154567a
commit 748aa51aec
  1. 32
      sop-example/sop-book/sop-book-web/src/main/java/com/gitee/sop/bookweb/controller/DubboConsumerController.java
  2. 2
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/service/DefaultDemoService.java
  3. 6
      sop-website/website-front/pages/doc/doc.js

@ -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;
}
}

@ -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;
}

@ -152,8 +152,7 @@ layui.config({
' "code": "10000",\n' +
' "msg": "Success",\n' +
bizResult +
' },\n' +
' "sign": "xxxxxx"\n' +
' }' +
'}';
json = formatJson(json);
$('#responseExampleJson').text(json);
@ -164,8 +163,7 @@ layui.config({
' "msg": "Service is temporarily unavailable",\n' +
' "sub_code": "isp.unknow-error",\n' +
' "sub_msg": "服务暂不可用"\n' +
' },\n' +
' "sign": "xxxxxxx"\n' +
' }' +
'}';
errorJson = formatJson(errorJson);
$('#responseErrorJson').text(errorJson);

Loading…
Cancel
Save