springboot+mybatis+dubbo+aop日志第二篇 springbootdubbomybatis
本篇主要介绍dubbo-demo-api接口层和dubbo-demo-service层,介绍代码前,咱们先来回顾一下整个demo工程的结构
1.dubbo-demo-api
这里面主要是定义所有的接口,demo工程里就定义了一个测试接口,看一下该层的代码结构
DemoApi.java代码
package com.example.dubbo.demo.api; import java.util.List; import dubbo.demo.model.entity.Student; /** * Demo 接口定义 * @author * */ public interface DemoApi { String sayHello(String name); void add(Student student); List<Student> getAll(); }
2.dubbo-demo-service
该层主要实现api的接口,因此该层依赖api层,pom文件的依赖如下
<dependency> <groupId>com.example.dubbo</groupId> <artifactId>dubbo-demo-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
说明:需要在api层执行install的maven命令,把api的jar包生打包到本地.m2仓库。
该层代码结构如下:
-
aop包主要是记录每一个service方法调用时的入参,返回值,执行时间、接口的全名称等信息。
-
impl包实现api的接口逻辑
-
mapper包是mybatis与数据库交互的方法,与mapper.xml对应
-
mapping文件夹下保存所有mapper.xml文件
-
dubbo-config.xml 是dubbo暴露服务的配置文件
impl/DemoApiImpl.java代码
package com.example.dubbo.demo.service.impl; import com.example.dubbo.demo.api.DemoApi; import com.example.dubbo.demo.service.mapper.StudentMapper; import dubbo.demo.model.entity.Student; import java.util.List; import org.apache.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired; /** * * @author chenlong12 * */ @Service public class DemoApiImpl implements DemoApi { @Autowired private StudentMapper studentMapper; /** * 实现 sayHello 接口 * * @param name * @return */ @Override public String sayHello(String name) { return "Hello, " + name + " (from Spring Boot with dubbo-2.7.1)"; } @Override public void add(Student student) { // TODO Auto-generated method stub studentMapper.add(student); } @Override public List<Student> getAll() { // TODO Auto-generated method stub return studentMapper.getAll(); } }
mapper/StudentMapper.java代码
package com.example.dubbo.demo.service.mapper; import java.util.List; import dubbo.demo.model.entity.Student; public interface StudentMapper { void add(Student student); List<Student> getAll(); }
mapping/StudentMapper.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.example.dubbo.demo.service.mapper.StudentMapper"> <resultMap id="StudentResultMap" type="dubbo.demo.model.entity.Student"> <result column="id" jdbcType="INTEGER" property="id" /> <result column="num" jdbcType="VARCHAR" property="num" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="age" jdbcType="INTEGER" property="age" /> <result column="sex" jdbcType="VARCHAR" property="sex" /> </resultMap> <insert id="add" parameterType="dubbo.demo.model.entity.Student"> insert into student (num, name, age,sex) values (#{num},#{name},#{age},#{sex}) </insert> <!--我自己加的方法--> <select id="getAll" resultType="dubbo.demo.model.entity.Student"> select * from student </select> </mapper>
application.properties
dubbo-config.xml会引用该配置文件里的内容
spring.config.name=application # spring 的环境配置 spring.profiles.active=dev # 服务启动端口,即内置 tomcat 启动时占用的端口 server.port=8087 spring.aop.auto=true spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/school?useSSL=false&useUnicode=true&characterEncoding=UTF-8 spring.datasource.username= spring.datasource.password= mybatis.mapper-locations=classpath:mapping/*.xml mybatis.type-aliases-package=dubbo.demo.model.entity # dubbo config # 应用定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识 my.dubbo.application.name=dubbo-demo-service # 应用所属者 my.dubbo.application.owner=ll # 应用所属组织 my.dubbo.application.organization=ll # 使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper # 注册中心id my.dubbo.registry.id=zookeeper-registry # 注册中心协议 my.dubbo.registry.protocol=zookeeper # 注册中心地址 my.dubbo.registry.address=127.0.0.1:2181 # dubbo协议在20880端口暴露服务 # 协议名称 my.dubbo.protocol.name=dubbo # 协议端口 my.dubbo.protocol.port=20880 # 协议访问log my.dubbo.protocol.accesslog=dubbo-access.log # 重试次数 my.dubbo.provider.retries=0 # 超时时间 my.dubbo.provider.timeout=3000 # 注册监控中心 my.dubbo.monitor.protocol=registry
dubbo-config.xml
这dubbo的配置文件,所有的服务都是通过这个配置文件发布出去
<?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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字 --> <dubbo:application name="${my.dubbo.application.name}" owner="ll" organization="ll" /> <!-- 使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper--> <dubbo:registry id="zookeeper-registry" protocol="${my.dubbo.registry.protocol}" address="${my.dubbo.registry.address}" /> <!-- dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="${my.dubbo.protocol.name}" port="${my.dubbo.protocol.port}" accesslog="dubbo-access.log"/> <dubbo:provider retries="0" timeout="30000"/> <dubbo:monitor protocol="registry"/> <bean id="demoApiImpl" class="com.example.dubbo.demo.service.impl.DemoApiImpl"></bean> <!-- 使用 dubbo 协议实现定义好的 Service Api 接口--> <dubbo:service interface="com.example.dubbo.demo.api.DemoApi" ref="demoApiImpl" retries="0" timeout="60000"> <dubbo:method name="add" timeout="10000" retries="0" loadbalance="leastactive" actives="5" /> </dubbo:service> </beans>
详细的配置说明请参见dubbo官方文档
http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-service.html
DubboDemoServiceApplication.java
项目启动main方法,项目启动前需要先把zookeeper启动
package com.example.dubbo.demo.service; import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan; import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; //@EnableDubboConfig //@DubboComponentScan("com.example.dubbo.demo.service.impl") @MapperScan("com.example.dubbo.demo.service.mapper") @SpringBootApplication @ImportResource(locations="classpath:dubbo-config.xml") public class DubboDemoServiceApplication { public static void main(String[] args) { SpringApplication.run(DubboDemoServiceApplication.class, args); } }
下一篇咱们介绍怎么用aop获取每一个service服务的入参、出参、执行时间等信息
上一篇: MyBatis缓存