SpringCloud - (三)负载均衡

SpringCloud - (三)负载均衡

前情回顾

在这里插入图片描述

1、Eureka Server

提供服务注册和发现

2、Service Provider

服务提供方
将自身服务注册到Eureka,从而使服务消费方能够找到

3、Service Consumer

服务消费方
从Eureka获取注册服务列表,从而能够消费服务

负载均衡

在这里插入图片描述

Service provider one,Service provider two 都提供相同的服务。

项目总览
在这里插入图片描述
eureka-provider-two 的创建和eureka-provider-one相同,不在赘述。
在配置文件中改动端口:
eureka-provider-twoapplication.yml如下:

# 配置服务器端口
server:
  port: 8911
spring:
  application:
    name: cloud-eureka-provider
# 注册服务
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/

为了便于区分,将HelloController 的first 改成second。如下:

package com.wy.eurekaprovidertwo.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/index/{name}")
    public String index(@PathVariable("name") String name) {
        return "hello "+name+",this is second messge";
    }


    @RequestMapping("/test")
    public String index() {
        return "hello this is second messge";
    }

    @RequestMapping("/hello")
    public String hello(@RequestParam String name) {
        return "hello "+name+",this is second messge";
    }

}

依次启动eureka-server、eureka-provider-one、eureka-provider-two、eureka-consumer-feign。

访问http://localhost:8921/feign/hello/wy,会看到如下图:
在这里插入图片描述
再刷新依次:
在这里插入图片描述

到这里我们发现,eureka-consumer-feign轮询访问eureka-provider-one、eureka-provider-two这两个服务。说明两个服务中心自动提供了服务均衡负载的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值