40分钟学 Go 语言高并发:负载均衡与服务治理

负载均衡与服务治理

一、知识要点总览

模块核心内容技术实现难度
负载策略轮询、权重、最小连接数自定义负载均衡器
服务降级服务降级、熔断降级、限流降级Hystrix模式
熔断机制熔断器状态机、失败计数、自动恢复Circuit Breaker
限流设计令牌桶、滑动窗口、计数器Rate Limiter

让我们开始具体实现:

1. 负载均衡实现

// loadbalancer/balancer.go
package loadbalancerimport ("sync""sync/atomic""time"
)// 服务实例
type Instance struct {ID           stringHost         stringPort         intWeight       intActive       boolLastActive   time.TimeConnections  int64  // 当前连接数FailCount    int64  // 失败计数
}// 负载均衡器接口
type LoadBalancer interface {Select() (*Instance, error)UpdateInstances(instances []*Instance)MarkSuccess(instance *Instance)MarkFailed(instance *Instance)
}// 轮询负载均衡器
type RoundRobinBalancer struct {instances []*Instancecounter   uint64mu        sync.RWMutex
}func NewRoundRobinBalancer() *RoundRobinBalancer {return &RoundRobinBalancer{instances: make([]*Instance, 0),}
}func (b *RoundRobinBalancer) Select() (*Instance, error) {b.mu.RLock()defer b.mu.RUnlock()if len(b.instances) == 0 {return nil, ErrNoAvailableInstances}// 获取当前计数count := atomic.AddUint64(&b.counter, 1)index := int(count % uint64(len(b.instances)))return b.instances[index], nil
}// 加权轮询负载均衡器
type WeightedRoundRobinBalancer struct {instances     []*Instanceweights       []intcurrentWeight intmu           sync.RWMutex
}func NewWeightedRoundRobinBalancer() *WeightedRoundRobinBalancer {return &WeightedRoundRobinBalancer{instances: make([]*Instance, 0),weights:   make([]int, 0),}
}func (b *WeightedRoundRobinBalancer) Select() (*Instance, error) {b.mu.Lock()defer b.mu.Unlock()if len(b.instances) == 0 {return nil, ErrNoAvailableInstances}totalWeight := 0var best *InstancebestWeight := -1for i, instance := range b.instances {if !instance.Active {continue}b.weights[i] += instance.WeighttotalWeight += instance.Weightif bestWeight < b.weights[i] {bestWeight = b.weights[i]best = instance}}if best == nil {return nil, ErrNoAvailableInstances}for i := range b.weights {b.weights[i] -= totalWeight}return best, nil
}// 最小连接数负载均衡器
type LeastConnectionBalancer struct {instances []*Instancemu        sync.RWMutex
}func NewLeastConnectionBalancer() *LeastConnectionBalancer {return &LeastConnectionBalancer{instances: make([]*Instance, 0),}
}func (b *LeastConnectionBalancer) Select() (*Instance, error) {b.mu.RLock()defer b.mu.RUnlock()if len(b.instances) == 0 {return nil, ErrNoAvailableInstances}var best *InstanceminConn := int64(^uint64(0) >> 1) // 最大int64值for _, instance := range b.instances {if !instance.Active {continue}connections := atomic.LoadInt64(&instance.Connections)if connections < minConn {minConn = connectionsbest = instance}}if best == nil {return nil, ErrNoAvailableInstances}// 增加连接数atomic.AddInt64(&best.Connections, 1)return best, nil
}// 更新实例列表
func (b *LeastConnectionBalancer) UpdateInstances(instances []*Instance) {b.mu.Lock()defer b.mu.Unlock()b.instances = instances
}// 标记请求成功
func (b *LeastConnectionBalancer) MarkSuccess(instance *Instance) {atomic.AddInt64(&instance.Connections, -1)atomic.StoreInt64(&instance.FailCount, 0)instance.LastActive = time.Now()
}// 标记请求失败
func (b *LeastConnectionBalancer) MarkFailed(instance *Instance) {atomic.AddInt64(&instance.Connections, -1)failCount := atomic.AddInt64(&instance.FailCount, 1)// 如果失败次数过多,标记为不可用if failCount >= 3 {instance.Active = false}
}

