Rest微服务案例

news/2024/10/21 11:59:32/

Rest

  • 父工程构建
    • microservicecloud-api公共子模块Module
    • microservicecloud-provider-dept-8001部门微服务提供者Module
    • microservicecloud-consumer-dept-80部门微服务消费者Module

以Dept部门模块做一个微服务通用案例
Consumer消费者(Client)通过REST调用Provider提供者(Server)提供的服务;

分模块架构
一个Project带着多个Module子模块
MicroServiceCloud父工程(Project)下初次带着三个子模块(Module)

在这里插入图片描述

下图中,如果entity在1.2\1.3\1.4\1.5子模块中都要用到,可以在api公共模块中定义entity,让剩余模块直接调用,不用再在每个模块里都写一遍entity
在这里插入图片描述

微服务化的核心就是将传统的一站式应用,根据业务拆分成一个一个的服务,彻底地去耦合,每一个微服务提供单个业务功能的服务,一个服务做一件事,从技术角度看就是一种小而独立的处理过程,类似进程概念,能够自行单独启动或销毁,拥有自己独立的数据库。

父工程构建

在这里插入图片描述

microservicecloud-api公共子模块Module

在这里插入图片描述
部门dept实体类

@Data
@NoArgsConstructor
@Accessors(chain = true) //chain为true,说明它支持链式写法
//所有实体类务必序列化,方便在网络传输
public class Dept implements Serializable {private Long deptno;    //主键private String dname;   //部门名称private String db_source;   //来自哪个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库public Dept(String dname) {this.dname = dname;}/*链式写法:Dept dept = Dept();dept.setDeptNo(11).setDname("ss").setDb_source("db1");*/
}

microservicecloud-provider-dept-8001部门微服务提供者Module

在这里插入图片描述
在这里插入图片描述
application.yml

server:port: 8001
mybatis:type-aliases-package: com.cht.springcloud.pojo          # 所有entity别名类所在包config-location: classpath:mybatis/mybatis-config.xml   # mybatis配置文件所在路径mapper-locations: classpath:mybatis/mapper/*.xml        #mapper映射文件
spring:application:name: springcloud-provider-deptdatasource:type: com.alibaba.druid.pool.DruidDataSource          # 当前数据源操作类型driver-class-name: org.gjt.mm.mysql.Driver            # mysql驱动包url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8 # 数据库名称username: rootpassword: root

mybatis配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><!--开启二级缓存--><setting name="cacheEnabled" value="true"/></settings>
</configuration>

Dao层——DeptDao

@Mapper
@Repository
public interface DeptDao {boolean addDept(Dept dept);Dept queryById(Long id);List<Dept> queryAll();
}

Dao的实现mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cht.springcloud.dao.DeptDao"><insert id="addDept" parameterType="Dept">insert into dept(dname,db_source) values(#{dname},DATABASE());</insert><select id="queryById" resultType="Dept" parameterType="Long">select * from dept where deptno = #{deptno}</select><select id="queryAll" resultType="Dept" >select * from dept</select>
</mapper>

Service层及其实现类Impl

public interface DeptService {boolean addDept(Dept dept);Dept queryById(Long id);List<Dept> queryAll();
}
@Service
public class DeptServiceImpl implements DeptService {@Autowiredprivate DeptDao deptDao;@Overridepublic boolean addDept(Dept dept) {return deptDao.addDept(dept);}@Overridepublic Dept queryById(Long id) {return deptDao.queryById(id);}@Overridepublic List<Dept> queryAll() {return deptDao.queryAll();}
}

Controller

