springboot集成ssm,dubbo,redis
思路
创建接口工程
创建服务的提供者
添加springboot的起步依赖,添加mybatis集成springboot的依赖,mysql驱动的依赖,dubbo的依赖,zookeeper的依赖,redis的依赖 ,接口工程的依赖。
创建服务的消费者
添加springboot的起步依赖,添加dubbo的依赖,添加zookeeper的依赖,添加接口工程的依赖。
操作
配置服务的提供者
需要配置内置tomcat的接口,配置上下文的根,配置dubbo的服务的名字,zookeeper的地址与端口,配置mybatis的驱动,url,用户名,密码,配置redis的地址和密码
server.servlet.context-path=/
server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.application.name=provider
spring.dubbo.server=true
spring.dubbo.registry=zookeeper://127.0.0.1:2181
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
配置服务的消费者
配置内置的tomcat的端口,配置上下文的根,配置dubbo的名字和zookeeper的地址,关闭spring thymeleaf的缓存
server.servlet.context-path=/
server.port=8081
spring.application.name=springboot
spring.dubbo.registry=zookeeper://127.0.0.1:2181
spring.thymeleaf.cache=false
同时将两个tomcat的update设置为updatesources
使用mybatis的逆向工程将实体bean生成在接口工程中,将mapper和dao接口生成在服务提供者中
逆向工程的操作要尽早进行,不然可能报错。
在接口工程中将service的接口部分设计出来,二service的实现类由服务提供者进行实现
接口
package com.yuyi.jiekou.service;
public interface StudentService {
Integer quaryStudent();
}
实现类
实现类需要即要提供服务的实现,需要由spring容器创建,并暴露给dubbo,使用component,和
@Service(interfaceClass = StudentService.class,timeout = 15000),延迟时间为15s。
package com.yuyi.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.yuyi.jiekou.service.StudentService;
import com.yuyi.provider.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Service(interfaceClass = StudentService.class,timeout = 15000)
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Integer quaryStudent() {
Integer num = studentMapper.countStudent();
return num;
}
}
进行demo的设计,在服务消费者的controller层设计如下
package com.yuyi.springboot.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.yuyi.jiekou.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentController {
@Reference(interfaceClass = StudentService.class,check = false)
private StudentService studentService;
@RequestMapping("student")
public String student(Model model){
Integer num=studentService.quaryStudent();
model.addAttribute("num",num);
return "index";
}
}
调用实现类的时候,需要使用@Reference(interfaceClass = StudentService.class,check = false),表明调用的是注册的服务。
同时我们需要在启动类的上面加上spring配置的注解和dubbo配置启动的注解
消费者的启动类
package com.yuyi.springboot;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubboConfiguration
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
服务者的启动类
多了一个mapper的扫描器
package com.yuyi.provider;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.yuyi.provider.mapper")
@EnableDubboConfiguration
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
在服务消费者中,使用thymeleaf模板引擎,必须在头部加上,
<html lang="en" xmlns:th="http://www.thymeleaf.org">
这是thymeleaf的命名空间,可以配合导入的thymeleaf包实现前后端分离,取到后台的数据。
在dao层实现redis的使用,使用redis的操作模板,RedisTemplate,判断这个内容在redis中有没有,没有则从数据库取出,返回的同时放在redis中,并设置15s的失效时间,
package com.yuyi.provider.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.yuyi.jiekou.service.StudentService;
import com.yuyi.provider.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
@Service(interfaceClass = StudentService.class,timeout = 15000)
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@Override
public Integer quaryStudent() {
Integer num = (Integer) redisTemplate.opsForValue().get("studentconunt");
if(num==null){
num = studentMapper.countStudent();
redisTemplate.opsForValue().set("studentcount",num,15, TimeUnit.SECONDS);
}
return num;
}
}