github上有一个开源项目spring-boot-starter-dubbo 提供了spring-boot与dubbo的集成功能,直接拿来用即可。(记得给作者点赞,以示感谢!)
下面是使用步骤,先看下工程的大致结构:
一、引入相关的依赖项
1 subprojects { 2 buildscript { 3 ext { 4 springBootVersion = '1.5.3.RELEASE' 5 } 6 repositories { 7 mavenLocal() 8 maven { 9 url "http://maven.aliyun.com/nexus/content/groups/public/" 10 } 11 mavenCentral() 12 } 13 dependencies { 14 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 15 } 16 } 17 18 apply plugin: "java" 19 apply plugin: "maven" 20 apply plugin: 'idea' 21 22 23 targetCompatibility = 1.8 24 sourceCompatibility = 1.8 25 26 repositories { 27 mavenLocal() 28 maven { 29 url "http://maven.aliyun.com/nexus/content/groups/public/" 30 } 31 mavenCentral() 32 } 33 34 configurations.all { 35 resolutionStrategy.cacheChangingModulesFor 1, "minutes" 36 } 37 38 dependencies { 39 compile('io.dubbo.springboot:spring-boot-starter-dubbo:1.0.0') 40 compile('org.springframework.boot:spring-boot-starter-web:1.5.3.RELEASE') 41 } 42 }
View Code
这是最外层根目录下的build.gradle,关键地方就是最后dependencies引入的2个依赖项
二、service-api中定义接口
1 package com.cnblogs.yjmyzz.service.api; 2 3 /** 4 * Created by 菩提树下的杨过(http:/yjmyzz.cnblogs.com) on 2017/5/21. 5 */ 6 public interface DemoService { 7 String hello(String nickName); 8 }
View Code
这一步平淡无奇,没什么好说的
三、service-provider
3.1 提供接口实现
1 package com.cnblogs.yjmyzz.service.impl; 2 3 import com.alibaba.dubbo.config.annotation.Service; 4 import com.cnblogs.yjmyzz.service.api.DemoService; 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 8 /** 9 * Created by 菩提树下的杨过(http:/yjmyzz.cnblogs.com) on 2017/5/21. 10 */ 11 @Service(version = "1.0.0") 12 public class DemoServiceImpl implements DemoService { 13 14 Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class); 15 16 public String hello(String nickName) { 17 logger.info(nickName + " call me!"); 18 return String.format("hi , %s!", nickName); 19 } 20 }
View Code
常规套路,不用多说
3.2 编写ServiceProvider主类
1 package com.cnblogs.yjmyzz.service; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 /** 7 * Created by yangjunming on 2017/5/21. 8 */ 9 @SpringBootApplication 10 public class ServiceProvider { 11 public static void main(String[] args) { 12 SpringApplication.run(ServiceProvider.class, args); 13 } 14 }
View Code
仍然是spring-boot的经典套路,跟dubbo也没任何关系
3.3 application.yml配置
1 server: 2 port: 8001 3 4 spring: 5 dubbo: 6 scan: com.cnblogs.yjmyzz.service 7 application: 8 name: provider 9 registry: 10 address: zookeeper://127.0.0.1:2181 11 protocol: 12 name: dubbo 13 port: 20880
这里是重点,指定了dubbo服务提供方启动所需的zk注册地址,协议类型及端口,包括扫描的包。
四、service-consumer
4.1 定义一个辅助用的Proxy
1 package com.cnblogs.yjmyzz.service.proxy; 2 3 import com.alibaba.dubbo.config.annotation.Reference; 4 import com.cnblogs.yjmyzz.service.api.DemoService; 5 import org.springframework.stereotype.Component; 6 7 /** 8 * Created by yangjunming on 2017/5/21. 9 */ 10 @Component 11 public class ServiceProxy { 12 13 @Reference(version = "1.0.0") 14 public DemoService demoService; 15 }
就是一个标准的spring组件(不管是叫proxy还是叫container都无所谓,随个人喜好),在该组件中持有对Service的引用实例,注意:如果指定了version,则该版本号要与service-provider中的版本号一致
4.2 调用服务
1 package com.cnblogs.yjmyzz.service; 2 3 import com.cnblogs.yjmyzz.service.proxy.ServiceProxy; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.context.ConfigurableApplicationContext; 7 8 /** 9 * Created by 菩提树下的杨过(http:/yjmyzz.cnblogs.com) on 2017/5/21. 10 */ 11 @SpringBootApplication 12 public class ServiceConsumer { 13 14 public static void main(String[] args) { 15 ConfigurableApplicationContext ctx = SpringApplication.run(ServiceConsumer.class, args); 16 ServiceProxy proxy = ctx.getBean(ServiceProxy.class); 17 System.out.println(proxy.demoService.hello("菩提树下的杨过"));//调用服务 18 } 19 }
View Code
一看即明,不多解释。
4.3 application.yml配置
1 server: 2 port: 8002 3 4 spring: 5 dubbo: 6 scan: com.cnblogs.yjmyzz.service 7 application: 8 name: consumer 9 registry: 10 address: zookeeper://127.0.0.1:2181
ok,搞定!
上述示例源代码,已托管至github,有需要的朋友自行下载:https://github.com/yjmyzz/spring-boot-dubbo-demo
时间: 2024-11-08 21:12:19