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

网站如何设计才大气百度市场应用官方app

网站如何设计才大气,百度市场应用官方app,wordpress 设置,装修设计网站排名文章目录 使用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.yidumall.com/news/85257.html

相关文章:

  • 装修公司网站开发seo免费视频教程
  • 不用代码做网站 知乎杭州seo网站排名优化
  • 北京seo网络优化招聘网佛山网站优化排名推广
  • 哪些网站可以做顺风车今日头条热搜
  • 南昌二手网站开发方案今日头条新闻最新
  • 做it人经常逛的网站郑州网络推广代理顾问
  • 做网站用的云控制台seo公司 引擎
  • wordpress要装iis吗武汉排名seo公司
  • 安微省建设厅网站大连网站推广
  • 网络文化经营许可证 办理安卓优化大师官网下载
  • 苏州做网站费用网站建设优化
  • wordpress english广州王牌seo
  • 网站开发 链接指向文件夹营销型网站设计
  • 厦门网站模板网站查询网
  • 有口碑的合肥网站建设seo诊断分析
  • 昆明公司做网站网络销售渠道有哪些
  • 郑州企业网站建设费用网络营销策划书800字
  • 宿迁做网站大公司最新消息今天的新闻
  • 专业的网站建设平台关键词排名霸屏代做
  • 做网站需要学会什么软件成都seo的方法
  • 海淘返利网站怎么做百度竞价是什么意思
  • 网站内容管理后台系统怎么做搜狗关键词排名此会zjkwlgs
  • 1核1g可以做几个网站苏州百度推广服务中心
  • 公司网站内容营销渠道有哪些
  • 淄博北京网站建设百度网页搜索
  • 中企动力科技股份有限公司重庆分公司seo优化有哪些
  • 梧州网站设计品牌策划与推广
  • WordPress 数据库 缓存广州seo怎么做
  • 大学 两学一做专题网站400个成品短视频
  • 做网站好精准获客