├── README.md ├── api ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── imooc │ └── springboot │ └── dubbo │ └── demo │ └── DemoService.java ├── consumer ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── imooc │ │ └── springboot │ │ └── dubbo │ │ └── demo │ │ └── consumer │ │ ├── DemoConsumerController.java │ │ └── MainConsumer.java │ └── resources │ └── application.properties ├── pom.xml └── provider ├── pom.xml └── src └── main ├── java └── com │ └── imooc │ └── springboot │ └── dubbo │ └── demo │ └── provider │ ├── DemoServiceImpl.java │ └── MainProvider.java └── resources └── application.properties /README.md: -------------------------------------------------------------------------------- 1 | # SpringBoot整合dubbo示例 2 | ## 一、定义API(API模块) 3 | ### 1. 定义api 4 | ```java 5 | package com.imooc.springboot.dubbo.demo; 6 | 7 | public interface DemoService { 8 | String sayHello(String name); 9 | } 10 | ``` 11 | ### 2. install到本地 12 | 对外提供的maven坐标如下: 13 | ```xml 14 | 15 | com.imooc 16 | dubbo-demo-api 17 | 1.0-SNAPSHOT 18 | 19 | ``` 20 | ## 二、服务提供者(provider模块) 21 | ### 1. 增加maven依赖 22 | ```xml 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-parent 27 | 1.5.3.RELEASE 28 | 29 | 30 | 31 | io.dubbo.springboot 32 | spring-boot-starter-dubbo 33 | 1.0.0 34 | 35 | 36 | 37 | com.imooc 38 | dubbo-demo-api 39 | 1.0-SNAPSHOT 40 | 41 | ``` 42 | ### 2. 实现API接口 43 | ```java 44 | package com.imooc.springboot.dubbo.demo.provider; 45 | 46 | import com.alibaba.dubbo.config.annotation.Service; 47 | import com.imooc.springboot.dubbo.demo.DemoService; 48 | 49 | @Service 50 | public class DemoServiceImpl implements DemoService { 51 | 52 | public String sayHello(String name) { 53 | return "Hello, " + name + " (from Spring Boot)"; 54 | } 55 | 56 | } 57 | ``` 58 | ### 3. springboot配置文件 - application.properties 59 | ```bash 60 | spring.dubbo.application.name=demo-provider 61 | #这里使用广播的注册方式, 62 | #如果有Can't assign address异常需要加vm参数: 63 | #-Djava.net.preferIPv4Stack=true 64 | spring.dubbo.registry.address=multicast://224.5.6.7:1234 65 | spring.dubbo.protocol.name=dubbo 66 | spring.dubbo.protocol.port=20880 67 | spring.dubbo.scan=com.imooc.springboot.dubbo.demo.provider 68 | ``` 69 | 70 | ### 4. 启动类 71 | ```java 72 | package com.imooc.springboot.dubbo.demo.provider; 73 | 74 | import org.springframework.boot.SpringApplication; 75 | import org.springframework.boot.autoconfigure.SpringBootApplication; 76 | 77 | @SpringBootApplication 78 | public class MainProvider { 79 | public static void main(String[] args) { 80 | SpringApplication.run(MainProvider.class,args); 81 | } 82 | } 83 | ``` 84 | ## 三、服务消费者(consumer模块) 85 | ### 1. 增加maven依赖 86 | ```xml 87 | 88 | 89 | org.springframework.boot 90 | spring-boot-starter-parent 91 | 1.5.3.RELEASE 92 | 93 | 94 | 95 | org.springframework.boot 96 | spring-boot-starter-web 97 | 98 | 99 | 100 | io.dubbo.springboot 101 | spring-boot-starter-dubbo 102 | 1.0.0 103 | 104 | 105 | 106 | com.imooc 107 | dubbo-demo-api 108 | 1.0-SNAPSHOT 109 | 110 | ``` 111 | ### 2. 实现controller 112 | ```java 113 | package com.imooc.springboot.dubbo.demo.consumer; 114 | 115 | import com.alibaba.dubbo.config.annotation.Reference; 116 | import com.imooc.springboot.dubbo.demo.DemoService; 117 | import org.springframework.web.bind.annotation.RequestMapping; 118 | import org.springframework.web.bind.annotation.RequestParam; 119 | import org.springframework.web.bind.annotation.RestController; 120 | 121 | @RestController 122 | public class DemoConsumerController { 123 | 124 | @Reference 125 | private DemoService demoService; 126 | 127 | @RequestMapping("/sayHello") 128 | public String sayHello(@RequestParam String name) { 129 | return demoService.sayHello(name); 130 | } 131 | 132 | } 133 | ``` 134 | ### 3. springboot配置文件 - application.properties 135 | ```bash 136 | server.port=8080 137 | #dubbo config 138 | spring.dubbo.application.name=demo-consumer 139 | #这里使用广播的注册方式, 140 | #如果有Can't assign address异常需要加vm参数: 141 | #-Djava.net.preferIPv4Stack=true 142 | spring.dubbo.registry.address=multicast://224.5.6.7:1234 143 | spring.dubbo.scan=com.imooc.springboot.dubbo.demo.consumer 144 | ``` 145 | 146 | ### 4. 启动类 147 | ```java 148 | package com.imooc.springboot.dubbo.demo.consumer; 149 | 150 | import org.springframework.boot.SpringApplication; 151 | import org.springframework.boot.autoconfigure.SpringBootApplication; 152 | 153 | @SpringBootApplication 154 | public class Main { 155 | 156 | public static void main(String[] args) { 157 | SpringApplication.run(Main.class,args); 158 | } 159 | 160 | } 161 | ``` 162 | -------------------------------------------------------------------------------- /api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.imooc 8 | dubbo-demo-api 9 | 1.0-SNAPSHOT 10 | 11 | 12 | -------------------------------------------------------------------------------- /api/src/main/java/com/imooc/springboot/dubbo/demo/DemoService.java: -------------------------------------------------------------------------------- 1 | package com.imooc.springboot.dubbo.demo; 2 | 3 | public interface DemoService { 4 | String sayHello(String name); 5 | } -------------------------------------------------------------------------------- /consumer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 1.5.3.RELEASE 9 | 10 | 4.0.0 11 | 12 | com.imooc 13 | dubbo-demo-consumer 14 | 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-web 20 | 21 | 22 | io.dubbo.springboot 23 | spring-boot-starter-dubbo 24 | 1.0.0 25 | 26 | 27 | com.imooc 28 | dubbo-demo-api 29 | 1.0-SNAPSHOT 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /consumer/src/main/java/com/imooc/springboot/dubbo/demo/consumer/DemoConsumerController.java: -------------------------------------------------------------------------------- 1 | package com.imooc.springboot.dubbo.demo.consumer; 2 | 3 | import com.alibaba.dubbo.config.annotation.Reference; 4 | import com.imooc.springboot.dubbo.demo.DemoService; 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 DemoConsumerController { 11 | 12 | @Reference 13 | private DemoService demoService; 14 | 15 | @RequestMapping("/sayHello") 16 | public String sayHello(@RequestParam String name) { 17 | return demoService.sayHello(name); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /consumer/src/main/java/com/imooc/springboot/dubbo/demo/consumer/MainConsumer.java: -------------------------------------------------------------------------------- 1 | package com.imooc.springboot.dubbo.demo.consumer; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MainConsumer { 8 | 9 | public static void main(String[] args) { 10 | 11 | SpringApplication.run(MainConsumer.class,args); 12 | 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /consumer/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 2 | #dubbo config 3 | spring.dubbo.application.name=demo-consumer 4 | spring.dubbo.registry.address=multicast://224.5.6.7:1234 5 | spring.dubbo.scan=com.imooc.springboot.dubbo.demo.consumer 6 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.imooc 8 | dubbo-demo 9 | pom 10 | 1.0-SNAPSHOT 11 | 12 | api 13 | consumer 14 | provider 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /provider/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 1.5.3.RELEASE 10 | 11 | 4.0.0 12 | 13 | com.imooc 14 | dubbo-demo-provider 15 | 16 | 17 | 18 | io.dubbo.springboot 19 | spring-boot-starter-dubbo 20 | 1.0.0 21 | 22 | 23 | com.imooc 24 | dubbo-demo-api 25 | 1.0-SNAPSHOT 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /provider/src/main/java/com/imooc/springboot/dubbo/demo/provider/DemoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.imooc.springboot.dubbo.demo.provider; 2 | 3 | import com.alibaba.dubbo.config.annotation.Service; 4 | import com.imooc.springboot.dubbo.demo.DemoService; 5 | 6 | @Service 7 | public class DemoServiceImpl implements DemoService { 8 | 9 | public String sayHello(String name) { 10 | return "Hello, " + name + " (from Spring Boot)"; 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /provider/src/main/java/com/imooc/springboot/dubbo/demo/provider/MainProvider.java: -------------------------------------------------------------------------------- 1 | package com.imooc.springboot.dubbo.demo.provider; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MainProvider { 8 | 9 | public static void main(String[] args) { 10 | 11 | SpringApplication.run(MainProvider.class,args); 12 | 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /provider/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.dubbo.application.name=demo-provider 2 | spring.dubbo.registry.address=multicast://224.5.6.7:1234 3 | spring.dubbo.protocol.name=dubbo 4 | spring.dubbo.protocol.port=20880 5 | spring.dubbo.scan=com.imooc.springboot.dubbo.demo.provider --------------------------------------------------------------------------------