Alt_Shift

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

什么是分布式和分布式?

在起初的单体项目中,前端,后端,mysql等都放在一个war包里面进行操作,耦合性高复用率差可拓展性差,随着时代的改变将mysql,前端,后端进行分离,分别放到不同的机器上面去

但是如果其中一台机器宕机的话项目也会宕掉,所以集群的概念.

然后随着后端业务代码越来越繁琐,服务越来越多,又出现了不同粒度划分出来的微服务概念,将后端业务之间可以相互独立的业务根据不同粒度进行抽离,抽离出来的微服务项目.

所以微服务是针对后端服务来说的.

 

分布式事务

https://www.cnblogs.com/savorboard/p/distributed-system-transaction-consistency.html

 

分布式索引

1.雪花算法 时间戳 + 机器号 + 补齐 能达到每秒创建4096个Id;

2.数据库  数据库中存储一定量的索引,每次分发取出一些索引进行分发

 

1.zookeeper 和 curator对应版本

http://curator.apache.org/getting-started.html

官方指定的版本,需要查找对应依赖并引入。

 
//对应zk版本3.4.14
<dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
 </dependency>

 

2.Curator配置

package com.zdj.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ Author     :fqg
 * @ Date       :Created in 11:52 2020/11/13
 */
@Configuration
public class CuratorConfig {
    @Bean(initMethod = "start")
    CuratorFramework curatorFramework(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.127.1 : 2181", retryPolicy);
        return client;
    }
}
View Code

3.使用场景

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    //注入Curator客户端
    @Autowired
    private CuratorFramework curatorFramework;

    @RequestMapping("/reduce/stock")
    public R reduceStock(Integer keyId) throws Exception {
        //创建互斥锁
        InterProcessMutex interProcessMutex = new InterProcessMutex(curatorFramework, "/book" + keyId);
        try {
            //加锁
            interProcessMutex.acquire();
            bookService.reduceStock(keyId);
        } catch (Exception e) {
            if(e instanceof RuntimeException){
                throw e;
            }
        }finally {
            //解锁
            interProcessMutex.release();
        }
        return R.ok("");
    }
}
  

 

posted on 2020-11-13 14:19  Alt_Shift  阅读(96)  评论(0编辑  收藏  举报