Springboot整合Dubbo - 代码集成和发布
一、项目介绍
1、IDEA创建多模块springboot项目
springboot-dubbo-api:接口层
springboot-dubbo-client:服务消费者
springboot-dubbo-server:服务提供者
二、springboot-dubbo-server:服务提供者
1、实现我们在springboot-dubbo-api上定义的接口,创建一个DubboServerServiceImpl类并实现ApiService
package com.example.dubbo.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.example.dubbo.service.ApiService; @Service(version = "1.0.0") public class DubboServerServiceImpl implements ApiService { @Override public String getParm(String parm) { return parm; } }
注意:代码里的@Service注解是com.alibaba.dubbo.config.annotation.Service的。
2、在resources下创建一个spring-dubbo.xml配置文件。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="provider"/> <!-- 注册中心的ip地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 --> <dubbo:annotation package="com.example.dubbo.service.impl"/> </beans>
3、在ServerApplication.java文件添加
package com.example.dubbo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication(scanBasePackages = {"com.example.dubbo"}) @MapperScan("com.example.dubbo.*") //扫描的mapper @ImportResource({"classpath:spring-dubbo.xml"}) public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }
三、springboot-dubbo-client:服务消费者
1、编写我们的Controller控制类
package com.example.dubbo.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.example.dubbo.service.ApiService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ClientController { @Reference(version = "1.0.0") private ApiService apiService; @RequestMapping("client") public String client(String parm){ return apiService.getParm(parm); } }
2、在resources下创建一个spring-dubbo.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="consumer"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:annotation package="com.example.dubbo.controller"/> </beans>
3、在ServerApplication.java文件添加
package com.example.dubbo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication(scanBasePackages = {"com.example.dubbo"}) @MapperScan("com.example.dubbo.*") //扫描的mapper @ImportResource({"classpath:spring-dubbo.xml"}) public class DubboClientApplication { public static void main(String[] args) { SpringApplication.run(DubboClientApplication.class, args); } }
至此,代码部分已经编写完毕
4、安装zookeeper注册中心到电脑中
下载地址:zookeeper下载地址
点击后下载适合自己的版本,如图所示
进入bin目录,运行zkServer.cmd文件。
5、安装dubbo-admin-2.5.8
链接:https://pan.baidu.com/s/1Hfb778tIYhwjTZ_gx2BcYg
提取码:yrfd
下载好用Jetty或Tomcat运行起来
界面
6. 现在终于可以运行我们的项目了
依次运行server、client
server控制台看到如图所示,说明注册成功