网页设计与制作页面,商城网站优化方案,利趣网站开发商,如何快速优化网站什么是负载均衡#xff1f;
第一种轮询算法#xff0c;依次遍历去执行#xff0c;达到负载均衡 集成Ribbon
导入pom#xff0c;在消费者服务里的pom文件导入 !-- Ribbon 集成 --!-- https://mvnrepository.com/artifact/org.springframework.cloud/spr…
什么是负载均衡
第一种轮询算法依次遍历去执行达到负载均衡 集成Ribbon
导入pom在消费者服务里的pom文件导入 !-- Ribbon 集成 --!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-ribbon/artifactIdversion1.4.6.RELEASE/version/dependency
还要导入Eureka客户端 !-- Eureka --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-eureka/artifactIdversion1.4.6.RELEASE/version/dependency然后消费者服务里的yml
server:port: 80#Eureka 配置
eureka:client:register-with-eureka: false #不向Eureka注册自己service-url:defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
主启动类加上注解
package com.kuang.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;SpringBootApplication
EnableEurekaClient
public class DeptConsumer80 {public static void main(String[] args) {SpringApplication.run(DeptConsumer80.class,args);}
}在配置类上加一个注解即可实现Ribbon的负载均衡的RestTemplate
package com.kuang.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 ConfigBean {//配置负载均衡实现RestTemplate,只需要加个注解LoadBalanced ,它去注册中心去请求的时候会平均分请求每个eureka中的注册的服务BeanLoadBalanced //Ribbon 基于这个注解实现的public RestTemplate restTemplate(){return new RestTemplate();}}在Controller访问那里别写死了改成 服务名因为服务在三个注册中心Eureka上面都注册了但是都是同一个服务名三个服务器相互绑定你只需要该地址为服务名他会用负载均衡算法来计算出下一个将要访问的服务器
package com.kuang.springcloud.controller;import com.kuang.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.List;RestController
public class DeptConsumerController {//理解消费者不应该有Service层//模板有很多方法供我们去使用//注入过来
//(String url, 实体map, ClassT responseType, Object... uriVariables)
//(url,实体,map,ClassT responseType 返回值类型) 三个参数 五种请求方式Autowiredprivate RestTemplate restTemplate;
//Ribbon 我们这里的地址应该是一个变量通过服务名字来访问// private static final String REST_URL_PREFIXhttp://localhost:8001;private static final String REST_URL_PREFIXhttp://SPRINGCLOUD-PROVIDER-DEPT;RequestMapping(/consumer/dept/get/{id})public Dept get(PathVariable(id)Long id){return restTemplate.getForObject(REST_URL_PREFIX/dept/get/id,Dept.class);}RequestMapping(/consumer/dept/add)public boolean add(Dept dept){return restTemplate.postForObject(REST_URL_PREFIX/dept/add,dept,Boolean.class);}RequestMapping(/consumer/dept/list)public ListDept list(){return restTemplate.getForObject(REST_URL_PREFIX/dept/list,List.class);}}默认是随机路由
实现的是这个接口 得出来一个重大结论
Ribbon和Eureka整合以后客户端可以直接调用不用关心 ip地址和端口号~ 再来总结一下
Eureka 就是注册中心其实也是一个服务给服务提供一个注册的容器让服务可以注册进来供消费者使用它可以多个注册中心组合在一起变成一个集群在每个注册中心上服务提供者都把自己的服务放到注册中心上只要还有一个服务器没有瘫痪它照旧可以运行它属于AP可用性与容错性一体的架构。
Ribbon是让多个注册中心相互调用相当于一个整体在每个注册中心上服务提供者都会注册一个它的服务服务名称都一样所以整合以后只需要调用服务名称不用关心 ip地址和端口号~ 即可访问服务会通过负载均衡算法进行选择调用哪个 Eureka。