SpringBoot整合dubbo(xml配置dubbo)
源码
GitHub:https://github.com/291685399/springboot-learning/tree/master/springboot-dubbo02
准备
1、在服务器或者本地搭建好zookeeper,具体可参考文章:https://blog.csdn.net/qq\_35620501/article/details/87519306
2、在服务器或者本地搭建好dubbo-admin,具体可参考文章:https://blog.csdn.net/qq\_35620501/article/details/89037354
代码
- 新建空的maven工程,命名为springboot-dubbo02
项目目录结构如下图:
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<modules>
<module>springboot-dubbo02-api-web</module>
<module>springboot-dubbo02-service-impl</module>
<module>springboot-dubbo02-service</module>
</modules>
- 新建模块,命名为springboot-dubbo02–service
service主要是用来存放在实体类、Enum、工具类、service等
项目结构如下图:
DemoService:
public interface DemoService {
public List<Message> findMessage();
}
Message:
public class Message implements Serializable {
private int id;
private String city;
private String time;
private String weather;
public Message() { }
public int getId() { return id;}
public void setId(int id) { this.id = id;}
public String getCity() { return city;}
public void setCity(String city) { this.city = city;}
public String getTime() { return time;}
public void setTime(String time) { this.time = time;}
public String getWeather() { return weather;}
public void setWeather(String weather) { this.weather = weather;}
}
ApiResponse:
public class ApiResponse implements Serializable {
private int code;
private String msg;
private Object data;
public ApiResponse() { }
public ApiResponse(int code, String msg) {
this.code = code;
this.msg = msg;
}
public ApiResponse(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public int getCode() { return code;}
public void setCode(int code) { this.code = code;}
public String getMsg() { return msg;}
public void setMsg(String msg) { this.msg = msg;}
public Object getData() { return data;}
public void setData(Object data) { this.data = data;}
}
- 新建模块,命名为springboot-dubbo02-service-impl
service-impl主要是存放service接口实现类、持久层的实现
项目结构如下图:
pom.xml:
<parent>
<groupId>com.wyj</groupId>
<artifactId>springboot-dubbo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 项目依赖 -->
<dependency>
<groupId>com.wyj</groupId>
<artifactId>springboot-dubbo02-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springboot-dubbo02-service-impl</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
dubbo-provider.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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="${dubbo.application.name}"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry id="register" address="${dubbo.registry.address}"/>
<dubbo:registry id="local" address="zookeeper://127.0.0.1:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}"/>
<!-- 声明需要暴露的服务接口(注意是接口,不是实现类) -->
<!-- 这里是具体实现类,id和上面的暴露的服务接口ref要一致,dubbo就是通过这个来注册对应的服务 -->
<!-- 同模块的registry使用local,不同模块的使用远程的register -->
<dubbo:service registry="register" interface="com.wyj.service.DemoService" ref="demoService"/>
<!-- 消费dubbo服务 -->
<!--<dubbo:reference registry="register" interface="" id=""/>-->
</beans>
DemoServiceImpl:
这个@Service是spring提供的注解
@Service("demoService")
public class DemoServiceImpl implements DemoService {
@Autowired
private DemoMapper demoMapper;
@Override
public List<Message> findMessage() {
return demoMapper.findMessage();
}
}
DemoMapper:
public interface DemoMapper {
public List<Message> findMessage();
}
DemoMappe.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.wyj.mapper.DemoMapper">
<select id="findMessage" resultType="com.wyj.entity.po.Message">
select * from message
</select>
</mapper>
application.properties:
# tomcat
server.port=8081
# dubbo
dubbo.application.name=demo-provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# dataBase
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot-dubbo02?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
- 新建模块,命名为springboot-dubbo02-api-web
api主要是用来存放controller、handler、interceptor
项目结构如下图:
pom.xml:
<parent>
<groupId>com.wyj</groupId>
<artifactId>springboot-dubbo02</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- springboor-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 项目依赖 -->
<dependency>
<groupId>com.wyj</groupId>
<artifactId>springboot-dubbo02-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>springboot-dubbo02-api-web</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
dubbo-consumer.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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="${dubbo.application.name}"/>
<dubbo:registry id="register" address="${dubbo.registry.address}"/>
<dubbo:registry id="local" address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="${dubbo.protocol.name}"/>
<!-- 生成远程服务代理,可以和本地bean一样调用 -->
<!-- 同模块的registry使用local,不同模块的使用远程的register -->
<dubbo:reference registry="register" id="demoService" interface="com.wyj.service.DemoService"/>
</beans>
DemoController:
@RestController
public class DemoController {
@Autowired
private DemoService demoService;
@RequestMapping(value = "/query")
public ApiResponse demo() {
try {
List<Message> messageList = demoService.findMessage();
return new ApiResponse(200, "操作成功", messageList);
} catch (Exception e) {
e.printStackTrace();
return new ApiResponse(500, "系统异常");
}
}
}
application.properties:
# tomcat
server.port=8080
# dubbo
dubbo.application.name=demo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
测试
- 先运行service-impl的服务,再启动api的服务,原则上是先启动service-impl的
- 访问
浏览器访问:http://127.0.0.1:8080/query
查看监控中心
- 访问dubbo-admin
点击服务治理中的服务
从监控中心可以看到com.wyj.service.DemoService这个接口能够正常注册到zookeeper中,并被api给消费
还没有评论,来说两句吧...