2. 服务降级实现

// degradation/degradation.go
package degradationimport ("context""sync""time"
)type DegradationLevel intconst (NoDegradation DegradationLevel = iotaPartialDegradationFullDegradation
)type DegradationRule struct {Name           stringThreshold      float64TimeWindow     time.DurationLevel          DegradationLevelRecoveryTime   time.Duration
}type DegradationManager struct {rules       map[string]*DegradationRulestates      map[string]*DegradationStatemu          sync.RWMutex
}type DegradationState struct {Level      DegradationLevelStartTime  time.TimeEndTime    time.TimeMetrics    map[string]float64
}func NewDegradationManager() *DegradationManager {return &DegradationManager{rules:  make(map[string]*DegradationRule),states: make(map[string]*DegradationState),}
}func (m *DegradationManager) AddRule(rule *DegradationRule) {m.mu.Lock()defer m.mu.Unlock()m.rules[rule.Name] = rule
}func (m *DegradationManager) CheckDegradation(ctx context.Context, name string, value float64) DegradationLevel {m.mu.Lock()defer m.mu.Unlock()rule, exists := m.rules[name]if !exists {return NoDegradation}state, exists := m.states[name]if !exists {state = &DegradationState{Level:   NoDegradation,Metrics: make(map[string]float64),}m.states[name] = state}// 更新指标state.Metrics["value"] = value// 如果当前处于降级状态,检查是否可以恢复if state.Level != NoDegradation {if time.Now().After(state.EndTime) {state.Level = NoDegradationstate.StartTime = time.Time{}state.EndTime = time.Time{}} else {return state.Level}}// 检查是否需要降级if value > rule.Threshold {state.Level = rule.Levelstate.StartTime = time.Now()state.EndTime = state.StartTime.Add(rule.RecoveryTime)return rule.Level}return NoDegradation
}// 降级处理器
type DegradationHandler struct {normal      func(context.Context) (interface{}, error)degraded    func(context.Context) (interface{}, error)fallback    func(context.Context) (interface{}, error)
}func NewDegradationHandler(normal func(context.Context) (interface{}, error),degraded func(context.Context) (interface{}, error),fallback func(context.Context) (interface{}, error),
) *DegradationHandler {return &DegradationHandler{normal:   normal,degraded: degraded,fallback: fallback,}
}func (h *DegradationHandler) Handle(ctx context.Context, level DegradationLevel) (interface{}, error) {switch level {case NoDegradation:return h.normal(ctx)case PartialDegradation:if h.degraded != nil {return h.degraded(ctx)}fallthroughcase FullDegradation:if h.fallback != nil {return h.fallback(ctx)}return nil, ErrServiceDegradeddefault:return h.normal(ctx)}
}

3. 熔断器实现

