GitHub:https://github.com/291685399/springboot-learning/tree/master/springboot-dubbo01
1、在服务器或者本地搭建好zookeeper,具体可参考文章:https://blog.csdn.net/qq_35620501/article/details/87519306
2、在服务器或者本地搭建好dubbo-admin,具体可参考文章:https://blog.csdn.net/qq_35620501/article/details/89037354
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.0.RELEASEversion>
parent>
<modules>
<module>springboot-dubbo01-api-webmodule>
<module>springboot-dubbo01-service-implmodule>
<module>springboot-dubbo01-servicemodule>
modules>
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;}
}
service-impl主要是存放service接口实现类、持久层的实现
<parent>
<groupId>com.wyjgroupId>
<artifactId>springboot-dubbo01artifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>io.dubbo.springbootgroupId>
<artifactId>spring-boot-starter-dubboartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>com.wyjgroupId>
<artifactId>springboot-dubbo01-serviceartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>log4j-over-slf4jartifactId>
dependency>
dependencies>
<build>
<finalName>springboot-dubbo01-service-implfinalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
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
api主要是用来存放controller、handler、interceptor
<parent>
<groupId>com.wyjgroupId>
<artifactId>springboot-dubbo01artifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>io.dubbo.springbootgroupId>
<artifactId>spring-boot-starter-dubboartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>com.wyjgroupId>
<artifactId>springboot-dubbo01-service-implartifactId>
<version>0.0.1-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
exclusion>
<exclusion>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<finalName>springboot-dubbo01-api-webfinalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
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的
访问
浏览器访问:http://127.0.0.1:8080/query
点击服务治理中的服务
从监控中心可以看到com.wyj.service.DemoService这个接口能够正常注册到zookeeper中,并被api给消费