Rest微服务案例

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.hqwc.cn/news/651330.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

react项目发布后,浏览器源码泄露的解决方案

在使用create-react-app时&#xff0c;打包生产环境npm run build&#xff0c;浏览器打开后仍然是可以看到源码的。源码都没上传&#xff0c;为啥线上能看到源码 。 例&#xff1a;线上与服务器 线上与源码 react-scripts build和npm run build 有什么不同 react-scripts bui…

PID算法学习

PID算法介绍 在过程控制中&#xff0c;按偏差的比例&#xff08;P&#xff09;、积分&#xff08;I&#xff09;和微分&#xff08;D&#xff09;进行控制的PID控制器&#xff08;亦称PID调节器&#xff09;是应用最为广泛的一种自动控制器。它具有原理简单&#xff0c;易于实…

嵌入式Linux学习——Ubantu初体验

Ubuntu 和Windows 的最大差别 Windows中的每一个分区都对应着一个盘符&#xff0c;盘符下可以存放目录与文件&#xff0c;而在Ubantu中没有盘符的概念&#xff0c;只有目录结构。实际上不同的目录可能挂载在不同的分区之下&#xff0c;如果想要查看当前目录位于磁盘的哪个分区…

【java数据结构-优先级队列向下调整Topk问题,堆的常用的接口详解】

&#x1f308;个人主页&#xff1a;努力学编程’ ⛅个人推荐&#xff1a;基于java提供的ArrayList实现的扑克牌游戏 |C贪吃蛇详解 ⚡学好数据结构&#xff0c;刷题刻不容缓&#xff1a;点击一起刷题 &#x1f319;心灵鸡汤&#xff1a;总有人要赢&#xff0c;为什么不能是我呢 …

递归的层序遍历

最近遇到一个业务需求&#xff1a;一颗依赖树&#xff0c;其实就是一颗递归树&#xff0c;如何一层一层的数据放在一起&#xff0c;可以近似理解为二叉树的层序遍历。 业务理解为递归树的层序遍历 代码示例&#xff1a; public class RecursionErgodic {public static void…

使用Kimi的一些体会

1、https://kimi.cn 这个回答问题还比较专业&#xff0c;感觉比以前chatgpt要好一些 2、Moonshot AI - 开放平台 可以通过注册账号&#xff0c;或微信扫描就可以登录进去 通过postman可以体会一下功能 2.1 POST https://api.moonshot.cn/v1/chat/completions 2.2 授权选择下…

达梦(DM) SQL日期操作及分析函数

达梦DM SQL日期操作及分析函数 日期操作SYSDATEEXTRACT判断一年是否为闰年周的计算确定某月内第一个和最后一个周末某天的日期确定指定年份季度的开始日期和结束日期补充范围内丢失的值按照给定的时间单位查找使用日期的特殊部分比较记录 范围处理分析函数定位连续值的范围查找…

docker部署通义千问-7B-Chat的openai-api环境

服务器环境&#xff1a; 显卡驱动&#xff1a;Driver Version: 530.30.02 CUDA版本&#xff1a;CUDA Version: 12.1 显卡&#xff1a;NVIDIA GeForce RTX 3090共4张 注意&#xff1a;最好把显卡驱动升级到530&#xff0c;CUDA版本之前使用11.7有问题。 一、下载模型文件 …

环境配置——Windows平台配置VScode运行环境为远程服务器或虚拟机

1. 远程机需要先安装SSH服务&#xff0c;命令如下 sudo apt install openssh-server 2. 安装好后需要开启SSH服务&#xff1a; sudo service sshd start 3. 查看SSH服务是否有被开启&#xff1a; sudo systemctl status sshd.service 4. 本地Windows需要生成密钥将公钥放…

茴香豆:搭建你的RAG智能助理-笔记三

本次课程由书生浦语社区贡献者【北辰】老师讲解【茴香豆&#xff1a;搭建你的 RAG 智能助理】课程 课程视频&#xff1a;https://www.bilibili.com/video/BV1QA4m1F7t4/ 课程文档&#xff1a;Tutorial/huixiangdou/readme.md at camp2 InternLM/Tutorial GitHub 该课程&…

贪吃蛇撞墙功能的实现 和自动行走刷新地图 -- 第三十天

1.撞墙 1.1最初的头和尾指针要置为空&#xff0c;不然是野指针 1.2 在增加和删除节点后&#xff0c;判断是否撞墙&#xff0c;撞墙则初始话蛇 1.3在撞墙后初始化蛇&#xff0c;如果头不为空就撞墙&#xff0c;得定义临时指针指向头&#xff0c;释放头节点 2.自动刷新地图 2.1…

解决问题:TypeError:unsupported operand type(s) for -: ‘float‘ and ‘decimal.Decimal‘

文章目录 一、现象二、解决方案 一、现象 用Pandas 处理数据的时候&#xff0c;想得到增长率&#xff0c;没想到翻车了&#xff1f; import pandas as pddf pd.read_csv(data.csv)df[增长率] ((df[今年] - df[去年]) / (df[今年]))执行一下语句发现报错 TypeError&#xf…