当前位置: 移动技术网 > IT编程>开发语言>Java > SpringCloud从入门到精通教程(三)- 服务消费者,实现方式一(ribbon)

SpringCloud从入门到精通教程(三)- 服务消费者,实现方式一(ribbon)

2020年07月14日  | 移动技术网IT编程  | 我要评论

需求背景

服务消费者,实现方式一:ribbon

Ribbon是什么?

Ribbon是一个客户端组件,提供了一系列完善的配置项,如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。简单地说,Ribbon是一个客户端负载均衡器。

Ribbon工作时分为两步:

  1. 第一步先选择 Eureka Server,它优先选择在同一个Zone且负载较少的Server。
  2. 第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。

其中Ribbon提供了多种策略,例如轮询、随机、根据响应时间加权等。

Tips技术点

1. 引入ribbon组件包

代码演示

1. 项目目录结构

2. pom.xml依赖组件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<artifactId>microservice-consumer-movie-ribbon</artifactId>
	<packaging>jar</packaging>

	<parent>
		<groupId>com.minbo.cloud</groupId>
		<artifactId>spring-cloud-microservice-study</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<!-- 整合ribbon -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>
</project>

3. application.yml配置文件

server:
   port: 8010
spring:
   application:
      name: microservice-consumer-movie-ribbon
eureka:
   client:
      serviceUrl:
         defaultZone: http://localhost:8761/eureka/
   instance:
      preferIpAddress: true

4. 消费接口类

RibbonService.java

package com.minbo.cloud.study.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RibbonService {

	@Autowired
	private RestTemplate restTemplate;

	public String hiService(String name) {
		String result = restTemplate.getForObject("http://microservice-provider-user/hi?name=" + name, String.class);
		System.out.println(result);
		return result;
	}
}

RibbonController.java

package com.minbo.cloud.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.minbo.cloud.study.service.RibbonService;

@RestController
public class RibbonController {

	@Autowired
	private RibbonService ribbonService;

	@GetMapping(value = "/hi")
	public String hi(@RequestParam String name) {
		return this.ribbonService.hiService(name);
	}
}

5. 启动类

package com.minbo.cloud.study;

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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。
 * Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。
 * 简单的说,就是在配置文件中列出Load Balancer后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。
 * 我们也很容易使用Ribbon实现自定义的负载均衡算法。简单地说,Ribbon是一个客户端负载均衡器。
 * 
 * Ribbon工作时分为两步:
 * 第一步先选择 Eureka Server,它优先选择在同一个Zone且负载较少的Server;
 * 第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。
 * 
 * 其中Ribbon提供了多种策略,例如轮询、随机、根据响应时间加权等。
 */
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class MovieRibbonApplication {
	/**
	 * 实例化RestTemplate,通过@LoadBalanced注解开启均衡负载能力.
	 * @return restTemplate
	 */
	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}

	public static void main(String[] args) {
		SpringApplication.run(MovieRibbonApplication.class, args);
	}
}

6. 是否消费接口成功

启动服务后,可以查看Eureka界面注册信息

访问本服务接口:http://localhost:8010/hi?name=minbo

表示已经成功消费了服务提供者的接口(服务提供者监听端口是8000)

注:第一次启动访问接口时,会有点慢,可能会出现以下错误信息,多刷新几次接口访问就好了

java.lang.IllegalStateException: No instances available for microservice-provider-user

完整源码下载

我的Github源码地址:

https://github.com/hemin1003/spring-cloud-study/tree/master/spring-cloud-greenwich/microservice-consumer-movie-ribbon

上一章教程

SpringCloud从入门到精通教程(二)- 服务提供者

下一章教程

SpringCloud从入门到精通教程(四)- 服务消费者,方式二(feign)

该系列教程

SpringCloud从入门到精通教程

我的专栏

 

 

至此,全部介绍就结束了

 

 

-------------------------------

-------------------------------

 

我的CSDN主页

(个人域名)

我的开源项目集Github

 

期望和大家一起学习,一起成长,共勉,O(∩_∩)O谢谢

欢迎交流问题,可加个人QQ 469580884,

或者,加我的群号 751925591,一起探讨交流问题

不讲虚的,只做实干家

Talk is cheap,show me the code

本文地址:https://blog.csdn.net/hemin1003/article/details/107320219

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网