三、Spring Cloud 的 Ribbon 负载均衡
来源:互联网 发布:龙卷风软件 编辑:程序博客网 时间:2024/06/07 19:24
一、Eureka Server 端的YML 文件配置加了一个禁止自我保护
与Ribbon无关
在此只加了一个enable-self-preservation为false,禁止Eureka Server 端自我保护
spring: application: name: microservice-eureka-server-ha1 # 指定 profile=peer1 profiles: peer1server: port: 8761eureka: server: enable-self-preservation: false #禁用自我保护模式, 默认为true instance: hostname: peer1 client: #register-with-eureka: false #表示是否将自己注册到Eureka Server中,默认为true, 由于当前应用就是 Eureka Server, 故而设置为false #fetch-registry: false #表示是否从 Eureka Server中获取注册信息, 默认为true, 因为这是一个单点的 Eureka Server, 不需要同步其它的 Eureka Server 节点的数据, 故而设置为 false service-url: defaultZone: http://user:password123@peer2:8762/eureka/ # 设置与Eureka Server交互的地址, 查询服务和注册服务都需要依赖这个地址. 默认http://localhost:8761/eureka; 多个地址可以使用","分隔security: basic: enabled: true user: name: user password: password123 ---spring: application: name: microservice-eureka-server-ha2 # 指定 profile=peer2 profiles: peer2server: port: 8762eureka: server: enable-self-preservation: false #禁用自我保护模式, 默认为true instance: hostname: peer1 client: #register-with-eureka: false #表示是否将自己注册到Eureka Server中,默认为true, 由于当前应用就是 Eureka Server, 故而设置为false #fetch-registry: false #表示是否从 Eureka Server中获取注册信息, 默认为true, 因为这是一个单点的 Eureka Server, 不需要同步其它的 Eureka Server 节点的数据, 故而设置为 false service-url: defaultZone: http://user:password123@peer1:8761/eureka/ # 设置与Eureka Server交互的地址, 查询服务和注册服务都需要依赖这个地址. 默认http://localhost:8761/eureka; 多个地址可以使用","分隔security: basic: enabled: true user: name: user password: password123
二、Ribbon Eureka Client 客户端实现
1) Eureka 客户端的 pom.xml 文件配置
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency>
2) 主程序文件添加注解@LoadBalanced
package com.itmuch.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableEurekaClientpublic class CloudConsumerApplication {@Bean // 等价于 RestTemplate restTemplate = new RestTemplate();@LoadBalanced // Ribbon 负载均衡public RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(CloudConsumerApplication.class, args);}}
3) Controller 控制台调用
package com.itmuch.cloud;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.List;import org.apache.tomcat.util.http.fileupload.IOUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;import org.springframework.cloud.client.ServiceInstance;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class UserController {@Value("${user.userServiceUrl}") // 在yml文件中已经配置, 解决它的硬编码问题private String userServiceUrl;@Value("${user.userEurekaService}")private String userEurekaService;@Value("${user.userEurekaName}") private String userEurekaName;@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;@Autowiredprivate LoadBalancerClient loadBalancerClient;@GetMapping("/load-instance")public String loadEurekaInstance() {ServiceInstance serviceInstance = this.loadBalancerClient.choose("cloud-service");String rs = "{" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "}";// 打印当前选择的是哪个节点System.out.println(rs);return rs;}/** * #1 第一种 基于Eureka服务方式来调用 * * @param id * @return */@GetMapping("/eureka/{id}") public User findByIdEureka(@PathVariable Long id) {User user = this.restTemplate.getForObject("http://cloud-service/get/" + id, User.class);String rs = loadEurekaInstance();user.setEurekaServiceName(rs);return user;}}
三、测试
1) 启动HA的Eureka服务
2) 启动3-5个提供者
3) 启动消费者去调用
4) 杀掉一个提供者,测试一下Ribbon会不会再访问它, 在本文第一节禁止了Eureka Server自我保护功能
阅读全文
0 0
- 三、Spring Cloud 的 Ribbon 负载均衡
- Spring Cloud -- Ribbon负载均衡
- Spring-Cloud 负载均衡 Ribbon
- 【Spring Cloud】Ribbon负载均衡
- spring cloud ribbon自定义负载均衡策略
- Spring Cloud 客服端负载均衡 Ribbon
- Spring Cloud 客服端负载均衡 Ribbon
- Spring Cloud Ribbon(负载均衡)
- spring-cloud-ribbon负载均衡(一)
- spring-cloud-ribbon负载均衡(二)
- 负载均衡之Spring Cloud Ribbon
- 客户端负载均衡 Spring Cloud Ribbon
- Spring Cloud学习--负载均衡(Ribbon)
- spring cloud Ribbon 2 (客户端负载均衡)
- spring cloud使用ribbon实现负载均衡
- Spring Cloud的负载均衡Spring Cloud Ribbon和Spring Cloud Feign
- 【Spring Cloud】--客户端负载均衡Spring Cloud Ribbon
- spring cloud之服务调用及使用ribbon实现负载均衡(三)
- HDU
- ApsaraDB for HBase性能/延时全面领先社区版本
- base64原理
- 【19】vue.js — 父子组件
- SpringCloud Feign参数传递问题记录
- 三、Spring Cloud 的 Ribbon 负载均衡
- python cv2 中cv2.imread和cv2.resize输入输出参数顺序
- 利用wmic查看本机杀软安装情况
- 手动或主动抛出异常
- PMOS防止倒灌电路
- Android 的全新的 Play Widget
- 注解创建和使用
- React Native学习(一)——搭建开发环境
- Maven 集成Tomcat插件