中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

動態(tài)網(wǎng)站開發(fā) 實(shí)訓(xùn)總結(jié)環(huán)球貿(mào)易網(wǎng)

動態(tài)網(wǎng)站開發(fā) 實(shí)訓(xùn)總結(jié),環(huán)球貿(mào)易網(wǎng),品牌策劃與設(shè)計機(jī)構(gòu),平邑網(wǎng)站制作🌹作者主頁:青花鎖 🌹簡介:Java領(lǐng)域優(yōu)質(zhì)創(chuàng)作者🏆、Java微服務(wù)架構(gòu)公號作者😄 🌹簡歷模板、學(xué)習(xí)資料、面試題庫、技術(shù)互助 🌹文末獲取聯(lián)系方式 📝 系列專欄目錄 [Java項(xiàng)…

🌹作者主頁:青花鎖 🌹簡介:Java領(lǐng)域優(yōu)質(zhì)創(chuàng)作者🏆、Java微服務(wù)架構(gòu)公號作者😄
🌹簡歷模板、學(xué)習(xí)資料、面試題庫、技術(shù)互助

🌹文末獲取聯(lián)系方式 📝
在這里插入圖片描述

系列專欄目錄

[Java項(xiàng)目實(shí)戰(zhàn)] 介紹Java組件安裝、使用;手寫框架等
[Aws服務(wù)器實(shí)戰(zhàn)] Aws Linux服務(wù)器上操作nginx、git、JDK、Vue等
[Java微服務(wù)實(shí)戰(zhàn)] Java 微服務(wù)實(shí)戰(zhàn),Spring Cloud Netflix套件、Spring Cloud Alibaba套件、Seata、gateway、shadingjdbc等實(shí)戰(zhàn)操作
[Java基礎(chǔ)篇] Java基礎(chǔ)閑聊,已出HashMap、String、StringBuffer等源碼分析,JVM分析,持續(xù)更新中
[Springboot篇] 從創(chuàng)建Springboot項(xiàng)目,到加載數(shù)據(jù)庫、靜態(tài)資源、輸出RestFul接口、跨越問題解決到統(tǒng)一返回、全局異常處理、Swagger文檔
[Spring MVC篇] 從創(chuàng)建Spring MVC項(xiàng)目,到加載數(shù)據(jù)庫、靜態(tài)資源、輸出RestFul接口、跨越問題解決到統(tǒng)一返回
[華為云服務(wù)器實(shí)戰(zhàn)] 華為云Linux服務(wù)器上操作nginx、git、JDK、Vue等,以及使用寶塔運(yùn)維操作添加Html網(wǎng)頁、部署Springboot項(xiàng)目/Vue項(xiàng)目等
[Java爬蟲] 通過Java+Selenium+GoogleWebDriver 模擬真人網(wǎng)頁操作爬取花瓣網(wǎng)圖片、bing搜索圖片等
[Vue實(shí)戰(zhàn)] 講解Vue3的安裝、環(huán)境配置,基本語法、循環(huán)語句、生命周期、路由設(shè)置、組件、axios交互、Element-ui的使用等
[Spring] 講解Spring(Bean)概念、IOC、AOP、集成jdbcTemplate/redis/事務(wù)等


前言

Apache ShardingSphere 是一款分布式的數(shù)據(jù)庫生態(tài)系統(tǒng), 可以將任意數(shù)據(jù)庫轉(zhuǎn)換為分布式數(shù)據(jù)庫,并通過數(shù)據(jù)分片、彈性伸縮、加密等能力對原有數(shù)據(jù)庫進(jìn)行增強(qiáng)。

Apache ShardingSphere 設(shè)計哲學(xué)為 Database Plus,旨在構(gòu)建異構(gòu)數(shù)據(jù)庫上層的標(biāo)準(zhǔn)和生態(tài)。 它關(guān)注如何充分合理地利用數(shù)據(jù)庫的計算和存儲能力,而并非實(shí)現(xiàn)一個全新的數(shù)據(jù)庫。 它站在數(shù)據(jù)庫的上層視角,關(guān)注它們之間的協(xié)作多于數(shù)據(jù)庫自身。

1、ShardingSphere-JDBC

ShardingSphere-JDBC 定位為輕量級 Java 框架,在 Java 的 JDBC 層提供的額外服務(wù)。

1.1、應(yīng)用場景

Apache ShardingSphere-JDBC 可以通過Java 和 YAML 這 2 種方式進(jìn)行配置,開發(fā)者可根據(jù)場景選擇適合的配置方式。

  • 數(shù)據(jù)庫讀寫分離
  • 數(shù)據(jù)庫分表分庫

1.2、原理

  • Sharding-JDBC中的路由結(jié)果是通過分片字段和分片方法來確定的,如果查詢條件中有 id 字段的情況還好,查詢將會落到某個具體的分片
  • 如果查詢沒有分片的字段,會向所有的db或者是表都會查詢一遍,讓后封裝結(jié)果集給客戶端。
    在這里插入圖片描述

1.3、spring boot整合

1.3.1、添加依賴

<!-- 分表分庫依賴 -->
<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.1.1</version>
</dependency>

1.3.2、添加配置

spring:main:# 一個實(shí)體類對應(yīng)多張表,覆蓋allow-bean-definition-overriding: trueshardingsphere:datasource:ds0:#配置數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動,地址,用戶名和密碼driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/account?autoReconnect=true&allowMultiQueries=truepassword: roottype: com.zaxxer.hikari.HikariDataSourceusername: rootds1:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/account?autoReconnect=true&allowMultiQueries=truepassword: roottype: com.zaxxer.hikari.HikariDataSourceusername: root# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名稱names: ds0,ds1props:sql:show: truesharding:tables:user_info:#指定 user_info 表分布情況,配置表在哪個數(shù)據(jù)庫里面,表名稱都是什么actual-data-nodes: ds0.user_info_${0..9}database-strategy:standard:preciseAlgorithmClassName: com.xxxx.store.account.config.PreciseDBShardingAlgorithmrangeAlgorithmClassName: com.xxxx.store.account.config.RangeDBShardingAlgorithmsharding-column: idtable-strategy:standard:preciseAlgorithmClassName: com.xxxx.store.account.config.PreciseTablesShardingAlgorithmrangeAlgorithmClassName: com.xxxx.store.account.config.RangeTablesShardingAlgorithmsharding-column: id

1.3.3、制定分片算法

1.3.3.1、精確分庫算法
/*** 精確分庫算法*/
public class PreciseDBShardingAlgorithm implements PreciseShardingAlgorithm<Long> {/**** @param availableTargetNames 配置所有的列表* @param preciseShardingValue 分片值* @return*/@Overridepublic String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> preciseShardingValue) {Long value = preciseShardingValue.getValue();//后綴 0,1String postfix = String.valueOf(value % 2);for (String availableTargetName : availableTargetNames) {if(availableTargetName.endsWith(postfix)){return availableTargetName;}}throw new UnsupportedOperationException();}}
1.3.3.2、范圍分庫算法
/*** 范圍分庫算法*/
public class RangeDBShardingAlgorithm implements RangeShardingAlgorithm<Long> {@Overridepublic Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {return collection;}
}
1.3.3.3、精確分表算法
/*** 精確分表算法*/
public class PreciseTablesShardingAlgorithm implements PreciseShardingAlgorithm<Long> {/**** @param availableTargetNames 配置所有的列表* @param preciseShardingValue 分片值* @return*/@Overridepublic String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> preciseShardingValue) {Long value = preciseShardingValue.getValue();//后綴String postfix = String.valueOf(value % 10);for (String availableTargetName : availableTargetNames) {if(availableTargetName.endsWith(postfix)){return availableTargetName;}}throw new UnsupportedOperationException();}}
1.3.3.4、范圍分表算法
/*** 范圍分表算法*/
public class RangeTablesShardingAlgorithm implements RangeShardingAlgorithm<Long> {@Overridepublic Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {Collection<String> result = new ArrayList<>();Range<Long> valueRange = rangeShardingValue.getValueRange();Long start = valueRange.lowerEndpoint();Long end = valueRange.upperEndpoint();Long min = start % 10;Long max = end % 10;for (Long i = min; i < max +1; i++) {Long finalI = i;collection.forEach(e -> {if(e.endsWith(String.valueOf(finalI))){result.add(e);}});}return result;}}

1.3.4、數(shù)據(jù)庫建表

DROP TABLE IF EXISTS `user_info_0`;
CREATE TABLE `user_info_0` (`id` bigint(20) NOT NULL,`account` varchar(255) DEFAULT NULL,`user_name` varchar(255) DEFAULT NULL,`pwd` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1.3.5、業(yè)務(wù)應(yīng)用

1.3.5.1、定義實(shí)體類
@Data
@TableName(value = "user_info")
public class UserInfo {/*** 主鍵*/private Long id;/*** 賬號*/private String account;/*** 用戶名*/private String userName;/*** 密碼*/private String pwd;}
1.3.5.2、定義接口
public interface UserInfoService{/*** 保存* @param userInfo* @return*/public UserInfo saveUserInfo(UserInfo userInfo);public UserInfo getUserInfoById(Long id);public List<UserInfo> listUserInfo();
}
1.3.5.3、實(shí)現(xiàn)類
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {@Override@Transactionalpublic UserInfo saveUserInfo(UserInfo userInfo) {userInfo.setId(IdUtils.getId());this.save(userInfo);return userInfo;}@Overridepublic UserInfo getUserInfoById(Long id) {return this.getById(id);}@Overridepublic List<UserInfo> listUserInfo() {QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();userInfoQueryWrapper.between("id",1623695688380448768L,1623695688380448769L);return this.list(userInfoQueryWrapper);}
}

1.3.6、生成ID - 雪花算法

package com.xxxx.tore.common.utils;import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;/*** 生成各種組件ID*/
public class IdUtils {/*** 雪花算法* @return*/public static long getId(){Snowflake snowflake = IdUtil.getSnowflake(0, 0);long id = snowflake.nextId();return id;}
}

1.4、seata與sharding-jdbc整合

https://github.com/seata/seata-samples/tree/master/springcloud-seata-sharding-jdbc-mybatis-plus-samples

1.4.1、common中添加依賴

<!--seata依賴-->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId><version>2021.0.4.0</version>
</dependency>
<!-- sharding-jdbc整合seata分布式事務(wù)-->
<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-transaction-base-seata-at</artifactId><version>4.1.1</version>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2021.0.4.0</version><exclusions><exclusion><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>1.4.2</version>
</dependency>

1.4.2、改造account-service服務(wù)

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {@Autowiredprivate OrderService orderService;@Autowiredprivate StorageService storageService;/*** 存放商品編碼及其對應(yīng)的價錢*/private static Map<String,Integer> map = new HashMap<>();static {map.put("c001",3);map.put("c002",5);map.put("c003",10);map.put("c004",6);}@Override@Transactional@ShardingTransactionType(TransactionType.BASE)public void debit(OrderDTO orderDTO) {//扣減賬戶余額int calculate = this.calculate(orderDTO.getCommodityCode(), orderDTO.getCount());AccountDTO accountDTO = new AccountDTO(orderDTO.getUserId(), calculate);QueryWrapper<Account> objectQueryWrapper = new QueryWrapper<>();objectQueryWrapper.eq("id",1);objectQueryWrapper.eq(accountDTO.getUserId() != null,"user_id",accountDTO.getUserId());Account account = this.getOne(objectQueryWrapper);account.setMoney(account.getMoney() - accountDTO.getMoney());this.saveOrUpdate(account);//扣減庫存this.storageService.deduct(new StorageDTO(null,orderDTO.getCommodityCode(),orderDTO.getCount()));//生成訂單this.orderService.create(orderDTO);      }/*** 計算購買商品的總價錢* @param commodityCode* @param orderCount* @return*/private int calculate(String commodityCode, int orderCount){//商品價錢Integer price = map.get(commodityCode) == null ? 0 : map.get(commodityCode);return price * orderCount;}
}

注意:調(diào)單生成調(diào)用的邏輯修改,減余額->減庫存->生成訂單。調(diào)用入口方法注解加上:@ShardingTransactionType(TransactionType.BASE)

1.4.3、修改business-service服務(wù)

@Service
public class BusinessServiceImpl implements BusinessService {@Autowiredprivate OrderService orderService;@Autowiredprivate StorageService storageService;@Autowiredprivate AccountService accountService;@Overridepublic void purchase(OrderDTO orderDTO) {//扣減賬號中的錢accountService.debit(orderDTO);        }
}

1.4.4、修改order-service服務(wù)

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements OrderService {/*** 存放商品編碼及其對應(yīng)的價錢*/private static Map<String,Integer> map = new HashMap<>();static {map.put("c001",3);map.put("c002",5);map.put("c003",10);map.put("c004",6);}@Override@Transactional@ShardingTransactionType(TransactionType.BASE)public Order create(String userId, String commodityCode, int orderCount) {int orderMoney = calculate(commodityCode, orderCount);Order order = new Order();order.setUserId(userId);order.setCommodityCode(commodityCode);order.setCount(orderCount);order.setMoney(orderMoney);//保存訂單this.save(order);try {TimeUnit.SECONDS.sleep(30);} catch (InterruptedException e) {e.printStackTrace();}if(true){throw new RuntimeException("回滾測試");}return order;}/*** 計算購買商品的總價錢* @param commodityCode* @param orderCount* @return*/private int calculate(String commodityCode, int orderCount){//商品價錢Integer price = map.get(commodityCode) == null ? 0 : map.get(commodityCode);return price * orderCount;}
}

1.4.5、配置文件參考

server:port: 8090spring:main:# 一個實(shí)體類對應(yīng)多張表,覆蓋allow-bean-definition-overriding: trueshardingsphere:datasource:ds0:#配置數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動,地址,用戶名和密碼driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/account?autoReconnect=true&allowMultiQueries=truepassword: roottype: com.zaxxer.hikari.HikariDataSourceusername: rootds1:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/account?autoReconnect=true&allowMultiQueries=truepassword: roottype: com.zaxxer.hikari.HikariDataSourceusername: root# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名稱names: ds0,ds1props:sql:show: truesharding:tables:account_tbl:actual-data-nodes: ds0.account_tbl_${0..1}database-strategy:standard:preciseAlgorithmClassName: com.xxxx.store.account.config.PreciseDBExtShardingAlgorithm#rangeAlgorithmClassName: com.xxxx.store.account.config.RangeDBShardingAlgorithmsharding-column: idtable-strategy:standard:preciseAlgorithmClassName: com.xxxx.store.account.config.PreciseTablesExtShardingAlgorithm#rangeAlgorithmClassName: com.xxxx.store.account.config.RangeTablesShardingAlgorithmsharding-column: iduser_info:#指定 user_info 表分布情況,配置表在哪個數(shù)據(jù)庫里面,表名稱都是什么actual-data-nodes: ds0.user_info_${0..9}database-strategy:standard:preciseAlgorithmClassName: com.xxxx.store.account.config.PreciseDBShardingAlgorithmrangeAlgorithmClassName: com.xxxx.store.account.config.RangeDBShardingAlgorithmsharding-column: idtable-strategy:standard:preciseAlgorithmClassName: com.xxxx.store.account.config.PreciseTablesShardingAlgorithmrangeAlgorithmClassName: com.xxxx.store.account.config.RangeTablesShardingAlgorithmsharding-column: id#以上是sharding-jdbc配置cloud:nacos:discovery:server-addr: localhost:8848namespace: 1ff3782d-b62d-402f-8bc4-ebcf40254d0aapplication:name: account-service  #微服務(wù)名稱
#  datasource:
#    username: root
#    password: root
#    url: jdbc:mysql://127.0.0.1:3306/account
#    driver-class-name: com.mysql.cj.jdbc.Driverseata:enabled: trueenable-auto-data-source-proxy: falseapplication-id: account-servicetx-service-group: default_tx_groupservice:vgroup-mapping:default_tx_group: defaultdisable-global-transaction: falseregistry:type: nacosnacos:application: seata-serverserver-addr: 127.0.0.1:8848namespace: 1ff3782d-b62d-402f-8bc4-ebcf40254d0agroup: SEATA_GROUPusername: nacospassword: nacosconfig:nacos:server-addr: 127.0.0.1:8848namespace: 1ff3782d-b62d-402f-8bc4-ebcf40254d0agroup: SEATA_GROUPusername: nacospassword: nacos

聯(lián)系方式

微信公眾號:Java微服務(wù)架構(gòu)

在這里插入圖片描述

系列文章目錄

第一章 Java線程池技術(shù)應(yīng)用
第二章 CountDownLatch和Semaphone的應(yīng)用
第三章 Spring Cloud 簡介
第四章 Spring Cloud Netflix 之 Eureka
第五章 Spring Cloud Netflix 之 Ribbon
第六章 Spring Cloud 之 OpenFeign
第七章 Spring Cloud 之 GateWay
第八章 Spring Cloud Netflix 之 Hystrix
第九章 代碼管理gitlab 使用
第十章 SpringCloud Alibaba 之 Nacos discovery
第十一章 SpringCloud Alibaba 之 Nacos Config
第十二章 Spring Cloud Alibaba 之 Sentinel
第十三章 JWT
第十四章 RabbitMQ應(yīng)用
第十五章 RabbitMQ 延遲隊(duì)列
第十六章 spring-cloud-stream
第十七章 Windows系統(tǒng)安裝Redis、配置環(huán)境變量
第十八章 查看、修改Redis配置,介紹Redis類型
第十九章 ShardingSphere - ShardingSphere-JDBC

http://www.risenshineclean.com/news/64108.html

相關(guān)文章:

  • 解除網(wǎng)站被攔截的方法快手流量推廣免費(fèi)網(wǎng)站
  • 合肥建網(wǎng)站公司騰訊與中國聯(lián)通
  • 各大網(wǎng)站提交入口怎么聯(lián)系地推公司
  • 西安火車站網(wǎng)站建設(shè)安卓優(yōu)化大師官方下載
  • 頂級裝修公司百度小程序優(yōu)化
  • 莆田網(wǎng)站建設(shè)技術(shù)托管南陽網(wǎng)站seo
  • 開發(fā)公司屬于什么行業(yè)網(wǎng)站優(yōu)化 seo和sem
  • 中國專業(yè)的網(wǎng)站建設(shè)12月10日新聞
  • 義烏多語言網(wǎng)站建設(shè)seo網(wǎng)絡(luò)推廣技術(shù)
  • 做五金生意什么網(wǎng)站做比較好精準(zhǔn)營銷的三要素
  • 網(wǎng)站首頁的模塊布局杭州百度快速排名提升
  • 做競猜網(wǎng)站合法嗎如何快速提升網(wǎng)站關(guān)鍵詞排名
  • 哪個平臺查企業(yè)免費(fèi)谷歌aso優(yōu)化
  • 專業(yè)做網(wǎng)站公司哪家好一句簡短走心文案
  • 個人做理財網(wǎng)站好上海seo關(guān)鍵詞優(yōu)化
  • ppt中仿網(wǎng)站鏈接怎么做b站引流推廣網(wǎng)站
  • 做網(wǎng)站的步驟流程seo外包杭州
  • 網(wǎng)站建設(shè)全套流程廣州seo全網(wǎng)營銷
  • 網(wǎng)站建設(shè)中的咨詢服務(wù)北京網(wǎng)絡(luò)推廣
  • 網(wǎng)站建設(shè)色系搭配種子資源
  • 長沙畢業(yè)設(shè)計代做網(wǎng)站價格網(wǎng)域名查詢地址
  • 如何做微商城網(wǎng)站建設(shè)國內(nèi)推廣平臺
  • 網(wǎng)站開發(fā)主流技術(shù)線路介紹網(wǎng)站制作論文
  • 專業(yè)設(shè)計企業(yè)網(wǎng)站中國體育新聞
  • 一個公司做兩個網(wǎng)站的好處我想接app純注冊推廣單
  • 網(wǎng)站前臺建設(shè)用到哪些工具百度app手機(jī)版
  • 政府網(wǎng)站建設(shè)要求有哪些口紅的推廣軟文
  • 地產(chǎn)公司做網(wǎng)站維護(hù)寫代碼么6廣州seo公司官網(wǎng)
  • 用DW做的網(wǎng)站怎么分享給別人怎樣做線上銷售
  • 網(wǎng)站建設(shè)服務(wù)宗旨代寫文章質(zhì)量高的平臺