20
20
21
21
You can introduce the latest ` dubbo-spring-boot-starter ` to your project by adding the following dependency to your pom.xml
22
22
``` xml
23
- <dependency >
24
- <groupId >com.alibaba.boot</groupId >
25
- <artifactId >dubbo-spring-boot-starter</artifactId >
26
- <version >0.2.1-SNAPSHOT</version >
27
- </dependency >
28
-
29
- <!-- Dubbo -->
30
- <dependency >
31
- <groupId >com.alibaba</groupId >
32
- <artifactId >dubbo</artifactId >
33
- <version >2.6.5</version >
34
- </dependency >
35
- <!-- Spring Context Extras -->
36
- <dependency >
37
- <groupId >com.alibaba.spring</groupId >
38
- <artifactId >spring-context-support</artifactId >
39
- <version >1.0.2</version >
40
- </dependency >
23
+ <properties >
24
+ <spring-boot .version>2.1.1.RELEASE</spring-boot .version>
25
+ <dubbo .version>2.6.5</dubbo .version>
26
+ </properties >
27
+
28
+ <dependencyManagement >
29
+ <dependencies >
30
+ <!-- Spring Boot -->
31
+ <dependency >
32
+ <groupId >org.springframework.boot</groupId >
33
+ <artifactId >spring-boot-dependencies</artifactId >
34
+ <version >${spring-boot.version}</version >
35
+ <type >pom</type >
36
+ <scope >import</scope >
37
+ </dependency >
38
+
39
+ <!-- Dubbo dependencies -->
40
+ <dependency >
41
+ <groupId >com.alibaba</groupId >
42
+ <artifactId >dubbo-dependencies-bom</artifactId >
43
+ <version >${dubbo.version}</version >
44
+ <type >pom</type >
45
+ <scope >import</scope >
46
+ </dependency >
47
+ </dependencies >
48
+ </dependencyManagement >
49
+
50
+ <dependencies >
51
+ <!-- Dubbo Spring Boot Starter -->
52
+ <dependency >
53
+ <groupId >com.alibaba.boot</groupId >
54
+ <artifactId >dubbo-spring-boot-starter</artifactId >
55
+ <version >0.2.1-SNAPSHOT</version >
56
+ </dependency >
57
+ <dependency >
58
+ <groupId >com.alibaba</groupId >
59
+ <artifactId >dubbo</artifactId >
60
+ <version >${dubbo.version}</version >
61
+ </dependency >
62
+ <dependency >
63
+ <groupId >io.netty</groupId >
64
+ <artifactId >netty-all</artifactId >
65
+ </dependency >
66
+ </dependencies >
41
67
```
42
68
43
69
If your project failed to resolve the dependency, try to add the following repository:
@@ -78,7 +104,7 @@ If you'd like to attempt to experience latest features, you also can build from
78
104
79
105
| versions | Java | Spring Boot | Dubbo |
80
106
| -------- | ----- | ----------- | ---------- |
81
- | ` 0.2.0 ` | 1.8+ | ` 2.0 .x ` | ` 2.6.2 ` + |
107
+ | ` 0.2.1 ` | 1.8+ | ` 2.1 .x ` | ` 2.6.2 ` + |
82
108
| ` 0.1.1 ` | 1.7+ | ` 1.5.x ` | ` 2.6.2 ` + |
83
109
84
110
@@ -103,148 +129,108 @@ public interface DemoService {
103
129
104
130
### Dubbo service(s) provider
105
131
106
- Service Provider implements ` DemoService ` :
107
-
108
- ``` java
109
- @Service (
110
- version = " 1.0.0" ,
111
- application = " ${dubbo.application.id}" ,
112
- protocol = " ${dubbo.protocol.id}" ,
113
- registry = " ${dubbo.registry.id}"
114
- )
115
- public class DefaultDemoService implements DemoService {
116
-
117
- public String sayHello (String name ) {
118
- return " Hello, " + name + " (from Spring Boot)" ;
119
- }
120
-
121
- }
122
- ```
123
-
132
+ 1 . Service Provider implements ` DemoService `
124
133
134
+ ``` java
135
+ @Service (version = " 1.0.0" )
136
+ public class DefaultDemoService implements DemoService {
125
137
126
- then, provides a bootstrap class:
127
-
128
- ``` java
129
- @SpringBootApplication
130
- public class DubboProviderDemo {
131
-
132
- public static void main (String [] args ) {
133
-
134
- SpringApplication . run(DubboProviderDemo . class,args);
138
+ /**
139
+ * The default value of ${dubbo.application.name} is ${spring.application.name}
140
+ */
141
+ @Value (" ${dubbo.application.name}" )
142
+ private String serviceName;
135
143
144
+ public String sayHello (String name ) {
145
+ return String . format(" [%s] : Hello, %s" , serviceName, name);
146
+ }
136
147
}
148
+ ```
137
149
138
- }
139
- ```
140
-
150
+ 2. Provides a bootstrap class
141
151
152
+ ```java
153
+ @EnableAutoConfiguration
154
+ public class DubboProviderDemo {
142
155
143
- last, configures ` application.properties ` :
144
-
145
- ``` properties
146
- # Spring boot application
147
- spring.application.name = dubbo-provider-demo
148
- server.port = 9090
149
- management.port = 9091
150
-
151
- # Base packages to scan Dubbo Components (e.g., @Service, @Reference)
152
- dubbo.scan.basePackages = com.alibaba.boot.dubbo.demo.provider.service
153
-
154
- # Dubbo Config properties
155
- # # ApplicationConfig Bean
156
- dubbo.application.id = dubbo-provider-demo
157
- dubbo.application.name = dubbo-provider-demo
158
-
159
- # # ProtocolConfig Bean
160
- dubbo.protocol.id = dubbo
161
- dubbo.protocol.name = dubbo
162
- dubbo.protocol.port = 12345
163
-
164
- # # RegistryConfig Bean
165
- dubbo.registry.id = my-registry
166
- dubbo.registry.address = N/A
167
- ```
156
+ public static void main (String [] args ) {
157
+ SpringApplication . run(DubboProviderDemo . class,args);
158
+ }
159
+ }
160
+ ```
168
161
162
+ 3. Configures the `application. properties`:
169
163
164
+ ```properties
165
+ # Spring boot application
166
+ spring. application. name= dubbo- auto- configuration- provider- demo
170
167
171
- ` DefaultDemoService ` 's placeholders( ` ${dubbo.application.id} ` , ` ${dubbo.protocol.id} ` , ` ${dubbo.registry.id} ` ) sources from ` application.properties ` .
168
+ # Base packages to scan Dubbo Component : @com . alibaba.dubbo.config.annotation. Service
169
+ dubbo. scan. base- packages= com. alibaba. boot. dubbo. demo. provider. service
172
170
171
+ # Dubbo Application
172
+ ## The default value of dubbo. application. name is ${spring. application. name}
173
+ ## dubbo. application. name= ${spring. application. name}
173
174
175
+ # Dubbo Protocol
176
+ dubbo. protocol. name= dubbo
177
+ dubbo. protocol. port= 12345
174
178
175
- More details, please refer to [ Dubbo Provider Sample] ( dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider ) .
179
+ ## Dubbo Registry
180
+ dubbo. registry. address= N / A
181
+ ```
176
182
177
183
178
184
179
185
### Dubbo service(s) consumer
180
186
187
+ 1. Service consumer also provides a bootstrap class to reference `DemoService`
181
188
189
+ ```java
190
+ @EnableAutoConfiguration
191
+ public class DubboConsumerBootstrap {
182
192
183
- Service consumer requires Spring Beans to reference ` DemoService ` :
184
-
185
- ``` java
186
- @RestController
187
- public class DemoConsumerController {
188
-
189
- @Reference (version = " 1.0.0" ,
190
- application = " ${dubbo.application.id}" ,
191
- url = " dubbo://localhost:12345" )
192
- private DemoService demoService;
193
-
194
- @RequestMapping (" /sayHello" )
195
- public String sayHello (@RequestParam String name ) {
196
- return demoService. sayHello(name);
197
- }
198
-
199
- }
200
- ```
201
-
202
-
203
-
204
- then, also provide a bootstrap class:
205
-
206
- ``` java
207
- @SpringBootApplication (scanBasePackages = " com.alibaba.boot.dubbo.demo.consumer.controller" )
208
- public class DubboConsumerDemo {
193
+ private final Logger logger = LoggerFactory . getLogger(getClass());
209
194
210
- public static void main (String [] args ) {
195
+ @Reference (version = " 1.0.0" , url = " dubbo://localhost:12345" )
196
+ private DemoService demoService;
211
197
212
- SpringApplication . run(DubboConsumerDemo . class,args);
198
+ @Bean
199
+ public ApplicationRunner runner () {
200
+ return args - > {
201
+ logger. info(demoService. sayHello(" mercyblitz" ));
202
+ };
203
+ }
213
204
205
+ public static void main (String [] args ) {
206
+ SpringApplication . run(DubboConsumerBootstrap . class). close();
207
+ }
214
208
}
209
+ ```
215
210
216
- }
217
- ```
218
-
219
-
211
+ 2. configures `application. properties`
220
212
221
- last, configures ` application.properties ` :
213
+ ```properties
214
+ # Spring boot application
215
+ spring. application. name = dubbo- consumer- demo
216
+ server. port = 8080
217
+ management. port = 8081
222
218
223
- ``` properties
224
- # Spring boot application
225
- spring.application.name = dubbo-consumer-demo
226
- server.port = 8080
227
- management.port = 8081
228
219
220
+ # Dubbo Config properties
221
+ ## ApplicationConfig Bean
222
+ dubbo. application. id = dubbo- consumer- demo
223
+ dubbo. application. name = dubbo- consumer- demo
229
224
230
- # Dubbo Config properties
231
- # # ApplicationConfig Bean
232
- dubbo.application.id = dubbo-consumer-demo
233
- dubbo.application.name = dubbo-consumer-demo
234
-
235
- # # ProtocolConfig Bean
236
- dubbo.protocol.id = dubbo
237
- dubbo.protocol.name = dubbo
238
- dubbo.protocol.port = 12345
239
- ```
240
-
225
+ ## ProtocolConfig Bean
226
+ dubbo. protocol. id = dubbo
227
+ dubbo. protocol. name = dubbo
228
+ dubbo. protocol. port = 12345
229
+ ```
241
230
231
+ If `DubboProviderDemo ` works well, please mark sure `DubboProviderDemo ` is started.
242
232
243
- If ` DubboProviderDemo ` works well, please mark sure Dubbo service(s) is active.
244
-
245
-
246
-
247
- More details, please refer to [ Dubbo Consumer Sample] ( dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer )
233
+ More details, please refer to [Samples ](dubbo- spring- boot- samples).
248
234
249
235
250
236
@@ -261,7 +247,7 @@ Having trouble with Dubbo Spring Boot? We’d like to help!
261
247
262
248
## Building from Source
263
249
264
- If you want to try out thr latest features of Dubbo Spring Boot, it can be easily built with the [ maven wrapper] ( https://github.com/takari/maven-wrapper ) . Your JDK is 1.7 or above.
250
+ If you want to try out thr latest features of Dubbo Spring Boot , it can be easily built with the [maven wrapper](https: // github.com/takari/maven-wrapper). Your JDK is 1.8 or above.
265
251
266
252
```
267
253
$ . / mvnw clean install
@@ -301,24 +287,10 @@ The main usage of `dubbo-spring-boot-parent` is providing dependencies managemen
301
287
302
288
### [dubbo-spring-boot-samples](dubbo-spring-boot-samples)
303
289
304
- The samples project of Dubbo Spring Boot that includes two parts:
305
-
306
-
307
-
308
- #### [ Dubbo Provider Sample] ( dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider )
309
-
310
- Dubbo Service will be exported on localhost with port ` 12345 ` .
311
-
312
- * [ Health Checks] ( dubbo-spring-boot-actuator#health-checks ) : http://localhost:9091/health
313
- * [ Dubbo Endpoint] ( dubbo-spring-boot-actuator#endpoints ) : http://localhost:9091/dubbo
314
-
315
-
316
-
317
- #### [ Dubbo Consumer Sample] ( dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer )
318
-
319
- Dubbo Service will be consumed at Spring WebMVC ` Controller ` .
320
-
321
- * Demo ` Controller ` : http://localhost:8080/sayHello?name=HelloWorld
322
- * [ Health Checks] ( dubbo-spring-boot-actuator#health-checks ) : http://localhost:8081/actuator/health
323
- * [ Dubbo Endpoint] ( dubbo-spring-boot-actuator#endpoints ) : http://localhost:8081/actuator/dubbo
290
+ The samples project of Dubbo Spring Boot that includes:
324
291
292
+ - [Auto-Configuaration Samples](dubbo-spring-boot-samples/auto-configure-samples)
293
+ - [Externalized Configuration Samples](dubbo-spring-boot-samples/externalized-configuration-samples)
294
+ - [Registry Zookeeper Samples](dubbo-spring-boot-samples/dubbo-registry-zookeeper-samples)
295
+ - [Registry Nacos Samples](dubbo-spring-boot-samples/dubbo-registry-nacos-samples)
296
+ - [Sample API](dubbo-spring-boot-samples/sample-api)
0 commit comments