// circuitbreaker/breaker.go
package circuitbreakerimport ("context""sync""time"
)type State intconst (StateClosed State = iota    // 关闭状态(正常运行)StateOpen                   // 打开状态(熔断)StateHalfOpen              // 半开状态(尝试恢复)
)type Settings struct {Name          stringMaxRequests   uint32        // 熔断前的最大请求数Interval      time.Duration // 统计时间窗口Timeout       time.Duration // 熔断恢复时间Threshold     float64       // 错误率阈值
}type CircuitBreaker struct {name          stringstate         Statesettings      Settingscounts        CountslastStateTime time.Timemu            sync.RWMutex
}type Counts struct {Requests       uint32TotalFailures  uint32ConsecutiveFailures uint32LastFailureTime time.Time
}func NewCircuitBreaker(settings Settings) *CircuitBreaker {return &CircuitBreaker{name:     settings.Name,state:    StateClosed,settings: settings,lastStateTime: time.Now(),}
}func (cb *CircuitBreaker) Execute(ctx context.Context, run func() (interface{}, error)) (interface{}, error) {state := cb.GetState()switch state {case StateOpen:if !cb.shouldAttemptReset() {return nil, ErrCircuitBreakerOpen}cb.setState(StateHalfOpen)return cb.executeAndUpdateState(ctx, run)case StateHalfOpen:return cb.executeAndUpdateState(ctx, run)default: // StateClosedreturn cb.executeAndUpdateState(ctx, run)}
}func (cb *CircuitBreaker) executeAndUpdateState(ctx context.Context, run func() (interface{}, error)) (interface{}, error) {defer func() {if r := recover(); r != nil {cb.recordFailure()}}()result, err := run()if err != nil {cb.recordFailure()return nil, err}cb.recordSuccess()return result, nil
}func (cb *CircuitBreaker) recordSuccess() {cb.mu.Lock()defer cb.mu.Unlock()cb.counts.Requests++cb.counts.ConsecutiveFailures = 0if cb.state == StateHalfOpen {cb.setState(StateClosed)}
}func (cb *CircuitBreaker) recordFailure() {cb.mu.Lock()defer cb.mu.Unlock()cb.counts.Requests++cb.counts.TotalFailures++cb.counts.ConsecutiveFailures++cb.counts.LastFailureTime = time.Now()if cb.shouldTrip() {cb.setState(StateOpen)}
}func (cb *CircuitBreaker) shouldTrip() bool {if cb.counts.ConsecutiveFailures >= cb.settings.MaxRequests {return true}if cb.counts.Requests >= cb.settings.MaxRequests {failureRate := float64(cb.counts.TotalFailures) / float64(cb.counts.Requests)if failureRate >= cb.settings.Threshold {return true}}return false
}func (cb *CircuitBreaker) shouldAttemptReset() bool {return time.Since(cb.lastStateTime) >= cb.settings.Timeout
}func (cb *CircuitBreaker) GetState() State {cb.mu.RLock()defer cb.mu.RUnlock()now := time.Now()// 清理过期的计数if now.Sub(cb.lastStateTime) >= cb.settings.Interval {cb.mu.RUnlock()cb.mu.Lock()cb.counts = Counts{}cb.lastStateTime = nowstate := cb.statecb.mu.Unlock()cb.mu.RLock()return state}return cb.state
}func (cb *CircuitBreaker) setState(state State) {cb.state = statecb.lastStateTime = time.Now()if state == StateClosed {cb.counts = Counts{}}
}// 监控指标
type Metrics struct {State              StateRequests           uint32TotalFailures      uint32ConsecutiveFailures uint32FailureRate        float64LastFailureTime    time.Time
}func (cb *CircuitBreaker) Metrics() Metrics {cb.mu.RLock()defer cb.mu.RUnlock()var failureRate float64if cb.counts.Requests > 0 {failureRate = float64(cb.counts.TotalFailures) / float64(cb.counts.Requests)}return Metrics{State:               cb.state,Requests:            cb.counts.Requests,TotalFailures:       cb.counts.TotalFailures,ConsecutiveFailures: cb.counts.ConsecutiveFailures,FailureRate:         failureRate,LastFailureTime:     cb.counts.LastFailureTime,}
}// 熔断器管理器
type BreakerManager struct {breakers map[string]*CircuitBreakermu       sync.RWMutex
}func NewBreakerManager() *BreakerManager {return &BreakerManager{breakers: make(map[string]*CircuitBreaker),}
}func (m *BreakerManager) GetBreaker(name string) (*CircuitBreaker, bool) {m.mu.RLock()defer m.mu.RUnlock()breaker, exists := m.breakers[name]return breaker, exists
}func (m *BreakerManager) AddBreaker(settings Settings) *CircuitBreaker {m.mu.Lock()defer m.mu.Unlock()breaker := NewCircuitBreaker(settings)m.breakers[settings.Name] = breakerreturn breaker
}// 自定义熔断策略
type TripStrategy interface {ShouldTrip(counts Counts) bool
}// 连续失败策略
type ConsecutiveFailuresStrategy struct {Threshold uint32
}func (s *ConsecutiveFailuresStrategy) ShouldTrip(counts Counts) bool {return counts.ConsecutiveFailures >= s.Threshold
}// 错误率策略
type ErrorRateStrategy struct {Threshold    float64MinRequests  uint32
}func (s *ErrorRateStrategy) ShouldTrip(counts Counts) bool {if counts.Requests < s.MinRequests {return false}failureRate := float64(counts.TotalFailures) / float64(counts.Requests)return failureRate >= s.Threshold
}

4. 限流器实现

// ratelimit/limiter.go
package ratelimitimport ("context""sync""time"
)// 令牌桶限流器
type TokenBucket struct {rate       float64     // 令牌产生速率capacity   float64     // 桶容量tokens     float64     // 当前令牌数lastUpdate time.Time   // 上次更新时间mu         sync.Mutex
}func NewTokenBucket(rate float64, capacity float64) *TokenBucket {return &TokenBucket{rate:       rate,capacity:   capacity,tokens:     capacity,lastUpdate: time.Now(),}
}func (tb *TokenBucket) Allow() bool {return tb.AllowN(1)
}func (tb *TokenBucket) AllowN(n float64) bool {tb.mu.Lock()defer tb.mu.Unlock()now := time.Now()// 计算从上次更新到现在产生的令牌数elapsed := now.Sub(tb.lastUpdate).Seconds()tb.tokens = min(tb.capacity, tb.tokens+elapsed*tb.rate)tb.lastUpdate = nowif tb.tokens < n {return false}tb.tokens -= nreturn true
}// 滑动窗口限流器
type SlidingWindow struct {capacity   int                // 窗口容量timeWindow time.Duration      // 时间窗口大小windows    map[int64]int      // 各个小窗口的请求数mu         sync.Mutex
}func NewSlidingWindow(capacity int, timeWindow time.Duration) *SlidingWindow {return &SlidingWindow{capacity:   capacity,timeWindow: timeWindow,windows:    make(map[int64]int),}
}func (sw *SlidingWindow) Allow() bool {sw.mu.Lock()defer sw.mu.Unlock()now := time.Now().UnixNano()windowStart := now - sw.timeWindow.Nanoseconds()// 清理过期的窗口for timestamp := range sw.windows {if timestamp < windowStart {delete(sw.windows, timestamp)}}// 计算当前请求数var total intfor _, count := range sw.windows {total += count}if total >= sw.capacity {return false}// 记录新请求currentWindow := now / int64(time.Second)sw.windows[currentWindow]++return true
}// 漏桶限流器
type LeakyBucket struct {rate      float64     // 漏出速率capacity  float64     // 桶容量water     float64     // 当前水量lastLeakTime time.Time  // 上次漏水时间mu        sync.Mutex
}func NewLeakyBucket(rate float64, capacity float64) *LeakyBucket {return &LeakyBucket{rate:     rate,capacity: capacity,lastLeakTime: time.Now(),}
}func (lb *LeakyBucket) Allow() bool {lb.mu.Lock()defer lb.mu.Unlock()now := time.Now()// 计算从上次漏水到现在漏掉的水量elapsed := now.Sub(lb.lastLeakTime).Seconds()lb.water = max(0, lb.water-elapsed*lb.rate)lb.lastLeakTime = nowif lb.water >= lb.capacity {return false}lb.water++return true
}// 分布式限流器(基于Redis)
type DistributedRateLimiter struct {redis      RedisClientkey        stringrate       intwindow     time.Duration
}type RedisClient interface {Eval(script string, keys []string, args ...interface{}) (interface{}, error)
}func NewDistributedRateLimiter(redis RedisClient, key string, rate int, window time.Duration) *DistributedRateLimiter {return &DistributedRateLimiter{redis:  redis,key:    key,rate:   rate,window: window,}
}const limitScript = `
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local current = tonumber(redis.call('get', key) or "0")
if current >= limit thenreturn 0
end
redis.call('incr', key)
redis.call('expire', key, window)
return 1
`func (rl *DistributedRateLimiter) Allow(ctx context.Context) bool {result, err := rl.redis.Eval(limitScript,[]string{rl.key},rl.rate,int(rl.window.Seconds()),)if err != nil {return false}return result.(int64) == 1
}func min(a, b float64) float64 {if a < b {return a}return b
}func max(a, b float64) float64 {if a > b {return a}return b
}

5. 系统流程图

让我们通过一个流程图来展示整个系统的工作流程:
在这里插入图片描述

6. 使用示例

让我们看一个完整的使用示例:

// main.go
package mainimport ("context""log""net/http""time"
)func main() {// 初始化限流器rateLimiter := ratelimit.NewTokenBucket(100, 1000) // 每秒100个请求,最多积攒1000个令牌// 初始化熔断器breaker := circuitbreaker.NewCircuitBreaker(circuitbreaker.Settings{Name:        "example-service",MaxRequests: 100,Interval:    time.Minute,Timeout:     time.Minute * 5,Threshold:   0.5, // 50%错误率触发熔断})// 初始化负载均衡器balancer := loadbalancer.NewWeightedRoundRobinBalancer()balancer.UpdateInstances([]*loadbalancer.Instance{{ID: "server1", Host: "localhost", Port: 8081, Weight: 2},{ID: "server2", Host: "localhost", Port: 8082, Weight: 1},{ID: "server3", Host: "localhost", Port: 8083, Weight: 1},})// 初始化服务降级管理器degradation := degradation.NewDegradationManager()degradation.AddRule(&degradation.DegradationRule{Name:         "high-load",Threshold:    0.8, // CPU使用率超过80%触发降级TimeWindow:   time.Minute,Level:        degradation.PartialDegradation,RecoveryTime: time.Minute * 5,})// HTTP处理器http.HandleFunc("/api/example", func(w http.ResponseWriter, r *http.Request) {// 限流检查if !rateLimiter.Allow() {http.Error(w, "Rate limit exceeded", http.StatusTooManyRequests)return}// 获取降级状态degradationLevel := degradation.CheckDegradation(r.Context(), "high-load", getCPUUsage())// 处理降级情况handler := degradation.NewDegradationHandler(// 正常处理func(ctx context.Context) (interface{}, error) {return breaker.Execute(ctx, func() (interface{}, error) {// 选择服务实例instance, err := balancer.Select()if err != nil {return nil, err}// 调用服务resp, err := callService(instance)if err != nil {// 标记失败balancer.MarkFailed(instance)return nil, err}// 标记成功balancer.MarkSuccess(instance)return resp, nil})},// 部分降级处理func(ctx context.Context) (interface{}, error) {// 返回缓存数据return getFromCache(ctx)},// 完全降级处理func(ctx context.Context) (interface{}, error) {// 返回降级默认值return getDefaultResponse(ctx)},)// 执行请求处理result, err := handler.Handle(r.Context(), degradationLevel)if err != nil {http.Error(w, err.Error(), http.StatusInternalServerError)return}// 返回结果w.Header().Set("Content-Type", "application/json")json.NewEncoder(w).Encode(result)})// 监控处理器http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {metrics := map[string]interface{}{"circuit_breaker": breaker.Metrics(),"rate_limiter": map[string]interface{}{"qps": rateLimiter.QPS(),"total_requests": rateLimiter.TotalRequests(),},"load_balancer": balancer.Metrics(),}w.Header().Set("Content-Type", "application/json")json.NewEncoder(w).Encode(metrics)})// 启动服务器log.Fatal(http.ListenAndServe(":8080", nil))
}// 辅助函数
func callService(instance *loadbalancer.Instance) (interface{}, error) {url := fmt.Sprintf("http://%s:%d/api", instance.Host, instance.Port)ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)defer cancel()req, err := http.NewRequestWithContext(ctx, "GET", url, nil)if err != nil {return nil, err}resp, err := http.DefaultClient.Do(req)if err != nil {return nil, err}defer resp.Body.Close()if resp.StatusCode != http.StatusOK {return nil, fmt.Errorf("service returned status: %d", resp.StatusCode)}var result interface{}if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {return nil, err}return result, nil
}func getFromCache(ctx context.Context) (interface{}, error) {// 实现缓存读取逻辑cache := redis.NewClient(&redis.Options{Addr: "localhost:6379",})defer cache.Close()value, err := cache.Get(ctx, "cache_key").Result()if err != nil {return nil, err}var result interface{}if err := json.Unmarshal([]byte(value), &result); err != nil {return nil, err}return result, nil
}func getDefaultResponse(ctx context.Context) (interface{}, error) {// 返回降级默认响应return map[string]interface{}{"status": "degraded","data": map[string]interface{}{"message": "Service is temporarily degraded","fallback_data": []string{"default", "response", "data"},},}, nil
}func getCPUUsage() float64 {var status syscall.Statfs_tif err := syscall.Statfs("/", &status); err != nil {return 0}// 获取CPU使用率percent, err := cpu.Percent(time.Second, false)if err != nil {return 0}if len(percent) > 0 {return percent[0]}return 0
}// 配置项
type Config struct {RateLimit struct {QPS      float64       `yaml:"qps"`Capacity float64       `yaml:"capacity"`} `yaml:"rate_limit"`CircuitBreaker struct {MaxRequests uint32        `yaml:"max_requests"`Interval    time.Duration `yaml:"interval"`Timeout     time.Duration `yaml:"timeout"`Threshold   float64       `yaml:"threshold"`} `yaml:"circuit_breaker"`LoadBalancer struct {Instances []struct {ID     string `yaml:"id"`Host   string `yaml:"host"`Port   int    `yaml:"port"`Weight int    `yaml:"weight"`} `yaml:"instances"`} `yaml:"load_balancer"`Degradation struct {Rules []struct {Name         string        `yaml:"name"`Threshold    float64       `yaml:"threshold"`TimeWindow   time.Duration `yaml:"time_window"`Level        string        `yaml:"level"`RecoveryTime time.Duration `yaml:"recovery_time"`} `yaml:"rules"`} `yaml:"degradation"`
}func loadConfig(filename string) (*Config, error) {data, err := ioutil.ReadFile(filename)if err != nil {return nil, err}var config Configif err := yaml.Unmarshal(data, &config); err != nil {return nil, err}return &config, nil
}

