SpringCloud教程 | 三.服务消费者(Ribbon)

Ribbon简介

Ribbon是一个基于HTTP和TCP客户端的负载均衡器。Feign中也使用Ribbon,后续会介绍Feign的使用。


Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用。

当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务端列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。


实战

一.环境准备

1.启动服务注册中心:spring-cloud-eureka

2.启动服务提供方:spring-cloud-eureka-client【端口为2001】

3.修改spring-cloud-eureka-client中配置的端口号server.port(需与2中不一样),在2的基础上再次启动【端口为2002】


详情如下:SpringCloud教程 | 一.服务的注册与发现(Eureka)


二.建立服务消费者

1.新建项目,名称为spring-cloud-eureka-ribbon,引入对应jar,详细pom.xml如下:

注意】切勿忘了添加spring-cloud-starter-eureka的相关jar包,否则会有问题



	4.0.0

	aikucun
	eureka-client
	0.0.1-SNAPSHOT
	war

	spring-cloud-eureka-ribbon
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.9.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Edgware.SR1
	

	
		
			org.springframework.cloud
			spring-cloud-starter-ribbon
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		
		
	       org.springframework.cloud
	       spring-cloud-starter-eureka
	   
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


2.application.properties配置文件如下

server.port=8764
spring.application.name=eureka-ribbon
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/


3.在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册,并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

package com.aikucun;

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.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudEurekaRibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudEurekaRibbonApplication.class, args);
	}
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}


4.创建服务TestService去调用服务提供者提供的服务

【注意】

1.这里的EUREKA-CLIENT为服务提供者名字的大写,在注册中心有显示【地址栏输入eureka.client.serviceUrl.defaultZone的地址会显示注册中心的页面】

2.这边的String.class指返回类型【具体可以查看代码】

3.不能忘了相关注解

package com.aikucun.service;

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

/** 
* @FileName TestService.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年1月18日 上午10:25:51 
* 修改历史:
* 时间           作者          版本        描述
*====================================================  
*/
@Service
public class TestService {
	@Autowired
    RestTemplate restTemplate;

    public String hiService() {	
        return restTemplate.getForObject("http://EUREKA-CLIENT/dc",String.class);
    }
}
SpringCloud教程 | 三.服务消费者(Ribbon)_第1张图片


5.创建TestController

package com.aikucun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.aikucun.service.TestService;

/** 
* @FileName TestController.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年1月21日 下午1:08:26 
* 修改历史:
* 时间           作者          版本        描述
*====================================================  
*
*/
@RestController
public class TestController {
	@Autowired
	TestService testService;
	
	@RequestMapping("/hi")
	public String testController() {
		return testService.hiService();
	}
}

6.请求http://localhost:8764/hi

两个服务交替显示,这说明当我们通过调用restTemplate.getForObject("http://EUREKA-CLIENT/dc",String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

SpringCloud教程 | 三.服务消费者(Ribbon)_第2张图片

SpringCloud教程 | 三.服务消费者(Ribbon)_第3张图片


你可能感兴趣的:(SpringCloud,SpringCloud教程)