SpringCloud学习记录(2)— Ribbon 服务消费者(Finchley篇)

前言

上一篇文章中,讲了服务的注册与发现。在微服务的开发模式中,每个不同的业务都会被拆分成一个服务,服务于服务之间采用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
本文出自方志朋的博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值