源码
GitHub:https://github.com//springboot-learning/tree/master/springboot-dubbo01
准备
代码
- 新建空的maven工程,命名为springboot-dubbo01
<parent> <groupId>org.springframework.boot
groupId> <artifactId>spring-boot-starter-parent
artifactId> <version>2.0.0.RELEASE
version>
parent> <modules> <module>springboot-dubbo01-api-web
module> <module>springboot-dubbo01-service-impl
module> <module>springboot-dubbo01-service
module>
modules>
- 新建模块,命名为springboot-dubbo01–service
service主要是用来存放在实体类、Enum、工具类、service等
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-dubbo01-service-impl
service-impl主要是存放service接口实现类、持久层的实现
<parent> <groupId>com.wyj
groupId> <artifactId>springboot-dubbo01
artifactId> <version>0.0.1-SNAPSHOT
version>
parent> <dependencies>
<dependency> <groupId>org.mybatis.spring.boot
groupId> <artifactId>mybatis-spring-boot-starter
artifactId> <version>2.0.1
version>
dependency>
<dependency> <groupId>mysql
groupId> <artifactId>mysql-connector-java
artifactId> <scope>runtime
scope>
dependency>
<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>
<dependency> <groupId>io.dubbo.springboot
groupId> <artifactId>spring-boot-starter-dubbo
artifactId> <version>1.0.0
version>
dependency>
<dependency> <groupId>com.wyj
groupId> <artifactId>springboot-dubbo01-service
artifactId> <version>0.0.1-SNAPSHOT
version>
dependency>
<dependency> <groupId>org.slf4j
groupId> <artifactId>slf4j-api
artifactId> <version>1.7.25
version>
dependency> <dependency> <groupId>org.slf4j
groupId> <artifactId>log4j-over-slf4j
artifactId>
dependency>
dependencies> <build> <finalName>springboot-dubbo01-service-impl
finalName> <plugins> <plugin> <groupId>org.springframework.boot
groupId> <artifactId>spring-boot-maven-plugin
artifactId>
plugin>
plugins>
build>
DemoServiceImpl:
@Service(timeout = 6000)//这个service是dubbo的service 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 spring.dubbo.application.name=demo-provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.wyj.service.impl # dataBase spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot-dubbo01?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-dubbo01-api-web
api主要是用来存放controller、handler、interceptor
<parent> <groupId>com.wyj
groupId> <artifactId>springboot-dubbo01
artifactId> <version>0.0.1-SNAPSHOT
version>
parent> <dependencies>
<dependency> <groupId>org.springframework.boot
groupId> <artifactId>spring-boot-starter-thymeleaf
artifactId>
dependency>
<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>
<dependency> <groupId>io.dubbo.springboot
groupId> <artifactId>spring-boot-starter-dubbo
artifactId> <version>1.0.0
version>
dependency>
<dependency> <groupId>com.wyj
groupId> <artifactId>springboot-dubbo01-service-impl
artifactId> <version>0.0.1-SNAPSHOT
version> <exclusions> <exclusion> <groupId>org.mybatis.spring.boot
groupId> <artifactId>mybatis-spring-boot-starter
artifactId>
exclusion> <exclusion> <groupId>mysql
groupId> <artifactId>mysql-connector-java
artifactId>
exclusion>
exclusions>
dependency>
dependencies> <build> <finalName>springboot-dubbo01-api-web
finalName> <plugins> <plugin> <groupId>org.springframework.boot
groupId> <artifactId>spring-boot-maven-plugin
artifactId>
plugin>
plugins>
build>
DemoController:
注意:
这里不能使用@Autowired注入bean,要使用dubbo中的注解@Reference注入bean和dubbo接口
@RestController public class DemoController {
@Reference 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 spring.dubbo.application.name=demo-consumer spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.scan=com.wyj.controller
测试
- 先运行service-impl的服务,再启动api的服务,原则上是先启动service-impl的
- 访问
查看监控中心
- 访问dubbo-admin

点击服务治理中的服务

从监控中心可以看到com.wyj.service.DemoService这个接口能够正常注册到zookeeper中,并被api给消费
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/222391.html原文链接:https://javaforall.net
