前情回顾
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-two 的application.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这两个服务。说明两个服务中心自动提供了服务均衡负载的功能。