使用IDEA开发Spring Cloud项目(四)创建服务消费者(Ribbon)

概述

在上一篇文章,我们讲了服务的注册和发现。在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest

本系列源码地址:https://github.com/lhmyy521125/toher-springcloud-sample

ribbon简介

ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。

ribbon 已经默认实现了这些配置bean:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl

  • IRule ribbonRule: ZoneAvoidanceRule

  • IPing ribbonPing: NoOpPing

  • ServerList ribbonServerList: ConfigurationBasedServerList

  • ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

准备工作

复制一份上一章节的项目,取名为 toher-springcloud-sample2-ribbon

  • 启动eureka-server工程
  • 启动服务提供者 service-hello,端口号为:9001
  • 修改配置文件的端口号为:9002,启动后在 Eureka 中会注册两个实例,这相当于一个小集群
    在这里插入图片描述

创建服务消费者

还是继续创建一个module , 取名为:service-ribbon; 在它的pom.xml继承了父pom文件,并引入了以下依赖:

为了规范与可读性,在创建SpringBoot 服务消费者工程过程中,博主推荐不选择Eureka Server, ,而是选择Eureka Discovery Client,这样我们见名知意,虽然Eureka Server也是可以注册到服务中心的

在这里插入图片描述

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>cn.toher</groupId>
        <artifactId>springcloud-sample</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>cn.toher</groupId>
    <artifactId>service-ribbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-ribbon</name>
    <description>Demo project for Spring Boot</description>

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


</project>


在工程的配置文件指定服务的注册中心地址,程序名称为 service-ribbon,程序端口为9003。配置文件application.yml如下:

spring:
  application:
    name: service-ribbon

server:
  port: 9003

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

通过 @EnableDiscoveryClient 注解注册到服务中心,并创建RestTemplate 通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {

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

    //通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

创建一个service HelloRibbonService ,通过spring.application.name 的项目名称来设置消费的服务提供者;在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名

package cn.toher.serviceribbon.service;

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

/**
 * @Author: 同恒科技-李怀明
 * @Date: 2019/10/19 10:54
 */
@Service
public class HelloRibbonService {

    @Autowired
    private RestTemplate restTemplate;

    public String helloRibbon(String name) {
        return restTemplate.getForObject("http://SERVICE-HELLO/hello?name=" + name, String.class);
    }
}

创建测试用的 Controller,代码如下:

package cn.toher.serviceribbon.controller;

import cn.toher.serviceribbon.service.HelloRibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 同恒科技-李怀明
 * @Date: 2019/10/19 10:58
 */
@RestController
public class HelloRibbonController {

    @Autowired
    private HelloRibbonService helloRibbonService;

    @RequestMapping("/helloRibbon")
    public String helloRibbon(String name){
        return helloRibbonService.helloRibbon(name);
    }
}

运行该工程,查看注册中心(下图) 证明我们已经创建完成,接下来测试一下;
在这里插入图片描述
浏览器中输入:http://localhost:9003/helloRibbon?name=Ribbon , 会发现浏览器输出信息中端口号的变化,证明负载已经生效

hi Ribbon ,this is Ribbon Project , i am from port:9001
hi Ribbon ,this is Ribbon Project , i am from port:9002

结语:本章我们学习了如何使用Ribbon实现服务与服务的通讯并实现负载均衡,大家可以根据博主的代码样例,进行相关测试学习;

下一篇:创建服务消费者(Feign)

  • 16
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micro麦可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值