让我们继续完成使用示例的实现:

7. 配置示例

让我们看一个配置文件的示例:

# config.yaml
rate_limit:qps: 100capacity: 1000circuit_breaker:max_requests: 100interval: 60stimeout: 300sthreshold: 0.5load_balancer:instances:- id: server1host: localhostport: 8081weight: 2- id: server2host: localhostport: 8082weight: 1- id: server3host: localhostport: 8083weight: 1degradation:rules:- name: high-loadthreshold: 0.8time_window: 60slevel: partialrecovery_time: 300s- name: error-ratethreshold: 0.3time_window: 60slevel: fullrecovery_time: 300s

8. 关键功能说明

  1. 负载均衡:

    • 轮询策略
    • 加权轮询
    • 最小连接数
    • 实例健康检查
    • 动态更新实例列表
  2. 服务降级:

    • 多级降级策略
    • 基于指标的降级
    • 自动恢复机制
    • 降级处理器
  3. 熔断机制:

    • 状态管理
    • 失败计数
    • 自动恢复
    • 半开状态试探
  4. 限流设计:

    • 令牌桶算法
    • 滑动窗口
    • 漏桶算法
    • 分布式限流

这个完整的服务治理系统提供了:

  1. 全面的服务保护机制
  2. 灵活的配置选项
  3. 可扩展的设计
  4. 完整的监控指标
  5. 多种降级策略
  6. 分布式支持

