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

儿童做的小游戏下载网站seo免费浏览网站

儿童做的小游戏下载网站,seo免费浏览网站,郑州网站制作-中国互联,深圳网站建设q.479185700惠文章目录 使用Ribbon自带负载均衡算法添加负载均衡算法ConfigurationRestTemplate使用上面负载均衡算法 自定义负载均衡算法负载均衡算法实现RestTemplate在Controller中使用该负载均衡算法ServiceIInstance解释 使用Ribbon自带负载均衡算法 添加负载均衡算法Configuration /…

文章目录

    • 使用Ribbon自带负载均衡算法
      • 添加负载均衡算法Configuration
      • RestTemplate使用上面负载均衡算法
    • 自定义负载均衡算法
      • 负载均衡算法实现
      • RestTemplate
      • 在Controller中使用该负载均衡算法
      • ServiceIInstance解释

使用Ribbon自带负载均衡算法

添加负载均衡算法Configuration

//注意package位置假设启动器为@ComponentScan("com.test.springcloud")
package com.test.config
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();//定义为随机}
}

注意:
官方文档明确给出了警告
这个自定义配置类不能放在@ComponentScan所扫描的当前包下以及子包下,
否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,达不到特殊化定制的目的了。

RestTemplate使用上面负载均衡算法

package com.test.springcloud.config
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class ApplicationContextConfig
{@Bean@LoadBalanced //添加负载均衡配置注解,如果自定义的话不需要使用该注解否则报错public RestTemplate getRestTemplate(){return new RestTemplate();}
}

自定义负载均衡算法

负载均衡算法实现

定义负载均衡算法接口

public interface LoadBalancer
{ServiceInstance instances(List<ServiceInstance> serviceInstances);
}

实现负载均衡算法

@Component
public class MyLB implements LoadBalancer
{//通过CAS(乐观锁)的方式进行,本质是通过CompareAndSet,先比较后赋值的方式,只有当前值和期望值相同时才可以进行赋值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));System.out.println("*****第几次访问,次数next: "+next);return next;}//负载均衡算法:rest接口第几次请求数 % 服务器集群总数量 = 实际调用服务器位置下标  ,每次服务重启动后rest接口计数从1开始。@Overridepublic ServiceInstance instances(List<ServiceInstance> serviceInstances){int index = getAndIncrement() % serviceInstances.size();return serviceInstances.get(index);}
}

以上实现负载均衡算法的instance可自定义

RestTemplate

package com.test.springcloud.config
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class ApplicationContextConfig
{@Bean//@LoadBalanced //去掉这个注解public RestTemplate getRestTemplate(){return new RestTemplate();}
}

在Controller中使用该负载均衡算法

public class Controller{@GetMapping(value = "/consumer/payment/lb")public String getPaymentLB(){List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");if(instances == null || instances.size() <= 0){return null;}// 在下面有解释ServiceInstanceServiceInstance serviceInstance = loadBalancer.instances(instances);URI uri = serviceInstance.getUri();return restTemplate.getForObject(uri+"/payment/lb",String.class);}}

ServiceIInstance解释

ServiceInstance 是 Spring Cloud 提供的一个接口,用于表示服务的实例信息。它提供了获取服务实例的相关信息和元数据的方法。
ServiceInstance 接口定义了以下常用方法:

  • String getServiceId(): 获取服务名称(serviceId)。
  • String getHost(): 获取服务实例的主机名。
  • int getPort(): 获取服务实例的端口号。
  • boolean isSecure(): 返回服务实例是否使用安全协议(如 HTTPS)进行通信。
  • URI getUri(): 获取服务实例的完整 URI。
  • Map<String, String> getMetadata(): 返回服务实例的元数据,这些元数据可以由服务提供方自定义。
  • 通过使用 DiscoveryClient(服务发现客户端)获取到的服务实例列表,可以进一步使用 ServiceInstance 接口来获取每个服务实例的具体信息。
    示例:
@Autowired
private DiscoveryClient discoveryClient;public void getServiceInstances(String serviceName) {List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);for (ServiceInstance instance : instances) {String serviceId = instance.getServiceId();String host = instance.getHost();int port = instance.getPort();boolean isSecure = instance.isSecure();URI uri = instance.getUri();Map<String, String> metadata = instance.getMetadata();// 打印服务实例信息System.out.println("Service ID: " + serviceId);System.out.println("Host: " + host);System.out.println("Port: " + port);System.out.println("Is Secure: " + isSecure);System.out.println("URI: " + uri);System.out.println("Metadata: " + metadata);}
}
http://www.rdtb.cn/news/21737.html

相关文章:

  • 宁德北京网站建设seo外链是什么意思
  • wordpress javascript广告插件关键词优化排名软件s
  • html5 css3 网站模板网站排行榜查询
  • 快速建站用什么在百度上打广告找谁推广产品
  • 正邦设计创始人长春seo结算
  • 做网站兰州怎么开通网站
  • 做私彩网站代发软文
  • 衡水网站公司seo专员招聘
  • 网站内容包括专业代写软文
  • 做恶搞图片的网站网站关键词优化软件效果
  • 相亲网站做推广的照片是谁自己怎么注册网站
  • .net网站开发教程网站站长seo推广
  • 公众号运营策划书宁波seo网络推广产品服务
  • 网站显示正在建设是什么意思竞价运营是做什么的
  • 静态企业网站模板下载电商培训内容有哪些
  • web2.0网站开发aseo优化文章网站
  • 唐山网站制作工具微信营销软件免费版
  • 网页美工设计推荐西安网络推广seo0515
  • iis网站右键没有属性成都最新消息今天
  • 商丘做网站用什么程序网络维护培训班
  • 做网站注意哪些成都培训机构排名前十
  • 网站建设管理自查工作总结十大教育培训机构排名
  • 闵行网站制作搜索引擎营销策略有哪些
  • 整站seo策略实施青岛seo外包服务
  • 蔬莱网站建设谷歌网站网址
  • 网站制作怎么做搜索栏市场调研的基本流程
  • 长沙营销型网站建设域名网站查询
  • 花乡做网站公司农产品网络营销推广方案
  • 牡丹江网站建设百度推广关键词规划师
  • 中央广播电视台直播入口seo运营专员