走进Spring Cloud之四 eureka ribbon(负载均衡服务调用者)(Greenwich版本)
Spring Cloud Ribbon
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯都是HTTP RESTful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。
Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。它是一个基于HTTP和TCP的客户端负载均衡器。它可以通过在客户端中配置ribbonServerList来设置服务端列表去轮询访问以达到均衡负载的作用。
当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务实例列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。
而当Ribbon与Consul联合使用时,ribbonServerList会被ConsulServerList来扩展成从Consul获取服务实例列表。同时由ConsulPing来作为IPing接口的实现。
我们在使用Spring Cloud Ribbon的时候,不论是与Eureka还是Consul结合,都会在引入Spring Cloud Eureka或Spring Cloud Consul依赖的时候通过自动化配置来加载上述所说的配置内容,所以我们可以快速在Spring Cloud中实现服务间调用的负载均衡。
service-consumer-ribbon
新建消费者模块
new ->moudle ->service-consumer-ribbon
pom.xml
修改pom.xml添加ribbon依赖:
<?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>scexample</artifactId>
<groupId>com.pubutech</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-cosumer-ribbon</artifactId>
<packaging>jar</packaging>
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
resource目录下新建application.yml修改配置信息如下:
server:
port: 8081
spring:
application:
name: service-consumer-ribbon
eureka:
client:
service-url:
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
RibbonConsumerApplication.java
新建SpringBootApplication RibbonConsumerApplication.java
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//EnableEurekaClient表明为EurekaClient
@EnableEurekaClient
//EnableDiscoveryClient向服务中心注册发现
@EnableDiscoveryClient
public class RibbonConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
//LoadBalanced 注解表明restTemplate使用LoadBalancerClient执行请求
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
RibbonServiceImpl.java
新建具体的消费服务实现,利用RestTemplate完成消费服务
@Service
public class RibbonServiceImpl implements ExampleService {
@Autowired
RestTemplate restTemplate;
@Override
public String hello(String name) {
return restTemplate.getForObject("http://service-producer/hello?name="+name,String.class);
}
}
这里注意http://service-producer/hello
中的service-producer
对应注册中心注册的服务者,hello
指明需要的服务。
新建调用入口ConsumerController.java
@RestController
public class ConsumerController {
@Autowired
RibbonServiceImpl ribbonServiceImpl;
@GetMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return ribbonServiceImpl.hello(name);
}
}
启动工程
再次访问 localhost:8761
测试服务调用
访问http://localhost:8081/hello/jason