怎么样今天的内容还满意吗?再次感谢观众老爷的观看,关注GZH:凡人的AI工具箱,回复666,送您价值199的AI大礼包。最后,祝您早日实现财务自由,还请给个赞,谢谢!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/486849.html

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

相关文章

LLMs之Agent之Lares:Lares的简介、安装和使用方法、案例应用之详细攻略

LLMs之Agent之Lares&#xff1a;Lares的简介、安装和使用方法、案例应用之详细攻略 导读&#xff1a;这篇博文介绍了 Lares&#xff0c;一个由简单的 AI 代理驱动的智能家居助手模拟器&#xff0c;它展现出令人惊讶的解决问题能力。 >> 背景痛点&#xff1a;每天都有新的…

Halcon 轮廓检测常用算子、原理及应用场景

一、引言 在机器视觉领域&#xff0c;轮廓检测是一项关键技术&#xff0c;它能够提取物体的边缘信息&#xff0c;从而实现物体的定位、识别、测量等多种功能。Halcon 作为一款强大的机器视觉软件库&#xff0c;提供了丰富的轮廓检测算子。本文将详细介绍 Halcon 中轮廓检测的常…

11.23[大数据]

PRO1:LSTM模型预测输出都是同一个值&#xff1f; 画出来的图像就是一条横线 这个搜了搜&#xff0c;原因可能有很多&#xff0c;但感觉最主要的原因极可能是激活函数选择不当&#xff0c;以及层的搭建不合适 原模型是 REF https://zhuanlan.zhihu.com/p/654325094 https:/…

