【SpringCloud微服务】第3章 服务治理SpringCloudEureka(三)—— 服务发现与消费

承接上一篇《第3章 服务治理SpringCloudEureka(二)—— 高可用注册中心》

2.6 服务发现与消费

2.6.1 简介

Spring Cloud框架中,服务发现的任务由Eureka的客户端完成,而服务消费任务由Ribbon完成Ribbon是一个基于HTTPTCP的客户端负载均衡器,它可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问,以达到负载均衡的作用。

RibbonEureka联合使用时,Ribbon的服务实例清单RibbonServerList会被DiscoverEnabledNIWSServerPing来取代IPing,它的职责委托给Eureka来确定服务端是否已经启动。

2.6.2 演示

①通过命令行的方式,启动我们之前搭建的高可用注册中心eureka-server(方式同前)

java -jar eureka-service-1.0.0.jar --spring.profiles.active=peer1

java -jar eureka-service-1.0.0.jar --spring.profiles.active=peer2

②通过命令行的方式,启动我们之前创建的hello-server

java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8082

java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8083

③访问http://start.spring.io/,添加如下依赖,构建Mavenribbon-consumer项目。

 

④创建应用主类RibbonConsumerApplication.java

package com.example.ribbonconsumer;

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;

/**
 * 应用主类
 * <p>Title: RibbonConsumerApplication</p>  
 * @author  Liyan  
 * @date    2018年4月4日 下午4:26:47
 * 通过@EnableDiscoveryClient注册改应用
 * 创建RestTemplate的实例,并通过@LoadBalanced注解开启客户端负载均衡
 */
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}

	public static void main(String[] args) {
		SpringApplication.run(RibbonConsumerApplication.class, args);
	}
}

⑤在com.example.ribbonconsumer.controller下创建ConsumerController.java

package com.example.ribbonconsumer.controller;

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;

/**
 * 模拟接口
 * <p>Title: ConsumerController</p>  
 * @author  Liyan  
 * @date    2018年4月4日 下午4:43:10
 * 通过RestTemplate来实现对HELLO-SERVER服务提供的/hello接口进行调用。
 */
@RestController
public class ConsumerController {
	
	@Autowired
	RestTemplate restTemplate;
	
	@RequestMapping(value = "/ribbon-consume", method = RequestMethod.GET)
	public String helloConsumer() {
		//此处可以看出,访问的地址是HELLO-SERVICE,而不是一个具体的地址,在服务治理框架中,这是一个非常重要的特征
		return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
	}
}

⑥在application.properties中配置Eureka服务注册中心的位置,需要与之前HELLO-SERVICE一样,不然是无法发现的⑤在com.example.ribbonconsumer.controller下创建ConsumerController.java

spring.application.name=ribbon-consumer
server.port=9000

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

 ⑦启动ribbon-consumer应用,访问http://localhost:1111,可以看到HELLO-SERVICE之外,还多了我们实现的RIBBON-CONSUMER服务。


⑧多次访问http://localhost:9000/ribbon-consumer,并观察启动的两个HELLO-SERVICE的控制台会交替打印日志,这样就实现了负载均衡的效果。




猜你喜欢

转载自blog.csdn.net/qq296398300/article/details/79820735
今日推荐