使用Ribbon作为服务消费者

Ribbon是一个基于Http和TCP的负载均衡工具

一、环境准备

在上一篇博客的基础上,开启 Eureka–Server 与 Eureka–Client项目。Eureka服务注册与服务发现
Eureka–Client 项目开启后,将配置文件中的端口从8762改为8763,再启动一次。这好比启动了两个服务器,模拟一个分布式项目,同一个项目分别部署在8762端口与8763端口。
IDEA开启多实例方式如下:
1.1在IDEA上点击Application右边的下三角,弹出选项后,点击Edit Configuration
在这里插入图片描述
1.2打开配置后,选择需要开启的Application,打钩Allow running in parallel,再保存。
在这里插入图片描述
启动后,访问http://localhost:8761/,在注册中心中应该就能看到同一个服务器名,有两个对应的端口。
在这里插入图片描述

二、建一个服务消费者

右击项目根目录,New–Module–选择左侧的Maven,点击next–输入新模块的名字ribbon。

1.1导入依赖

<?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">
    <parent>
        <artifactId>main</artifactId>
        <groupId>com.safesoft</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ribbon</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </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>
        </dependency>
    </dependencies>
</project>

2.2编写application.yml配置文件

#当前项目要被部署的端口
#8761是注册中心  8762是服务提供者一号  8763是服务提供者二号
server:
  port: 8764

#当前ribbon项目注册到下面配置的注册中心中
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

#当前项目名
spring:
  application:
    name: service-ribbon

2.3编写启动类

@EnableEurekaClient表明这是一个服务提供者
@EnableDiscoveryClient表示向服务中心注册
@LoadBalanced表明RestTemplate工具类将会负载均衡

package com.safesoft.ribbon;

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;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ApplicationRibbon {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

2.4编写服务接口

HelloController与HelloService,在Service中,访问其它服务器的接口。
HelloController.class

package com.safesoft.ribbon.web;

import com.safesoft.ribbon.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @RequestMapping("hello")
    public String hello(String name){
        //调用Service接口,并返回JSON数据
        return helloService.obtainOtherServiceData(name);
    }
}

HelloService.class

package com.safesoft.ribbon.service;

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

@Service
public class HelloService {

    /**
     * 将IOC容器中的负载均衡RestTemplate工具注入进来
     */
    @Autowired
    RestTemplate restTemplate;

    public String obtainOtherServiceData(String name) {
        //尝试调用其它微服务接口,访问的是SERVICE-CLIENT这个服务器的/hi 接口
        return restTemplate.getForObject("http://SERVICE-CLIENT/hello?name=" + name, String.class);
    }
}

2.5启动ribbon项目

在浏览器上输入:http://localhost:8764/hello?name=pc,第一次刷新结果如下:
在这里插入图片描述
第二次刷新结果如下:
在这里插入图片描述
多次刷新发现:发送给8764端口的请求,被分发到SERVICE-CLIENT项目。且分别交替发给部署在8762与8763的服务器。

三、源码下载和目录结构

1.1项目源码

spring-cloud-01

2.1项目整体目录

在这里插入图片描述
参考文章
https://blog.csdn.net/yanluandai1985/article/details/86628111
https://blog.csdn.net/forezp/article/details/76408139
https://www.jianshu.com/p/59295c91dde7
https://blog.csdn.net/xiaojin21cen/article/details/86704954

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值