前言
在上一篇文章中,讲了服务的注册与发现。在微服务的开发模式中,每个不同的业务都会被拆分成一个服务,服务于服务之间采用Http、Restful相互通讯。Spring Cloud有两种服务的调用方式,一种是Rest + RestTemplate,另一种是Feign,这篇文章给大家分享基于Ribbon + Rest。
一、什么是Ribbon
1.1、Ribbon简介
Ribbon是一个负载均衡客户端,它基于Netflix Ribbon,可以很好的控制HTTP及TCP的一些行为;Ribbon是一个工具框架,不需要部署,它几乎存在于每一个通过Spring Cloud构建的项目中,Feign也是基于Ribbon。
Ribbon已经默认实现了这些配置Bean:
1、IClientConfig ribbonClientConfig: DefaultClientConfigImpl
2、IRule ribbonRule: ZoneAvoidanceRule
3、IPing ribbonPing: NoOpPing
4、ServerList ribbonServerList: ConfigurationBasedServerList
5、ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
二、新建-服务消费者(Ribbon)
1.1、说明
本文基于上一篇文章的项目,启动Eureka-Server(端口:8080);启动Eureka-Client(端口:8081);将Eureka-Client的application.yml文件中断开修改为8082,再次启动Eureka-Client,你会发现Eureka-Client在Eureka-Server中注册了两个实例,这相当于一个小的集群;
如何在Idea中一个项目启动多个实例:
Edit Configuration… > 选择项目 > 勾选Allow parallel run


修改该项目的端口 > 启动

访问Eureka Server “http://localhost:8080”:

1.2、新建Module工程(Service-Ribbon)
和Eureka-client一样,他其实也是一个Client,pom配置如下:
<?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>
<groupId>com.yangx.springcloud</groupId>
<artifactId>service-ribbon</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ServiceRibbon</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.yangx.springcloud</groupId>
<artifactId>yangx-eureka</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
</project>
在application.yml配置端口及服务注册中心地址:
server:
port: 8083
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka/
spring:
application:
name: server-ribbon
在项目的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的IOC容器中注入一个Bean: RestTemplate;并通过@LoadBalanced注解表明这个RestRemplate开启负载均衡的功能
package com.yangx.springcloud.service.ribbon.controller;
import com.yangx.springcloud.service.ribbon.service.HelloService;
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;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("hello")
String hello(@RequestParam String name){
return helloService.hello(name);
}
}
写一个测试类HelloServiceImpl实现与测试接口HelloService,通过之前注入IOC容器的RestTemplate来消费Eureka-Client的“hello”接口,这里我们直接用服务名代替了具体的URL地址,在Ribbon中它会根据服务名来选择具体的服务实例,服务实例在请求的时候会用具体的URL地址替换掉服务名,代码如下:
package com.yangx.springcloud.service.ribbon.service.impl;
import com.yangx.springcloud.service.ribbon.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloServiceImpl implements HelloService {
@Autowired
private RestTemplate restTemplate;
@Override
public String hello(String name) {
return restTemplate.getForObject("http://EUREKA-CLIENT/hello?name="+name,String.class);
}
}
写一个Controller调用HelloServiceImpl的hello方法,代码如下:
package com.yangx.springcloud.service.ribbon.controller;
import com.yangx.springcloud.service.ribbon.service.HelloService;
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;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("hello")
String hello(@RequestParam String name){
return helloService.hello(name);
}
}
在浏览器多次访问http://localhost:8081/hello?name=test,浏览器交替显示以下内容:
Hello test ! I am from port 8081
Hello test ! I am from port 8082
这说明我们在调用restTemplate的getForObject方法时已经做了负载均衡,访问不同端口的服务实例。
http://blog.csdn.net/forezp/article/details/81040946
本文出自方志朋的博客
— Ribbon 服务消费者(Finchley篇)&spm=1001.2101.3001.5002&articleId=89436675&d=1&t=3&u=e2a374cf4a2f41df84f094d861f74300)
533

被折叠的 条评论
为什么被折叠?



