springboot集成ssm,dubbo,redis的步骤

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;
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot使用“习惯优于配置”的理念让我们的项目快速运行起来,我们可以不用或者只需要很少的配置就能创建一个独立运行、准生产级别的基于Spring框架的项目。我们不禁要问,这么一个优秀的框架,是不是在企业开发中就已经足够了,如果是,那么为什么像BAT这些大公司还要研发自己的交易框架,当然这里面除了核心技术之外,还有两个比较重要的原因:第一:像SpringSpring Boot这些开源框架固然很优秀,但却不满足这些大公司对框架的功能要求,如spring scheduler就没有分布式调度能力,阿里研发了自己的tbschedule,以及后来的schedulerx;第二:开源框架可以解决具体的领域问题,比如持久化框架Mybatis,RPC框架Dubbo,但是面对业务流程的开发却不是它的强项,以此就诞生了SSM,以及后来的Spring MVC。放眼整个java开源世界,不管是功能问题还是业务流程开发问题都有对应框架和组件能满足我们的需求,只要我们的视野足够开阔,能有效的去整合开源组件,足以应付日常的开发。当然我们很难写出像SpringSpring BootMybatis这些优秀的框架,但是我们可以在这个基础之上,进行整合,甚至二次开发,形成公司自己的功能组件或者交易开发框架。不客气的说,开源框架的底层少不了spring的身影,那么可以肯定在Spring Boot推出以后,开源框架势必会以Spring Boot作为底层平台进行二次改造,这是趋势,也是必然。本课程顺应潮流,以Spring Boot作为基础平台,充分发挥其特性,抽象业务流程,整合开源组件,降低开发难度,打造出一个功能强大的交易开发框架,简洁,优雅,好用。本课程有如下技术特色:第一:充分使用Spring Boot的自动装配、条件注解,以及各种使用技巧;第二:使用注解@Transaction抽象业务流程,简化交易的定义和执行方式,比SpringMVC更符合业务流程的开发(当然SpringMVC很强大,无贬低之意)第三:为使交易具备RPC能力,使用泛化方式集成Dubbo,其好处是服务端不再需要提供接口给客户端使用,简单、高效;第四:使用nacos作为服务注册中心,也支持zookeeper;第五:为使交易具备Http能力,在Spring MVC的基础上提供HandlerMapping、HandlerAdapter。。。。一切尽在代码中

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值