TEA系列例题

解析 TEA 加密算法(C语言、python)&#xff1a;_tea加密-CSDN博客 CTF-RE 从0到N: TEA_tea加密原理-CSDN博客 1 字节 8 位 牢记密文的64位和密钥的128位,最好可以自己独立的写出tea解密代码 相当于密文是传入8个字符类型数据或者是2个整型数据, 密钥是16个字符数据或者4个…

[241206] X-CMD 发布 v0.4.15:env 升级,mirror 支持华为/腾讯 npm 镜像,pb-wayland 剪贴板

目录 X-CMD 发布 v0.4.15&#x1f4c3;Changelog&#x1f4e6; env|pkg&#x1fa9e; mirror&#x1f4d1; pb&#x1f3a8; theme|starship|ohmyposh&#x1f916; chat&#x1f4dd; man✅ 升级指南 X-CMD 发布 v0.4.15 &#x1f4c3;Changelog &#x1f4e6; env|pkg 新增…

SpringBoot【一】零基础入门 springboot 及 idea 搭建

一、前言 springboot是什么&#xff1f; Spring Boot是由Pivotal团队提供的全新框架&#xff0c;其设计目的是用来简化新Spring应用的初始搭建以及开发过程。 该框架使用了特定的方式来进行配置&#xff0c;从而使开发人员不再需要定义样板化的配置。用我的话来理解&#xff0…

