Spring Cloud 服务发现和消费
服务的发现和消费
有了服务中心和服务提供者,下面我们来实现一个服务的消费者:
服务消费者主要完成两个任务——服务的发现和服务的消费,服务发现的任务是由Eureka客户端完成,而服务消费的任务是由Ribbon完成。
Ribbon是一个基于HTTP和TCP客户端的负载均衡器。它可以在通过客户端中配置ribbonServerList服务端列表去轮询访问以达到负载均衡目的。
当Ribbon和Eureka同时使用时,Ribbon的服务实例清单RibbonServerList会被DiscoveryEnableNIEWServerList重写。
Ribbon将职责交给Eureka来确定服务端是否已经启动。
例:构建服务发现和消费简单示例:
1. 将上一节的/hello 服务通过jar -jar 使用不同的两个端口来启动,为了试验下Ribbon客户端负载均衡的功能,通过如下命令来启动两个不同的服务:
java -jar helloworld-0.0.1-SNAPSHOT.jar --server.port=8081
java -jar helloworld-0.0.1-SNAPSHOT.jar --server.port=8082
启动后在服务注册中心就可以看到这两个实例服务,如下:
2. 创建新的基础Spring Boot工程来作为服务消费者,取名ribbon-consumer,加入依赖。
<?xml version="1.0" encoding="UTF-8"?>xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.dcz rebbon-consumer 0.0.1-SNAPSHOT jar rebbon-consumer Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.3.8.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-ribbon org.springframework.cloud spring-cloud-dependencies Brixton.SR5 pom import org.springframework.boot spring-boot-maven-plugin
3. 在刚创建的应用主类中RebbonConsumerApplication上,通过@EnableDiscoveryClient注解让该应用注册为Eureka客户端应用,以获得服务发现能力。
同时在该主类中创建RestTemplate的Spring Bean实例,并通过@LoadBalanced注解开启客户端负载均衡。
package com.dcz; 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 RebbonConsumerApplication { @Bean @LoadBalanced // 开启负载均衡 RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RebbonConsumerApplication.class, args); } }
4. 创建ConsumerController控制器并实现/ribbon-consumer接口,并通过注入RestTemplate来实现对服务中心的服务进行调用。
我们必须通过服务名的方式来进行调用,在服务治理框架中这个非常重要。
package com.dcz; import com.netflix.discovery.converters.Auto; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * Created by Administrator on 2017/5/13. */ @RestController public class ConsumerController { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET) public String helloConsumer(){ return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody(); } }
6. 最后在application.properties 文件中配置Eureka服务中心的位置,并设置一个不同的端口。
spring.application.name=ribbon-consumer server.port=9000 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
7. 启动rebbon-consumer应用,之后我们就可以在服务中心面板中看到了RIBBON-CONSUMER服务。
8. 在浏览器中通过http://localhost:9000/rebbon-consumer发起GET请求。
我们可以多次刷新,打开应用的两个不同端口的控制条发现Rebbon会轮询的方式调用两个应用。