//提供restful服务
@RestController
public class DeptController {@Autowiredprivate DeptService deptService;@PostMapping("/dept/add")public boolean addDept(Dept dept){return deptService.addDept(dept);}@GetMapping("/dept/get/{id}")public Dept queryById(@PathVariable("id") Long id){return deptService.queryById(id);}@GetMapping("/dept/list")public List<Dept> queryAll(){return deptService.queryAll();}

microservicecloud-consumer-dept-80部门微服务消费者Module

在这里插入图片描述
在这里插入图片描述
ConfigBean

@Configuration  //boot优化了spring  spring用的配置文件叫applicationContext.xml 现在用的是注解版的配置 @Configuration配置
public class ConfigBean {@Bean@LoadBalanced //这样你再用RestTemplate去访问的时候就自带负载均衡了public RestTemplate getRestTemplate(){return new RestTemplate();}}
// applicationContext.xml == ConfigBean(@Configuration)
// <bean id = "userService" class = "com.atguigu.tmall.UserServiceImpl"> 原本xml中的写法

在这里插入图片描述
在这里插入图片描述
消费者Controller,只消费,所以没有service,通过rest调用8001模块的controller

@RestController
public class DeptConsumerController {//消费者:不应该有service层//RestTemplate .... 供我们直接调用就可以了! 注册到Spring中//参数可以通过map,实体,url传过去@Autowiredprivate RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法,简单的restful风格服务/*** 服务提供方地址前缀* 这里的地址,应该是一个变量,通过服务名来访问*/private static final String REST_URL_PREFIX = "http://localhost:8001";@RequestMapping("/consumer/dept/get/{id}")public Dept get(@PathVariable("id") Long id) {// getForObject(服务提供方地址(接口),返回类型.class) 其中的get表示Get请求,因为也有postForObject/*ForObject表示拿到一个对象*/return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);}@RequestMapping("/consumer/dept/add")public boolean add(Dept dept) {// postForObject(服务提供方地址(接口),参数实体,返回类型.class)//可以用http://localhost/consumer/dept/add?dname=小葵,完成添加,只不过dname在数据库没有显示出来,但记录增加了return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);}@RequestMapping("/consumer/dept/list")public List<Dept> list() {return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);}

http://www.ppmy.cn/news/1440500.html

相关文章

PHP定期给自己网站目录做个特征镜像供快速对比

效果图 上代码&#xff1a; <style> h1{font-size:24px;line-height:180%;font-weight:600;margin:1px 2px;color:#0180cf;} h2{font-size:20px;line-height:140%;font-weight:600;margin:2px 4px;color:green;} h3{font-size:16px;line-height:140%;font-weight:600;m…

数据结构 - 队列 [动画+代码注释超详解],萌新轻松上手!!!

一. 队列的概念 队列是一种特殊的线性表&#xff0c;用于存储元素&#xff0c;并且按照先进先出(First In First Out)的顺序进行管理&#xff0c;这意味着最先加入队列的元素将会是最先从队列中被移除的元素 队列的原型&#xff1a;只允许在一端进行插入数据的操作&#xff0c…

Redis面试题二(数据存储)

目录 1.redis 的数据过期策略 1. 惰性删除&#xff08;Lazy Expiration&#xff09; 2. 定期删除&#xff08;Periodic Expiration&#xff09; 3. 定时删除&#xff08;Timing-Based Expiration&#xff09; 实际应用中的组合策略 2.redis 有哪些内存淘汰机制 volatile&…

prompt炼金:ChatGPT在文献综述中100+类高阶提示词应用

点击下方▼▼▼▼链接直达AIPaperPass &#xff01; AIPaperPass - AI论文写作指导平台 近期小编沉迷总结ChatGPT提示词&#xff0c;从之前涵盖全流程的数百条提示词到今天一步一步精炼每个流程中宝子们可能用的上的提示词。今天分享给大家文献综述相关提示词技巧。 如何提升你…

鸿蒙云函数调试坑点

如果你要本地调试请使用 const {payload, action} event.body/** 本地调试不需要序列化远程需要序列化 */ // const {payload, action} JSON.parse(event.body) const {payload, action} event.body 注意: 只要修改云函数&#xff0c;必须上传云函数 如果使用 const {pay…

江开2024年春《计算机组成原理 060214》第4次计分作业参考答案

答案&#xff1a;更多答案&#xff0c;请关注【电大搜题】微信公众号 答案&#xff1a;更多答案&#xff0c;请关注【电大搜题】微信公众号 答案&#xff1a;更多答案&#xff0c;请关注【电大搜题】微信公众号 单选题 1某计算机字长32位&#xff0c;其存储容量为4GB&am…

小红书笔记的规则权重算法7个要点

1.笔记原创度 小红书平台非常重视用户创作的独特性和原创性。因此&#xff0c;在评估笔记的权重时&#xff0c;原创度是一个重要的考量因素。用户可以通过提供独特的观点、个人经验和创意内容来提高笔记的原创度。 2.笔记内容是否违规 小红书作为一个社区平台&#xff0c;对用户…

机器视觉检测技术是什么?突出的亮点有哪些?

机器视觉检测技术是一种利用图像处理软件分析处理相机捕捉到的图像&#xff0c;以替代人眼在制造和测试过程中进行检测和测量的技术。它广泛应用于工业自动化领域&#xff0c;可以提高生产效率和质量控制的精度。机器视觉系统通常包括光源、相机、图像处理单元和执行机构等组成…