【kotlin 】内联类(value class / inline class)

官方文档&#xff1a;https://kotlinlang.org/docs/inline-classes.html 注&#xff1a;inline class 关键字已经被废弃&#xff0c;取而代之的是value class。现在使用内联类需要定义类为value class&#xff0c;并使用JvmInline注解进行标注。 一、使用场景 有时候&#xff…

3D 生成重建023-DMV3D用扩散模型做3D生成大模型

3D 生成重建023-DMV3D用扩散模型做3D生成大模型 文章目录 0 论文工作1 论文方法2 实验效果 0 论文工作 这个论文提出了一种新的三维生成方法DMV3D&#xff0c;它利用基于Transformer 的大型三维重建模型对多视图扩散进行去噪处理。重建模型包含三平面NeRF表示&#xff0c;并能…

Linux - 进程等待和进程替换

进程等待 前面我们了解了如果父进程没有回收子进程, 那么当子进程接收后, 就会一直处于僵尸状态, 导致内存泄漏, 那么我们如何让父进程来回收子进程的资源. waitpid 我们可以通过 Linux 提供的系统调用函数 wait 系列函数来等待子进程死亡, 并回收资源. #include <sys/t…

【Redis】壹 —— Redis 介绍

文章目录&#xff1a; 前言 一、认识Redis 1. Redis 用途 作为数据库 作为流引擎 二、服务端高并发分布式结构演变 1. 单机架构 2. 应用数据分离架构 3. 应用服务集群架构 4. 读写分离 / 主从分离架构 5. 冷热分离 —— 引入缓存 6. 分库分表 7. 微服务架构 8. …

