Ribbon实现负载均衡
1、在pom配置文件中一如ribbon包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
2、在配置文件中配置eureka的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/
3、在微服务启动类中添加”@LoadBalanced”注解
package com.bfxy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient //标识具体的一个服务,需要向注册中心注册
@SpringBootApplication //SpringBoot 核心配置
public class Application {
@Bean
@LoadBalanced //用于实现内部的服务负载均衡机制: service-id service-name
public RestTemplate restTemplate(){
HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpComponentsClientHttpRequestFactory.setConnectTimeout(10000);
httpComponentsClientHttpRequestFactory.setConnectionRequestTimeout(10000);
httpComponentsClientHttpRequestFactory.setReadTimeout(20000);
return new RestTemplate(httpComponentsClientHttpRequestFactory);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Ribbon自定义负载均衡
1、在pom中新增ribbon包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
2、新建规则类”RuleConfiguration.java”和接口“ExcludeComponentScan.java”
package com.bfxy;
public @interface ExcludeComponentScan {
}
package com.bfxy;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Configuration;
@Configuration
@ExcludeComponentScan
public class RuleConfiguration {
public IRule ribbonRule(){
return new RandomRule();
}
}
3、在启动类中使用@RiibonClient,添加使用新规则的微服务
package com.bfxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@RibbonClient(name="service-a",configuration=RuleConfiguration.class)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value=ExcludeComponentScan.class)})
public class FoundationZookeeperApplication {
public static void main(String[] args) {
SpringApplication.run(FoundationZookeeperApplication.class, args);
}
}