三、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自我保护功能








原创粉丝点击