1.服务提供者只需要启动多个服务实例并注册到一个注册中心或者时多个相关联的服务注册中心
2.服务消费者直接通过调用被@LoadBanced注册修饰过的RestTemplate来实现面向服务的接口调用
在eureka-consumer中已经通过调用实现了负载均衡
package com.study.cloud.consumer.configs;
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 WebConfig {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
3.服务容错保护hystrix
向eureka-consumer中pom中pom中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
修改启动类