Ribbon是一个基于HTTP和TCP客户端的负载均衡器。Feign中也使用Ribbon,后续会介绍Feign的使用。
当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务端列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。
1.启动服务注册中心:spring-cloud-eureka
2.启动服务提供方:spring-cloud-eureka-client【端口为2001】
3.修改spring-cloud-eureka-client中配置的端口号server.port(需与2中不一样),在2的基础上再次启动【端口为2002】
详情如下:SpringCloud教程 | 一.服务的注册与发现(Eureka)
1.新建项目,名称为spring-cloud-eureka-ribbon,引入对应jar,详细pom.xml如下:
【注意】切勿忘了添加spring-cloud-starter-eureka的相关jar包,否则会有问题
4.0.0
aikucun
eureka-client
0.0.1-SNAPSHOT
war
spring-cloud-eureka-ribbon
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
Edgware.SR1
org.springframework.cloud
spring-cloud-starter-ribbon
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
server.port=8764
spring.application.name=eureka-ribbon
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
package com.aikucun;
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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudEurekaRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
【注意】
1.这里的EUREKA-CLIENT为服务提供者名字的大写,在注册中心有显示【地址栏输入eureka.client.serviceUrl.defaultZone的地址会显示注册中心的页面】
2.这边的String.class指返回类型【具体可以查看代码】
3.不能忘了相关注解
package com.aikucun.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @FileName TestService.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年1月18日 上午10:25:51
* 修改历史:
* 时间 作者 版本 描述
*====================================================
*/
@Service
public class TestService {
@Autowired
RestTemplate restTemplate;
public String hiService() {
return restTemplate.getForObject("http://EUREKA-CLIENT/dc",String.class);
}
}
5.创建TestController
package com.aikucun.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.aikucun.service.TestService;
/**
* @FileName TestController.java
* @Description:TODO
* @author JackHisen(gu.weidong)
* @version V1.0
* @createtime 2018年1月21日 下午1:08:26
* 修改历史:
* 时间 作者 版本 描述
*====================================================
*
*/
@RestController
public class TestController {
@Autowired
TestService testService;
@RequestMapping("/hi")
public String testController() {
return testService.hiService();
}
}
6.请求http://localhost:8764/hi
两个服务交替显示,这说明当我们通过调用restTemplate.getForObject("http://EUREKA-CLIENT/dc",String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。