当前位置: 首页 > wzjs >正文

泉州有专门帮做网站的吗网站运营每天做啥工作

泉州有专门帮做网站的吗,网站运营每天做啥工作,自建房平面图设计软件,国外效果做的好的网站1 概述 在同城多机房情景下,各个机房各自部署一套微服务集群,正常情况下微服务调用在本机房闭环。在如下某些灾难情景,可以尝试拉远调用以最大程度维持业务连续性,这些情景例如: A机房多个服务器宕机。应用由于BUG发…

1 概述

在同城多机房情景下,各个机房各自部署一套微服务集群,正常情况下微服务调用在本机房闭环。在如下某些灾难情景,可以尝试拉远调用以最大程度维持业务连续性,这些情景例如:

  • A机房多个服务器宕机。
  • 应用由于BUG发生OOM导致暂时性应用不可用、或者被kubelet重启,等应用重新正常运行需要5分钟以上。

为了实现拉远调用,进程的负载均衡逻辑需要感知机房位置,因此微服务注册到服务注册中心时需要夹带额外的元数据。

2 spring cloud loadbalancer

Spring Cloud LoadBalancer是Spring Cloud提供的一个用于微服务架构中的客户端负载均衡解决方案。它旨在取代Netflix Ribbon,提供了更现代化的API和更好的与Spring生态系统的集成。

2.1 主要特性

  • 简化配置:
    Spring Cloud LoadBalancer提供了简化的配置选项,并且可以通过应用程序属性文件轻松配置。
  • 自动配置支持:
    它能够自动与RestTemplate和Feign客户端集成,无需手动设置负载均衡逻辑。
  • 反应式编程支持:
    支持基于 WebFlux 的非阻塞 I/O 操作,对于构建高性能、响应式的微服务非常重要。
  • 灵活的负载均衡策略:
    内置多种负载均衡算法(如轮询、随机选择等),并且可以自定义实现以满足特定需求。
  • 服务发现集成:
    与Spring Cloud DiscoveryClient接口兼容,可以与Eureka、Consul等服务发现工具无缝协作。

2.2 自定义负载均衡的套路

2.2.1 步骤1

编写自定义负载均衡逻辑的类,内容如下:

package com.example.consumer.balancer;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.Response;
import  org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
import  org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.SelectedInstanceCallback;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;public class MyNewLoadBalancer implements ReactorServiceInstanceLoadBalancer {private static final Log log =  LogFactory.getLog(MyNewLoadBalancer.class);private final String serviceId;private ObjectProvider<ServiceInstanceListSupplier>  serviceInstanceListSupplierProvider;private final String localDataCenter;/**
*      * @param serviceInstanceListSupplierProvider a provider of
*             * {@link ServiceInstanceListSupplier} that will be used to get available instances
*                    * @param serviceId id of the service for which to choose an  instance
*                           */public MyNewLoadBalancer(ObjectProvider<ServiceInstanceListSupplier>  serviceInstanceListSupplierProvider,String serviceId, String localDataCenter) {this.serviceId = serviceId;this.serviceInstanceListSupplierProvider =  serviceInstanceListSupplierProvider;this.localDataCenter = localDataCenter;}@SuppressWarnings("rawtypes")@Override// 核心方法,负载均衡的逻辑就是从choose()开始public Mono<Response<ServiceInstance>> choose(Request request) {ServiceInstanceListSupplier supplier =  serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new);return supplier.get(request).next().map(serviceInstances ->  processInstanceResponse(supplier, serviceInstances));}private Response<ServiceInstance>  processInstanceResponse(ServiceInstanceListSupplier supplier,List<ServiceInstance> serviceInstances) {Response<ServiceInstance> serviceInstanceResponse =  getInstanceResponse(serviceInstances);if (supplier instanceof SelectedInstanceCallback &&  serviceInstanceResponse.hasServer()) {((SelectedInstanceCallback)  supplier).selectedServiceInstance(serviceInstanceResponse.getServer());}return serviceInstanceResponse;}private Response<ServiceInstance>  getInstanceResponse(List<ServiceInstance> instances) {if (instances.isEmpty()) {if (log.isWarnEnabled()) {log.warn("No servers available for service: " +  serviceId);}return new EmptyResponse();}// 同机房的服务实例List<ServiceInstance> sameDcInstances = instances.stream().filter(instance -> localDataCenter.equals(instance.getMetadata().get("DATA_CENTER"))).collect(Collectors.toList());// 其他机房的服务实例List<ServiceInstance> otherDcInstances = instances.stream().filter(instance -> !localDataCenter.equals(instance.getMetadata().get("DATA_CENTER"))).collect(Collectors.toList());// 两个服务实例列表,选择一个  List<ServiceInstance> selectedInstances = sameDcInstances.isEmpty() ?otherDcInstances : sameDcInstances;// 选好实例列表后,再使用随机方式挑选出一个int index =  ThreadLocalRandom.current().nextInt(selectedInstances.size());ServiceInstance instance = selectedInstances.get(index);return new DefaultResponse(instance);}
}

