dubbo+spring-boot+mybatis+redis缓存
1、创建spring-boot项目:项目结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>xh</groupId>
<artifactId>boot-user-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-user-service-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 引入公共接口 -->
<dependency>
<groupId>dubbo</groupId>
<artifactId>dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo + spring-boot依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- spring-boot单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis + spring-boot依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- redis缓存+spring-boot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties配置
#端口号
server.port=8180
#####Dubbo配置
#1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)
dubbo.application.name=user-service-provider
#2、指定注册中心和位置
dubbo.registry.address=localhost:2181
dubbo.registry.protocol=zookeeper
#指定通信规则(通信协议、通信端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
#连接监控中心
dubbo.monitor.protocol=registry
#连接mysql数据库
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/text?useUnicode=true&charset=utf8&useSSL=true&serverTimezone=Asia/Shanghai
#开启debug打印sql语句
logging.level.dubbo.mapper.UserMapper=debug
#扫描mapper xml文件
mybatis.mapperLocations=classpath:dubbo/mapper/*.xml
mybatis.type-aliases-package=dubbo.mapper
#连接redis
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
UserServiceImpl.java redis缓存例子
package dubbo.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import dubbo.bean.User;
import dubbo.mapper.UserMapper;
import dubbo.service.UserService;
@Service("userService")
@com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper mapper;
@Override
==@Cacheable(value="user", key="'users'")==
public List<User> getUser() {
return mapper.getUser();
}
@Override
==@CacheEvict(value="user", key = "'user_' + #p0")==
public int delUser(Integer id) {
return mapper.delUser(id);
}
@Override
==@Cacheable(value="user",key="'user_' + #p0")==
public User seleUser(Integer id) {
return mapper.seleUser(id);
}
@Override
==@CacheEvict(value="user",key="'user_' + #p0.id")==
public int updataUser(User user) {
return mapper.updataUser(user);
}
}
UserController.java
package dubbo.concroller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import dubbo.bean.User;
import dubbo.service.UserService;
@RestController
public class UserController {
@Reference
private UserService userService;
@RequestMapping("/show")
public Object userList() {
return userService.getUser();
}
@RequestMapping("/getUser")
public Object getUser(@RequestParam(name = "id") Integer id) {
return userService.seleUser(id);
}
@RequestMapping("/update")
public User updateUser(User user) {
userService.updataUser(user);
return user;
}
@RequestMapping("/del")
public int delUser(Integer id) {
userService.delUser(id);
return id;
}
}
BootOrderServiceConsumerApplication.java 服务消费者启动类
package dubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
@SpringBootApplication
@EnableDubbo
@EnableCaching
public class BootOrderServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
}
}
BootUserServiceProviderApplication.java 服务提供者启动类
package dubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
@EnableDubbo
@SpringBootApplication
@EnableCaching
@MapperScan("dubbo.mapper")
public class BootUserServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootUserServiceProviderApplication.class, args);
}
}