Spring Cloud 入门Eureka -Consumer服务消费(一)
这里介绍:LoadBalancerClient接口,它是一个负载均衡客户端的抽象定义,下面我们就看看如何使用Spring Cloud提供的负载均衡器客户端接口来实现服务的消费。
引用之前的文章中构建的eureka-server作为服务注册中心、eureka-client作为服务提供者作为基础。
1、pom.xml 和eureka-client一样的配置
<?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">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>eurekaConsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eurekaConsumer</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</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>
2、application, server使用了8000,client使用了8001端口,这里我们使用8002
spring.application.name=eurekaConsumer
server.port=8002
#指定eureka-servcer注册中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3、Spring Cloud 使用的是rest api, 这里我们初始化RestTemplate,用来真正发起REST请求
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumerApplication { @Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}
4、创建一个消费接口
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class ClientConsumerController { @Autowired
LoadBalancerClient loadBalancerClient; @Autowired
RestTemplate restTemplate; @GetMapping("/consumer")
public String all() {
// 发起REST请求
return restTemplate.getForObject(getUrl("eurekaClient", "/all"), String.class);
} /**
* 获取指定url
* @param clientApplicationName 指定的服务提供名
* @param interfaceName 需要消费的接口名
* @return
*/
private String getUrl(String clientApplicationName, String interfaceName) {
// 使用loadBalancerClient的choose函数来负载均衡的选出一个eurekaClient的服务实例
ServiceInstance serviceInstance = loadBalancerClient.choose(clientApplicationName);
// 获取之前eurekaClient /all接口地址
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + interfaceName;
System.out.println(url);
return url;
} }
最后依次启动注册服务server,服务提供者client ,服务消费consumer,可以看到,访问http://localhost:8000/服务中心结果如下:

再然后访问http://localhost:8002/consumer,去消费 eurekaclient 的 all接口

由此可以看出,消费服务 ,需要指定服务提供方,和需要消费的接口,而引入LoadBalancerClient ,需要host,port,并且配置也很不友好,下面一篇会介绍Ribbon
Spring Cloud 入门Eureka -Consumer服务消费(一)的更多相关文章
- Spring Cloud 入门Eureka -Consumer服务消费(声明式Feign)(三)
Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单.我们只需要通过创建接口并用注解来配置它既可完成对Web服务接 ...
- Spring Cloud 入门Eureka -Consumer服务消费(Ribbon)(二)
前面一篇介绍了LoadBalancerClient来实现负载均衡, 这里介绍Spring cloud ribbon 1.ribbon Spring Cloud Ribbon 是一个基于Http和TCP ...
- Spring Cloud 入门教程(一): 服务注册
1. 什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁 ...
- Spring Cloud 入门教程(二): 服务消费者(rest+ribbon)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- spring cloud深入学习(三)-----服务消费
在上一篇博文中简单实现了eureka-server以及eureka-provider,后面会实现eureka-cosumer,现在针对eureka做进一步的详解. 微服务整体架构 文字再美也没有图片直 ...
- spring cloud 使用Eureka作为服务注册中心
什么是Eureka? Eureka是在AWS上定位服务的REST服务. Eureka简单示例,仅作为学习参考 在pom文件引入相关的starter(起步依赖) /*定义使用的spring cloud ...
- Spring Cloud Netflix Eureka【服务治理】
一.简介 二.使用 一.源码分析
- Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务
前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...
随机推荐
- Apache Beam的特点
不多说,直接上干货! Apache Beam 有两大特点: 1.统一了数据批处理(batch)和流处理(stream)编程范式: 2.能在任何执行引擎上运行. 它不仅为模型设计.更为执行一系列数据导向 ...
- 《nginx 四》双机主从热备
lvs+keepalived+nginx实现高性能负载均衡集群 LVS作用 LVS是一个开源的软件,可以实现传输层四层负载均衡.LVS是Linux Virtual Server的缩写,意思是Linux ...
- 让GIt忽略SSL证书错误的方法
当你通过HTTPS访问Git远程仓库,如果服务器的SSL证书未经过第三方机构签署,那么Git就会报错.这是十分合理的设计,毕竟未知的没有签署过的证书意味着很大安全风险.但是,如果你正好在架设Git服务 ...
- C#多样式EXECl导出
sing NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using System; using System.Collecti ...
- 第一次尝试用Open Live Writer写日志
注册博客园很久了,一直没怎么用,今天登陆了一下,看到这个写日志工具,看着不错,试一下
- Nodejs计时器定时执行函数
一.最low的定时器: 每次执行完间隔5s,然后继续执行 (function schedule() { setTimeout(do_it, 5000, schedule); }()); functio ...
- 合唱队(华为OJ)
描述 计算最少出列多少位同学,使得剩下的同学排成合唱队形 说明: N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左 ...
- form中action属性后面?传递参数 获取不到
$p_id = $_REQUEST['p_id']; echo "<h1>您将更新商品编号为<span>$p_id</span>的商品信息 <a h ...
- 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification
这个例子介绍了如何在Service中使用Notification,相关的类为NotifyingController和NotifyingService. 在Service中使用Notification的 ...
- Azure进阶攻略 | 你的程序也能察言观色?这个真的可以有!
前段时间有个网站曾经火爆微博和朋友圈:颜龄机器人.只要随便上传一张包含人面孔的照片,这个网站就可以分析图片,并判断照片中人物的年龄.化妆.美颜 P 图.帽子墨镜之类的配饰,几乎都没法影响这个网站的检测 ...