2.2.2 步骤2

编写工厂类,不需要添加@Configuration:

package com.example.consumer.balancer;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;public class MyLoadBalancerConfig {@Beanpublic ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment  environment, LoadBalancerClientFactory loadBalancerClientFactory){String name =  environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);// 本地机房的信息,从环境变量中获取即可String localDataCenter = environment.getProperty("spring.cloud.nacos.discovery.metadata.DATA_CENTER");return new MyNewLoadBalancer(loadBalancerClientFactory.getLazyProvider(name,  ServiceInstanceListSupplier.class), name, localDataCenter);}
}

2.2.3 步骤3

在main类中使用@LoadBalancerClient或@LoadBalancerClients来指定刚刚创建工厂类:

package com.example.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import com.example.consumer.balancer.MyLoadBalancerConfig;@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@LoadBalancerClient(name = "service-provider", configuration =  MyLoadBalancerConfig.class)
// @LoadBalancerClients(defaultConfiguration = MyLoadBalancerConfig.class)
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}
}

2.2.4 完整代码

https://gitee.com/handsomeboylj/spring-cloud-nacos-demo

3 容灾方案

两边机房都正常时:
在这里插入图片描述
DC1机房的Provider应用临时不可用时,拉远调用另外机房的Provider应用:
在这里插入图片描述

4 测试

本次测试中,namespace dc1作为dc1机房,namespace dc2作为dc2机房,所有微服务实例都注册到同一个nacos服务中,所有微服务实例在网络层都是扁平的、可直接调用的(对应到现实里,就是是两个机房通过VPN或专线打通,容器网络使用underlay模式)。

git clone https://gitee.com/handsomeboylj/spring-cloud-nacos-demo.git
kubectl apply -f doc/k8s/dc-awareness/

部署成功后,如下:
在这里插入图片描述
dc1机房的一个消费者的IP是10.0.13.96,其工作端口是8082,接口是/consumer/call,调用可以看见结果,消费者和生产者都会响应自己所在的机房:
在这里插入图片描述
将dc1机房的生产者关闭后,再访问dc1机房的消费者的接口,可以看见响应是dc2,说明调用了机房2的生产者。
在这里插入图片描述

将dc1机房的生产者重新上线后,dc1的消费者从拉远调用转变成本机房调用
在这里插入图片描述

5 小结

本文介绍拉远调用可临时维持业务系统的连续性,并且使用spring cloud loadbalancer来实现感知机房,优先本机房闭环调用,次之拉远调用。

http://www.dtcms.com/wzjs/575392.html

相关文章:

  • 宁波网站seo桂林模板网站建设
  • 公司网站如何被收录做网站就上微赞网
  • 域客士单页网站网站开发价目表
  • 建e室内设计网 3d模型医美前台网站排名优化
  • 工作室网站开发免费1级做爰片在线观看网站
  • 网站建设公司浙江江苏中粟建设工程有限公司网站
  • 中国建设网官方网站地址网络营销推广的方式包括
  • wordpress nodejs版本seo关键词优化服务
  • 网站效果用什么软件做微信制作网站开发
  • 上海网站备案人工服务器wordpress 高端主题
  • 好看的企业网站最近热点新闻事件2023
  • 软件工程师证书报考网站长沙百姓网招聘信息
  • 大气简约企业网站模板免费下载均安公司网站建设
  • 购物商城外贸网站建设做网站还有搞头吗
  • 建设网站网络公司内蒙古优途国际旅行社
  • 建e网站网站检索 标签
  • 泗洪网站百度地图怎么没有实景导航了
  • 鄂州第一官方网站网站建设实训设计思想
  • 外贸网站如何推广出去秦皇岛吧百度贴吧
  • 广东两学一做考试网站wordpress 图片保护
  • 装修房子的风格设计图软件北京seo服务商找行者seo
  • 多个wordpress站点同步做会员卡的网站在线制作
  • 关系网站优化公司广东网站建设方便
  • 深圳做电商平台网站企业年检网上申报流程
  • 广告网站推广销售昆明做网站建设的公司排名
  • 如何创办自己的网站如何做网络营销推广赚钱
  • 建设化工网站的目的品牌网页设计公司
  • 交易网站开发文档关于域名用于非网站用途
  • 律师事务所网站建设重要性中英语双语网站咋做
  • 做图书网站赚钱么可以登陆的wordpress