华为网站搭建,wordpress搜索插件,拍摄宣传片费用明细,兰州微网站建设提示#xff1a;文章写完后#xff0c;目录可以自动生成#xff0c;如何生成可参考右边的帮助文档 文章目录 微服务微服务架构#xff0c;是服务化思想指导下的一套最佳实践架构方案。服务化#xff0c;就是把单体架构中的功能模块拆分为多个独立项目。 单体架构微服务架构… 提示文章写完后目录可以自动生成如何生成可参考右边的帮助文档 文章目录 微服务微服务架构是服务化思想指导下的一套最佳实践架构方案。服务化就是把单体架构中的功能模块拆分为多个独立项目。 单体架构微服务架构SpringCloud 服务拆分原则商城项目工程结构有两种 服务调用RestTemplate远程调用小结Java发送http请求可以使用Spring提供的RestTemplate使用的基本步骤如下HttpClient 微服务 微服务架构是服务化思想指导下的一套最佳实践架构方案。服务化就是把单体架构中的功能模块拆分为多个独立项目。 单体架构 微服务架构
单一职责一个微服务负责一部分业务功能并且其核心数据不依赖于其它模块。团队自治每个微服务都有自己独立的开发、测试、发布、运维人员团队人员规模不超过10人2张披萨能喂饱服务自治每个微服务都独立打包部署访问自己独立的数据库。并且要做好服务隔离避免对其它服务产生影响 SpringCloud
微服务拆分以后碰到的各种问题都有对应的解决方案和微服务组件而SpringCloud框架可以说是目前Java领域最全面的微服务组件的集合了。 .
服务拆分原则 所谓纵向拆分就是按照项目的功能模块来拆分。例如黑马商城中就有用户管理功能、订单管理功能、购物车功能、商品管理功能、支付功能等。那么按照功能模块将他们拆分为一个个服务就属于纵向拆分。这种拆分模式可以尽可能提高服务的内聚性。
而横向拆分是看各个功能模块之间有没有公共的业务部分如果有将其抽取出来作为通用服务。例如用户登录是需要发送消息通知记录风控数据下单时也要发送短信记录风控数据。因此消息发送、风控数据记录就是通用的业务功能因此可以将他们分别抽取为公共服务消息中心服务、风控管理服务。这样可以提高业务的复用性避免重复开发。同时通用业务一般接口稳定性较强也不会使服务之间过分耦合。
商城项目 工程结构有两种 独立ProjectMaven聚合 商城并不是一个完整的项目其中的短信发送、风控管理并没有实现这里就不再考虑了。而其它的业务按照纵向拆分可以分为以下几个微服务
用户服务商品服务订单服务购物车服务支付服务
服务调用
在拆分的时候我们发现一个问题就是购物车业务中需要查询商品信息但商品信息查询的逻辑全部迁移到了item-service服务导致我们无法查询。
最终结果就是查询到的购物车数据不完整因此要想解决这个问题我们就必须改造其中的代码把原本本地方法调用改造成跨微服务的远程调用RPC即Remote Produce Call。
因此现在查询购物车列表的流程变成了这样 代码中需要变化的就是这一步 RestTemplate
Spring给我们提供了一个RestTemplate的API可以方便的实现Http请求的发送。 其中提供了大量的方法方便我们发送Http请求例如 可以看到常见的Get、Post、Put、Delete请求都支持如果请求参数比较复杂还可以使用exchange方法来构造请求。
我们在cart-service服务中定义一个配置类先将RestTemplate注册为一个Bean
package com.hmall.cart.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;Configuration
public class RemoteCallConfig {Beanpublic RestTemplate restTemplate() {return new RestTemplate();}
}远程调用
接下来我们修改cart-service中的com.hmall.cart.service.impl.CartServiceImpl的handleCartItems方法发送http请求到item-service 可以看到利用RestTemplate发送http请求与前端ajax发送请求非常相似都包含四部分信息
① 请求方式② 请求路径③ 请求参数④ 返回值类型
handleCartItems方法的完整代码如下
private void handleCartItems(ListCartVO vos) {// TODO 1.获取商品idSetLong itemIds vos.stream().map(CartVO::getItemId).collect(Collectors.toSet());// 2.查询商品// ListItemDTO items itemService.queryItemByIds(itemIds);// 2.1.利用RestTemplate发起http请求得到http的响应ResponseEntityListItemDTO response restTemplate.exchange(http://localhost:8081/items?ids{ids},HttpMethod.GET,null,new ParameterizedTypeReferenceListItemDTO() {},Map.of(ids, CollUtil.join(itemIds, ,)));// 2.2.解析响应if(!response.getStatusCode().is2xxSuccessful()){// 查询失败直接结束return;}ListItemDTO items response.getBody();if (CollUtils.isEmpty(items)) {return;}// 3.转为 id 到 item的mapMapLong, ItemDTO itemMap items.stream().collect(Collectors.toMap(ItemDTO::getId, Function.identity()));// 4.写入vofor (CartVO v : vos) {ItemDTO item itemMap.get(v.getItemId());if (item null) {continue;}v.setNewPrice(item.getPrice());v.setStatus(item.getStatus());v.setStock(item.getStock());}
}在这个过程中item-service提供了查询接口cart-service利用Http请求调用该接口。因此item-service可以称为服务的提供者而cart-service则称为服务的消费者或服务调用者。
小结
服务拆分之后不可避免的会出现跨微服务的业务此时微服务之间就需要进行远程调用。微服务之间的远程调用被称为RPC即远程过程调用。RPC的实现方式有很多比如
基于Http协议基于Dubbo协议
我们上述代码使用的是Http方式这种方式不关心服务提供者的具体技术实现只要对外暴露Http接口即可更符合微服务的需要。
Java发送http请求可以使用Spring提供的RestTemplate使用的基本步骤如下
注册RestTemplate到Spring容器调用RestTemplate的API发送请求常见方法有 getForObject发送Get请求并返回指定类型对象PostForObject发送Post请求并返回指定类型对象put发送PUT请求delete发送Delete请求exchange发送任意类型请求返回ResponseEntity
HttpClient HttpClient–01–简介 文章转载自: http://www.morning.gcftl.cn.gov.cn.gcftl.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.xrksf.cn.gov.cn.xrksf.cn http://www.morning.gmysq.cn.gov.cn.gmysq.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.tkrdg.cn.gov.cn.tkrdg.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.aswev.com.gov.cn.aswev.com http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.qpzjh.cn.gov.cn.qpzjh.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.yltyz.cn.gov.cn.yltyz.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.bmsqq.cn.gov.cn.bmsqq.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.glxmf.cn.gov.cn.glxmf.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.zlrrj.cn.gov.cn.zlrrj.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.ndcf.cn.gov.cn.ndcf.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.yxplz.cn.gov.cn.yxplz.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.webife.com.gov.cn.webife.com http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.hcsnk.cn.gov.cn.hcsnk.cn http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.xczyj.cn.gov.cn.xczyj.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.mtsgx.cn.gov.cn.mtsgx.cn http://www.morning.qsy41.cn.gov.cn.qsy41.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.gjtdp.cn.gov.cn.gjtdp.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.gwzfj.cn.gov.cn.gwzfj.cn http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.mgkcz.cn.gov.cn.mgkcz.cn http://www.morning.rbrd.cn.gov.cn.rbrd.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn