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

网站建设服务费属于什么费用微应用和微网站的区别

网站建设服务费属于什么费用,微应用和微网站的区别,4成都网站建设,wordpress 调用页面列表五、负载均衡服务调用Ribbon 技术版本Spring Cloud版本Hoxton.SR1Spring Boot版本2.2.2RELEASECloud Alibaba版本2.1.0.RELEASE Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。 简单的说#xff0c;Ribbon是Netflix发布的开源项目#xff0c;主…五、负载均衡服务调用Ribbon 技术版本Spring Cloud版本Hoxton.SR1Spring Boot版本2.2.2RELEASECloud Alibaba版本2.1.0.RELEASE Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。 简单的说Ribbon是Netflix发布的开源项目主要功能是提供客户端的软件负载均衡算法和服务调用。Ribbon客户端组件提供一系列完善的配置项如连接超时重试等。简单的说就是在配置文件中列出Load Balancer简称LB后面所有的机器Ribbon会自动的帮助你基于某种规则如简单轮询随机连接等去连接这些机器。我们很容易使用Ribbon实现自定义的负载均衡算法。 官网:https://github.com/Netflix/ribbon/wiki/Getting-Started 5.1 简介 LB负载均衡(Load Balance)是什么 简单的说就是将用户的请求平摊的分配到多个服务上从而达到系统的HA高可用。常见的负载均衡有软件NginxLVS硬件 F5等。 Ribbon本地负载均衡客户端 VS Nginx服务端负载均衡区别 Nginx是服务器负载均衡客户端所有请求都会交给nginx然后由nginx实现转发请求。即负载均衡是由服务端实现的。 Ribbon本地负载均衡在调用微服务接口时候会在注册中心上获取注册信息服务列表之后缓存到JVM本地从而在本地实现RPC远程服务调用技术。Ribbon概括起来就是负载均衡RestTemplate调用 Ribbon在工作时分成两步 第一步先选择 EurekaServer ,它优先选择在同一个区域内负载较少的server. 第二步再根据用户指定的策略在从server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略比如轮询、随机和根据响应时间加权。 在做Eureka集群配置的时候并没有引入如下Ribbon的配置就已经实现了负载均衡。 dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-netflix-ribbon/artifactId /dependency 这是因为spring-cloud-starter-netflix-eureka-client自带了spring-cloud-starter-ribbon引用 !--添加eureka-client-- dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId /dependencyRestTemplate调用 getForObject方法/getForEntity方法 getForObject方法 返回对象为响应体中数据转化成的对象基本上可以理解为Json getForEntity方法: 返回对象为ResponseEntity对象包含了响应中的一些重要信息比如响应头、响应状态码、响应体等 GetMapping(/get/{id})public CommonResultPayment getPayment(PathVariable(id) Long id) {return restTemplate.getForObject(PAYMENT_URL/payment/get/id,CommonResult.class);}GetMapping(/get/entity/{id})public CommonResultPayment getPaymentEntity(PathVariable(id) Long id) {ResponseEntityCommonResult entity restTemplate.getForEntity(PAYMENT_URL /payment/get/ id, CommonResult.class);if(entity.getStatusCode().is2xxSuccessful()){return entity.getBody();}else {return new CommonResult(444,操作失败);}}postForObject/postForEntity GetMapping(/create)public CommonResultPayment create(Payment payment) {return restTemplate.postForObject(PAYMENT_URL /payment/create,payment,CommonResult.class);}GetMapping(/entity/create)public CommonResultPayment createEntity(Payment payment) {return restTemplate.postForEntity(PAYMENT_URL /payment/create,payment,CommonResult.class).getBody();}5.2 核心组件IRule 5.2.1 IRule实现的负载均衡算法 com.netflix.loadbalancer.RoundRobinRule ​ 轮询 com.netflix.loadbalancer.RandomRule ​ 随机 com.netflix.loadbalancer.RetryRule ​ 先按照RoundRobinRule的策略获取服务如果获取服务失败则在指定时间内会进行重试获取可用的服务 WeightedResponseTimeRule ​ 对RoundRobinRule的扩展响应速度越快的实例选择权重越大越容易被选择 BestAvailableRule ​ 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务然后选择一个并发量最小的服务 AvailabilityFilteringRule ​ 先过滤掉故障实例再选择并发较小的实例 ZoneAvoidanceRule ​ 默认规则,复合判断server所在区域的性能和server的可用性选择服务器 5.2.2 如何改变负载算法 修改cloud-consumer-order80 Configuration public class ApplicationContextConfig {BeanLoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();} }package com.yyds.myrule;import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;Configuration public class MySelfRule {Beanpublic IRule myRule(){// 随机负载return new RandomRule();} } package com.yyds.springcloud;import com.yyds.myrule.MySelfRule; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClient;SpringBootApplication EnableEurekaClient // 添加 RibbonClient(name CLOUD-PAYMENT-SERVICE,configuration MySelfRule.class) public class OrderMain80 {public static void main(String[] args) {SpringApplication.run(OrderMain80.class, args);System.out.println(***************OrderMain80启动成功********************);} }注意自定义配置类不能放在ComponentScan所扫描的当前包下以及子包下否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享达不到特殊化定制的目的了。 注意一些新spring cloud的版本的Eureka starter已经不在集成Ribbon所以新版本不会在使用RibbonClient(name ,configuration ) 以及RibbonClients使用LoadBalancerClient来进行对LoadBalanced的配置修改。可以查阅新版本的修改方法。 5.3 负载均衡算法 5.3.1 原理 负载均衡算法rest接口第几次请求数 % 服务器集群总数量 实际调用服务器位置下标 每次服务重启动后rest接口计数从1开始。ListServiceInstance instances discoveryClient.getInstances(CLOUD-PAYMENT-SERVICE);如 List [0] instances 127.0.0.1:8002List [1] instances 127.0.0.1:80018001 8002 组合成为集群它们共计2台机器集群总数为2 按照轮询算法原理当总请求数为1时 1 % 2 1 对应下标位置为1 则获得服务地址为127.0.0.1:8001 当总请求数位2时 2 % 2 0 对应下标位置为0 则获得服务地址为127.0.0.1:8002 当总请求数位3时 3 % 2 1 对应下标位置为1 则获得服务地址为127.0.0.1:8001 如此类推......5.3.2 手动实现 支付服务提供者8001和8002接口添加 GetMapping(value /lb)public String getPaymentLB() {return serverPort;}在订单服务消费者80中 自定义LoadBalancer接口 package com.yyds.springcloud.myloadbalancer;import org.springframework.cloud.client.ServiceInstance;import java.util.List;public interface LoadBalancer {ServiceInstance instances(ListServiceInstance serviceInstances); } 自定义LoadBalancer实现类 package com.yyds.springcloud.myloadbalancer;import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.client.ServiceInstance; import org.springframework.stereotype.Component;import java.util.List; import java.util.concurrent.atomic.AtomicInteger;Component Slf4j public class MyLoadBalancer implements LoadBalancer {private AtomicInteger atomicInteger new AtomicInteger(0);public final int getAndIncrement(){int current;int next;do{current this.atomicInteger.get();next current 2147483647 ? 0 : current 1;}while (!this.atomicInteger.compareAndSet(current,next));log.info(--------------next next);return next;}Overridepublic ServiceInstance instances(ListServiceInstance serviceInstances) {int index getAndIncrement() % serviceInstances.size();return serviceInstances.get(index);} }ApplicationContextBean去掉注解LoadBalanced(注意此时原来接口报异常) Configuration public class ApplicationContextConfig {Bean// LoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();} }Controller中添加接口使用自定义负载均衡策略 //可以获取注册中心上的服务列表Resourceprivate DiscoveryClient discoveryClient;Resourceprivate LoadBalancer loadBalancer;GetMapping(/lb)public String getPaymentLB() {ListServiceInstance instances discoveryClient.getInstances(CLOUD-PAYMENT-SERVICE);if (instances null || instances.size() 0) {return null;}ServiceInstance serviceInstance loadBalancer.instances(instances);URI uri serviceInstance.getUri();return restTemplate.getForObject(uri /payment/lb, String.class);}测试 http://localhost/consumer/payment/lb 5.3.3 RoundRobinRule源码 package com.netflix.loadbalancer;import com.netflix.client.config.IClientConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.util.List; import java.util.concurrent.atomic.AtomicInteger;/*** The most well known and basic load balancing strategy, i.e. Round Robin Rule.** author stonse* author Nikos Michalakis nikosnetflix.com**/ public class RoundRobinRule extends AbstractLoadBalancerRule {private AtomicInteger nextServerCyclicCounter;private static final boolean AVAILABLE_ONLY_SERVERS true;private static final boolean ALL_SERVERS false;private static Logger log LoggerFactory.getLogger(RoundRobinRule.class);public RoundRobinRule() {nextServerCyclicCounter new AtomicInteger(0);}public RoundRobinRule(ILoadBalancer lb) {this();setLoadBalancer(lb);}public Server choose(ILoadBalancer lb, Object key) {if (lb null) {log.warn(no load balancer);return null;}Server server null;int count 0;while (server null count 10) {ListServer reachableServers lb.getReachableServers();ListServer allServers lb.getAllServers();int upCount reachableServers.size();int serverCount allServers.size();if ((upCount 0) || (serverCount 0)) {log.warn(No up servers available from load balancer: lb);return null;}// 利用下标从服务提供者中选取服务提供者int nextServerIndex incrementAndGetModulo(serverCount);server allServers.get(nextServerIndex);if (server null) {/* Transient. */Thread.yield();continue;}if (server.isAlive() (server.isReadyToServe())) {return (server);}// Next.server null;}if (count 10) {log.warn(No available alive servers after 10 tries from load balancer: lb);}return server;}/*** Inspired by the implementation of {link AtomicInteger#incrementAndGet()}.** param modulo The modulo to bound the value of the counter.* return The next value.*/private int incrementAndGetModulo(int modulo) {// 使用AtomicInteger的CASfor (;;) {int current nextServerCyclicCounter.get();int next (current 1) % modulo;if (nextServerCyclicCounter.compareAndSet(current, next))return next;}}Overridepublic Server choose(Object key) {return choose(getLoadBalancer(), key);}Overridepublic void initWithNiwsConfig(IClientConfig clientConfig) {} }六、服务接口调用OpenFeign 6.1 简介 Feign能干什么 Feign旨在使编写Java Http客户端变得更容易。 前面在使用RibbonRestTemplate时利用RestTemplate对http请求的封装处理形成了一套模版化的调用方法。但是在实际开发中由于对服务依赖的调用可能不止一处往往一个接口会被多处调用所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以Feign在此基础上做了进一步封装由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下我们只需创建一个接口并使用注解的方式来配置它即可完成对服务提供方的接口绑定简化了使用Spring cloud Ribbon时自动封装服务调用客户端的开发量。 Feign集成了Ribbon 利用Ribbon维护了Payment的服务列表信息并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是通过feign只需要定义服务绑定接口且以声明式的方法优雅而简单的实现了服务调用 Feign和OpenFeign ​ Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端Feign内置了Ribbon用来做客户端负载均衡去调用服务注册中心的服务。Feign的使用方式是使用Feign的注解定义接口调用这个接口就可以调用服务注册中心的服务 ​ OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解如RequesMapping等等。OpenFeign的FeignClient可以解析SpringMVC的RequestMapping注解下的接口并通过动态代理的方式产生实现类实现类中做负载均衡并调用其他服务。 6.2 OpenFeign使用案例 新建cloud-consumer-feign-order80 6.2.1 pom文件 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringcloud/artifactIdgroupIdcom.yyds/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdcloud-consumer-feign-order80/artifactIddependencies!--openfeign--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency!--添加eureka-client--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency!--引入自己定义的api通用包--dependencygroupIdcom.yyds/groupIdartifactIdcloud-api-commons/artifactIdversion1.0-SNAPSHOT/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies/project6.2.2 yml配置 server:port: 80spring:application:name: cloud-order-feign-service# 添加下面配置信息 eureka:client:service-url:defaultZone: http://localhost:7001/eureka # defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本register-with-eureka: true # 是否从EurekaServer抓取已有的注册信息默认为true。单节点无所谓集群必须设置为true才能配合ribbon使用负载均衡fetch-registry: true6.2.3 主启动类 package com.yyds.springcloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients;SpringBootApplication EnableEurekaClient EnableFeignClients // 添加此注解 public class OpenFeignOrderMain80 {public static void main(String[] args) {SpringApplication.run(OpenFeignOrderMain80.class, args);System.out.println(***************OpenFeignOrderMain80启动成功********************);} }6.2.4 业务类 业务逻辑接口FeignClient配置调用provider服务 package com.yyds.springcloud.service;import com.yyds.springcloud.entities.CommonResult; import com.yyds.springcloud.entities.Payment; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable;Component FeignClient(value CLOUD-PAYMENT-SERVICE) public interface PaymentFeignService {GetMapping(/payment/get/{id})CommonResultPayment getPaymentById(PathVariable(id) Long id) ; }package com.yyds.springcloud.controller;import com.yyds.springcloud.entities.CommonResult; import com.yyds.springcloud.entities.Payment; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.yyds.springcloud.service.PaymentFeignService;import javax.annotation.Resource;RestController RequestMapping(/consumer/payment) Slf4j public class OrderController {Resourceprivate PaymentFeignService service;GetMapping(/get/{id})public CommonResultPayment getPaymentById(PathVariable(id) Long id) {return service.getPaymentById(id);}}6.2.5 测试 http://localhost/consumer/payment/get/1{code:200,message:查询成功,serverPort8002,data:{id:1,serial:00001a}}6.3 更多用法 6.3.1 超时功能 默认Feign客户端只等待一秒钟但是服务端处理需要超过1秒钟导致Feign客户端不想等待了直接返回报错。为了避免这样的情况有时候我们需要设置Feign客户端的超时控制。 #设置feign客户端超时时间(OpenFeign默认支持ribbon) ribbon:#指的是建立连接所用的时间适用于网络状况正常的情况下,两端连接所用的时间ReadTimeout: 5000#指的是建立连接后从服务器读取到可用资源所用的时间ConnectTimeout: 50006.3.2 日志打印功能 Feign 提供了日志打印功能我们可以通过配置来调整日志级别从而了解 Feign 中 Http 请求的细节。 日志的级别如下NONE默认的不显示任何日志BASIC仅记录请求方法、URL、响应状态码及执行时间HEADERS除了 BASIC 中定义的信息之外还有请求和响应的头信息FULL除了 HEADERS 中定义的信息之外还有请求和响应的正文及元数据。配置日志Bean package com.yyds.springcloud.config;import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;Configuration public class FeignConfig {BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL;} }Yml文件 logging:level:# feign日志以什么级别监控哪个接口com.yyds.springcloud.service.PaymentFeignService: debug
http://www.dnsts.com.cn/news/263633.html

相关文章:

  • 网站链接可以自己做吗网站建设分析书引言
  • 阿里云备案网站服务内容怎么填代做效果图的网站好
  • 怎样批量做地级市网站个人网站做淘宝客
  • 加强网站建设管理办法学院实验室建设网站的好处
  • 网站备案查询 优帮云竹子建站官网
  • 网站开发实训报告总结2021阳江保安招聘网
  • 景区微网站 建设方案外卖小程序怎么制作
  • 郴州买房网站百度广告怎么推广
  • 西宁做网站君博认同上海企业网站优化多少钱
  • 网站小编可以在家做吗河南省城市建设网站
  • 山东企业建站系统信息网站建设公司推来客网站系统怎么样
  • 网站关键词库如何做做公司+网站建设价格低
  • 网站建设解决方登录设备管理
  • 公司网站制作定制在西安建设工程交易中心网站
  • 手机网站开发要哪些人南京百度seo代理
  • 初创品牌网站建设网站和app的关系
  • 网站制作是怎样做的微信放在网站根目录
  • 海口网站建设方面网站建设 业务
  • 网站防恶意注册网站模版网
  • 湖南做网站磐石网络案例绍兴网站建设设计制作
  • php网站开发试题及答案网站建设单子
  • p2p做网站建设网站都需要注意什么
  • 新手做免费网站免费视频网站怎么赚钱
  • 网站建设需要入无形资产吗响应式网站建设的未来发展6
  • 网站开发公司网站模板抖音商家页面模板
  • 一 美食 视频网站模板下载安装手机设计
  • php开源企业网站安徽网新科技有限公司网站开发
  • 网站机房建设成本ui最好的网站
  • 三星网站建设内容p图做网站兼职
  • 湖北企业响应式网站建设价位福田专业网站建设公司哪家好