因为项目里用到了dubbo,所以我们需要建三个独立的项目,分别是生产者(provider)、消费者(consumer)和它们共用的接口
首先是接口,接口里定义了一些公共的jar包依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<groupId>com.test</groupId>
<artifactId>springBoot-inter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>
<!-- commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
下面是公共接口的项目结构,里面主要存放着生产、消费共用的JavaBean,utils和service接口。这里我们主要看与Student相关的类与接口
首先是pojo
创建一个JavaBean,命名为Student,以下是其中的一些属性,省去getter和setter方法
Integer id;
String name;
Integer number;
String subject;
Integer point;
然后是StudentService接口,我们的需求是查询出一条学生的信息:
public interface StudentService {
Student selectOne(Integer id);
}
生产者:
这里展示一些用到的项目结构,没有用到的就不展示了。
生产者里首先是依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<groupId>com.test</groupId>
<artifactId>springBoot-back</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<version>1.1.1</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- 引入公共接口依赖 -->
<dependency>
<groupId>com.test</groupId>
<artifactId>springBoot-inter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
然后是全局配置文件application.properties
########################################################
###DataSource
########################################################
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.test.pojo
#mybatis.configuration.map-underscore-to-camel-case=true
#mybatis.config-location=classpath:a/mybatis-config.xml
########################################################
###Dubbo
########################################################
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://192.168.25.128:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#这里扫的包是属于生产者自己的service包,这个包中的类实现了公共接口中的service接口
spring.dubbo.scan=com.test.service.impl
server.port=8081
数据访问对象(DAO)Mapper包,我们让DAO中的接口与对应的数据库查询.xml文件同名
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.test.mapper.StudentMapper">
<select id="selectOne" resultType="Student">
select * from student where id=#{0}
</select>
</mapper>
对应的Mapper接口:
public interface StudentMapper {
Student selectOne(Integer id);
}
service实现类:
package com.test.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.test.mapper.StudentMapper;
import com.test.pojo.Student;
import com.test.service.StudentService;
import com.test.utils.EUDataGrid;
@Service
//这里注意,这个Service是alibaba给我们提供的注解而不是spring中的service注解
//其作用是暴露服务
public class StudentServiceImpl implements StudentService{
@Autowired
StudentMapper studentMapper;
@Override
public Student selectOne(Integer id) {
return studentMapper.selectOne(id);
}
}
最后是程序启动入口App.java文件
接下来是消费者:
首先是依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<groupId>com.test</groupId>
<artifactId>springBoot-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web的启动器,包括tomcat、webmvc等jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>springBoot-inter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
然后是application.properties文件
########################################################
###DUBBO
########################################################
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://192.168.25.128:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#这里dubbo扫描的是属于公共接口的service包,用来引入服务
spring.dubbo.scan=com.test.service
server.port=8080
#若没有reference这个配置,则消费端先于提供端启动,消费端就不能订阅到后启动的提供端的服务
#加上这个配置后,不管消费端还是服务端先启动,程序都能正常访问
spring.dubbo.reference.check=false
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/static/
#thymeleaf默认的静态文件被放在/static目录下
#默认的页面被放在/templates目录下,这个路径是安全的,这里我将路径改为/templates/themes/
spring.thymeleaf.prefix=classpath:/templates/themes/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
#spring.thymeleaf.content-type=text/html
代理service:
package com.test.service.impl;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Reference;
import com.test.pojo.Student;
import com.test.service.StudentService;
@Component
public class StudentServiceImpl01 {
//这个Reference也是alibaba提供给我们的,用来表名它引入了哪个接口
//这里引入了公共接口StudentService ,并将其注入进来
@Reference
StudentService studentServiceImpl;
public Student to(Integer id) throws Exception {
return studentServiceImpl.selectOne(id);
}
}
Controller:
//restFull风,暂时不访问页面,仅仅返回Json格式的数据用来测试
@RestController
@RequestMapping("p")
public class StudentController {
@Autowired
StudentServiceImpl01 studentServiceImpl01;
@RequestMapping("/to")
public String show() throws Exception {
return studentServiceImpl01.to(1);
}
}
若要整合thymeleaf则使用下面的代码
@Controller
@RequestMapping("p")
public class StudentController {
@Autowired
StudentServiceImpl01 studentServiceImpl01;
//thymeleaf已经配置过路径了,在上面的application.properties文件中
//接下来只需要创建页面并从中取值就行了
@RequestMapping("/to")
public String show(HttpServletRequest req) throws Exception {
Student stu = studentServiceImpl01.to(1);
req.setAttribute("STU",stu);
return "你想返回的页面名";
}
}
最后创建启动类App.java