Ribbon及Feign接口使用--2

Ribbon(实现基于客户端负载均衡)

1、Ribbon介绍

负载均衡
1、服务器端负载均衡:
  例如:Nginx,通过Nginx进行负载均衡,先发送请求,然后通过负载均衡算法,在多个服务器之间选择一个进行访问;即在服务器端再进行负载均衡算法分配。

2、客户端负载均衡:
  例如:spring cloud中的ribbon,客户端会有一个服务器地址列表,在发送请求前通过负载均衡算法选择一个服务器,然后进行访问,这是客户端负载均衡;即在客户端就进行负载均衡算法分配。
  ribbon的默认负载均衡(轮询),当多次访问Eureka(注册中心)中同一个服务时(该服务有多个服务提供者),采用轮询方式,也可自定义配置。
Ribbon介绍
Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现。通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的服务调用。Spring Cloud Ribbon虽然只是一个工具类框架,它不像服务注册中心、配置中心、API网关那样需要独立部署,但是它几乎存在于每一个Spring Cloud构建的微服务和基础设施中。因为微服务间的调用,API网关的请求转发等内容,实际上都是通过Ribbon来实现的,包括后续我们将要介绍的Feign,它也是基于Ribbon实现的工具。所以,对Spring Cloud Ribbon的理解和使用,对于我们使用Spring Cloud来构建微服务非常重要。

2、Ribbon基本使用

在consumer启动类上加入启用Ribbon的注解,并指定实现负载均衡的provider,就会自动实现负载均衡,其余代码同上一节

@SpringBootApplication
@EnableEurekaClient
@RibbonClient("provider")
public class ConsumerApp {
	public static void main(String[] args) {
		SpringApplication.run(ConsumerApp.class, args);
	}
	
	/*RestTemplate必须手动来注入,否责使用时无法用@Autowired自动注入*/
	@Bean
	public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
		return new RestTemplate(factory);
	}
 
	@Bean
	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setReadTimeout(180000);//单位为ms
		factory.setConnectTimeout(5000);//单位为ms
		return factory;
	}
}

结果
在这里插入图片描述
使用前
在这里插入图片描述

使用后
在这里插入图片描述

3、自定义Ribbon负载均衡算法

自定义需要添加一个配置文件,在其中实现所需的方法并注入,然后在consumer启动类中通过@RibbonClient来指定,从而达到使用自定义所指定的负载均衡算法的目的。
启动类位置必须在启动类包外
在这里插入图片描述

consumer中的配置类–配置中的Bean方法可通过注解@RibbonClient查看源码

/**
 * 此类为自定义负载均衡模式类
 * 必须放置在启动类包外,否则会报错
 * @author l
 *
 */

@Configuration
public class RibbonConfig {
	
	@Autowired
	IClientConfig icCofing;
	
	/**
	 * 创建负载均衡算法的方法
	 * @param config
	 * @return
	 */
	@Bean
	public IRule ribbonRule(IClientConfig config) {
		/*返回随机算法*/
		return new RandomRule();
	}
}

consumer中的启动类–指定相应的配置类

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "provider",configuration = RibbonConfig.class)
public class ConsumerApp {
	public static void main(String[] args) {
		SpringApplication.run(ConsumerApp.class, args);
	}
	
	/*RestTemplate必须手动来注入,否责使用时无法用@Autowired自动注入*/
	@Bean
	public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
		return new RestTemplate(factory);
	}
 
	@Bean
	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setReadTimeout(180000);//单位为ms
		factory.setConnectTimeout(5000);//单位为ms
		return factory;
	}
}

结果–随机使用
在这里插入图片描述

4、挪动Ribbon自定义配置文件方式

自定义负载均衡配置类必须放置在启动类包外,否则会报错,但可通过自定义注解及@ComponentScan来实现位置的改变
目录结构
在这里插入图片描述

自定义注解配置

/**
 * 自定义注解用于标记
 * @author l
 *
 */
public @interface ExcludeAnnotation {}

给配置类标记自定义注解

@Configuration
/*添加此注解则不进行扫描,此时可放在启动类包内部*/
@ExcludeAnnotation
public class RibbonConfig {
	@Autowired
	IClientConfig icCofing;
	
	/**
	 * 创建负载均衡算法的方法
	 * @param config
	 * @return
	 */
	@Bean
	public IRule ribbonRule(IClientConfig config) {
		/*返回随机算法*/
		return new RandomRule();
	}
}

启动类进行过滤扫描,排除配置类

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "provider",configuration = RibbonConfig.class)
/*添加value中注解的类,不进行扫描*/
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeAnnotation.class)})
public class ConsumerApp {
	public static void main(String[] args) {
		SpringApplication.run(ConsumerApp.class, args);
	}
	
	/*RestTemplate必须手动来注入,否责使用时无法用@Autowired自动注入*/
	@Bean
	public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
		return new RestTemplate(factory);
	}
 
	@Bean
	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setReadTimeout(180000);//单位为ms
		factory.setConnectTimeout(5000);//单位为ms
		return factory;
	}
}

5、配置文件配置Ribbon

在application.yml中配置Ribbon

#配置后不需要在使用配置类
provider:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

Feign接口的使用

Feign是声明性的web服务客户端。它使编写web服务客户端更加容易。

1、Feign基本使用

pom依赖

<!-- feign依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

Feign接口定义—要和provider中controller访问路径相同
provider中controller

@RestController
@RequestMapping("provider")
public class ProviderController {
	
	@GetMapping("/getMsg")
	public String returnToConsumer() {
		return "服务提供者返回的消息";
	}
}

feign接口

@FeignClient(name = "provider")
public interface ConsumerFeign {
	
	@GetMapping("provider/getMsg")
	public String getMsg();
}

Feign接口使用

@RestController
@RequestMapping("consumer")
public class ConsumerController {
	
	@Autowired
	private ConsumerFeign consumerFeign;
	
	@PostMapping("/getMsg")
	public void getProvideMsg() {
		System.out.println(consumerFeign.getMsg());
	}
}

开启Feign
启动类添加@EnableFeignClients

2、Feign的自定义配置

feign接口另一种方式–使用feign自己的接收方式
a、配置文件

@Configuration
public class FeignConfiguration {
	@Bean
	public Contract feignContract() {
		return new feign.Contract.Default();
	}
}

b、feign接口

@FeignClient(name = "provider",configuration = FeignConfiguration.class)
public interface ConsumerFeign {
	
	//@GetMapping("provider/getMsg")
	@RequestLine("GET provider/getMsg")
	public String getMsg();
}

3、Feign URL方式和配置请求用户名和密码

Feign接口
url指向provider服务的端口

@FeignClient(name = "provider",url = "http://localhost:8888/",configuration = FeignConfigUrl.class)
public interface ConsumerFeign {
	
	//@GetMapping("provider/getMsg")
	@RequestLine("GET provider/getMsg")
	public String getMsg();
}

Feign配置文件

@Configuration
public class FeignConfigUrl {
	@Bean
	public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
		return new BasicAuthRequestInterceptor("root", "Admin123");
	}
}

4、Feign输出日志

第一种
java配置


/**
 * Feign配置
 * 该配置放到SpringBoot可以扫描到的路径下
 */
@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLevel() {
        return Logger.Level.FULL;
    }
}

第二种
application.xml配置


/**
 * Feign配置
 * 该配置放到SpringBoot可以扫描到的路径下
 */
@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLevel() {
        return Logger.Level.FULL;
    }
}

5、Feign超时设置

#超时配置
feign:
  client:
    config:
      default:
        connectTimeout: 10000
        readTimeout: 10000
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值