D88【python 接口自动化学习】- pytest基础用法

day88 pytest运行参数-q -s -v 学习日期&#xff1a;20241203 学习目标&#xff1a;pytest基础用法 -- pytest运行参数-q -s -v&pytest使用ini配置指定运行参数 学习笔记&#xff1a; pytest -q 说明&#xff1a;简化控制台的输出 pytest -q .\testcases\test_reques…

22 网络编程:Go 语言如何通过 RPC 实现跨平台服务

在上一讲中&#xff0c;我为你讲解了 RESTful API 的规范以及实现&#xff0c;并且留了两个作业&#xff0c;它们分别是删除和修改用户&#xff0c;现在我为你讲解这两个作业。 删除一个用户比较简单&#xff0c;它的 API 格式和获取一个用户一样&#xff0c;但是 HTTP 方法换…

java八股-流量封控系统

文章目录 请求后台管理的频率-流量限制流量限制的业务代码UserFlowRiskControlFilter 短链接中台的流量限制CustomBlockHandler 对指定接口限流UserFlowRiskControlConfigurationSentinelRuleConfig 请求后台管理的频率-流量限制 根据登录用户做出控制&#xff0c;比如 x 秒请…

AI给我们生活带来了哪些便利?

在21世纪的科技浪潮中&#xff0c;人工智能&#xff08;AI&#xff09;如同一股不可阻挡的力量&#xff0c;正深刻地改变着我们的生活方式&#xff0c;从日常琐事到复杂决策&#xff0c;无一不渗透着AI的智慧之光。它不仅极大地提升了生活效率&#xff0c;还为我们开启了前所未…

在vue3里使用scss实现简单的换肤功能

实现的换肤功能&#xff1a;主题色切换、亮色模式和暗黑模式切换、背景图切换 主题色就是网站主色&#xff0c;可以配置到组件库上面&#xff1b;亮色模式又分为两种风格&#xff1a;纯白风格和背景图风格&#xff0c;不需要背景图的话可以删掉这部分逻辑和相关定义&#xff1b…

css实现圆周运动效果

在CSS中可以通过 keyframes 动画 和 transform 属性实现元素的圆周运动。以下是一个示例代码&#xff1a; 示例代码 <!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8"> <meta name"viewport" content…

Python subprocess.run 使用注意事项,避免出现list index out of range

在执行iOS UI 自动化专项测试的时候&#xff0c;在运行第一遍的时候遇到了这样的错误&#xff1a; 2024-12-04 20:22:27 ERROR conftest pytest_runtest_makereport 106 Test test_open_stream.py::TestOpenStream::test_xxx_open_stream[iPhoneX-xxx-1-250] failed with err…

不一样的CSS(4)--icon图标系列之svg

序言 上一节内容我们讲解了如何利用css去画一个五角星&#xff0c;其中包括了使用svg的方法&#xff0c;有些小伙伴们对svg的使用不是很了解&#xff0c;那么本节内容我们主要来讲一下&#xff0c;关于svg标签的的使用。 目录 序言一、svg的介绍二、安装SVG扩展插件三、SVG基…

springSecurity认证流程

Spring Security 是spring家族中的一个安全管理框架。相比于另一个安全框架Shiro&#xff0c;它提供更丰富的功能和社区资源&#xff0c;但也较难上手。所以一般大项目用spring Security&#xff0c;小项目用Shiro。 一般web应用需要认证和授权&#xff0c;这也是spring Secur…

FastAPI解决跨域报错net::ERR_FAILED 200 (OK)

目录 一、跨域问题的本质 二、FastAPI中的CORS处理 1. 安装FastAPI和CORS中间件 2. 配置CORS中间件 3. 运行FastAPI应用 三、解决跨域报错的步骤 四、案例:解决Vue.js与FastAPI的跨域问题 1. Vue.js前端应用 2. FastAPI后端API 3. 配置CORS中间件 4. 运行和测试 五…