创建接口工程 创建服务的提供者 创建服务的消费者 配置服务的提供者 配置服务的消费者 同时将两个tomcat的update设置为updatesources 使用mybatis的逆向工程将实体bean生成在接口工程中,将mapper和dao接口生成在服务提供者中 在接口工程中将service的接口部分设计出来,二service的实现类由服务提供者进行实现 实现类 进行demo的设计,在服务消费者的controller层设计如下 调用实现类的时候,需要使用@Reference(interfaceClass = StudentService.class,check = false),表明调用的是注册的服务。 服务者的启动类 在服务消费者中,使用thymeleaf模板引擎,必须在头部加上, 这是thymeleaf的命名空间,可以配合导入的thymeleaf包实现前后端分离,取到后台的数据。 在dao层实现redis的使用,使用redis的操作模板,RedisTemplate,判断这个内容在redis中有没有,没有则从数据库取出,返回的同时放在redis中,并设置15s的失效时间,
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
逆向工程的操作要尽早进行,不然可能报错。
接口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; } }
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"; } }
同时我们需要在启动类的上面加上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); } }
<html lang="en" xmlns:th="https://www.thymeleaf.org">
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; } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算