本文共 7257 字,大约阅读时间需要 24 分钟。
开发工具:intellij idea
JDK版本:1.8
项目管理工具:Maven 3.2.5
总项目 pom.xml配置代码:
1 25 4.0.0 6 7spring-boot-dubbo 8spring-boot-dubbo 9pom 101.0-SNAPSHOT 1112 16 17spring-boot-dubbo-api 13spring-boot-dubbo-provider 14spring-boot-dubbo-consumer 1518 22 23org.springframework.boot 19spring-boot-starter-parent 201.5.6.RELEASE 2124 27 281.7 251.5.6.RELEASE 2629 30 47 4831 34 35org.springframework.boot 32spring-boot-starter-web 3336 40 41org.springframework.boot 37spring-boot-starter-test 38test 3942 46io.dubbo.springboot 43spring-boot-starter-dubbo 441.0.0 45
provider pom.xml配置代码:
1 25 6 11spring-boot-dubbo 7spring-boot-dubbo 81.0-SNAPSHOT 9../pom.xml 104.0.0 12 13spring-boot-dubbo-provider 14 1516 2217 21spring-boot-dubbo 18spring-boot-dubbo-api 191.0-SNAPSHOT 2023 31 3224 3025 29org.springframework.boot 26spring-boot-maven-plugin 27${springboot.version} 28
consumer pom.xml配置代码:
1 25 6 11spring-boot-dubbo 7spring-boot-dubbo 81.0-SNAPSHOT 9../pom.xml 104.0.0 12 13spring-boot-dubbo-consumer 14 1516 2217 21spring-boot-dubbo 18spring-boot-dubbo-api 191.0-SNAPSHOT 2023 31 3224 3025 29org.springframework.boot 26spring-boot-maven-plugin 27${springboot.version} 28
1 package com.goku.demo.api.service;2 3 /**4 * Created by nbfujx on 2017-11-23.5 */6 public interface ExampleService {7 String echo(String str);8 }
## 避免和 consumer 工程端口冲突server.port=8081spring.dubbo.application.name=providerspring.dubbo.registry.address=zookeeper://localhost:2181spring.dubbo.protocol.name=dubbospring.dubbo.protocol.port=20880## 本机的IP地址spring.dubbo.protocol.host=127.0.0.1spring.dubbo.scan=com.goku.demo## 设置Modulespring.dubbo.module.default=false
package com.goku.demo.service.impl;import com.goku.demo.api.service.ExampleService;import com.alibaba.dubbo.config.annotation.Service;/** * Created by nbfujx on 2017-11-23. */@Service(version = "1.0.0")public class ExampleServiceImpl implements ExampleService { @Override public String echo(String str) { return "hello"+ str; }}
spring.dubbo.application.name=consumerspring.dubbo.registry.address=zookeeper://localhost:2181spring.dubbo.scan=com.goku.demospring.dubbo.module.default=false
1 package com.goku.demo.controller; 2 3 import com.alibaba.dubbo.config.annotation.Reference; 4 import com.goku.demo.api.service.ExampleService; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 /**10 * Created by nbfujx on 2017-11-20.11 */12 @RestController13 public class ExampleController {14 15 @Reference(version = "1.0.0")16 public ExampleService exampleService;17 18 @RequestMapping("/")19 public String helloWorld()20 {21 return "helloWorld";22 }23 24 @RequestMapping("/{str}")25 public String echo(@PathVariable String str)26 {27 return exampleService.echo(str);28 }29 }
mock的配置可以在出现非业务异常(比如超时,网络异常等)时执行。mock的配置支持两种,一种为boolean值,默认的为false。如果配置为true,则缺省使用mock类名,即类名+Mock后缀;另外一种则是配置"return null",可以很简单的忽略掉异常。
1 package com.goku.demo.api.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * Created by nbfujx on 2017-11-27. 7 */ 8 @Service 9 public class ExampleServiceMock implements ExampleService {10 @Override11 public String echo(String str) {12 return "hello"+ str+"this is error!";13 }14 }
1 package com.goku.demo.controller; 2 3 import com.alibaba.dubbo.config.annotation.Reference; 4 import com.goku.demo.api.service.ExampleService; 5 import com.goku.demo.api.service.ExampleServiceMock; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.PathVariable; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController;10 11 /**12 * Created by nbfujx on 2017-11-20.13 */14 @RestController15 public class ExampleController {16 17 @Reference(version = "1.0.0",check=false,mock="com.goku.demo.api.service.ExampleServiceMock")18 @Autowired19 public ExampleService exampleService;20 21 @RequestMapping("/")22 public String helloWorld()23 {24 return "helloWorld";25 }26 27 @RequestMapping("/{str}")28 public String echo(@PathVariable String str)29 {30 return exampleService.echo(str);31 }32 }
待续
转载地址:http://dcuoo.baihongyu.com/