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服务,同时将原来的端 ...
随机推荐
- Nginx + Keepalived 实例(测试可行)
Nginx_Master: 192.168.1.103 提供负载均衡 Nginx_BackUp: 192.168.1.104 负载均衡备机 Nginx_VIP_TP: 192.168.1.108 网站 ...
- 谷歌添加百度翻译提示Google已将百度翻译标记为恶意程序并阻止安装,怎么办
进入谷歌浏览器的设置, 显示高级设置——隐私设置下七个选项中的第四个选项(启用针对网上诱骗和恶意软件的防护功能)把勾去掉,然后将百度翻译的CRX拖动到chrome的安装插件页面, 修改chrome的限 ...
- jQuery Validate验证框架详解(转)
jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script type=& ...
- html 01前沿-web介绍
1. 认识网页 网页主要由文字.图像和超链接等元素构成.当然,除了这些元素,网页中还可以包含音频.视频以及Flash等. 2. 浏览器(显示代码) 浏览器是网页显示.运行的平台,常用的浏览器有IE.火 ...
- Vue.js 插件开发
Vue.js 的插件应当有一个公开方法 install .这个方法的第一个参数是 Vue 构造器 , 第二个参数是一个可选的选项对象: MyPlugin.install = function (Vue ...
- sharepoint2007就地升级2010系列(三)升级系统
OK,上两篇我们完成sharepoint2007的预览,以及升级前的补丁准备.今天我们来正式进行升级windows server系统以及SQL数据库 升级之前首先确定 search服务停止而且被禁用, ...
- 秒懂JSON.parse()与JSON.stringify()的区别
在网站开发中,Json是最为常见的一种数据交互手段.在使用过程中,常会遇到Json字段串和对象之间进行转换.很多朋友对于JSON.parse() 和JSON.stringify() 的区别,下面为大家 ...
- python 修改xml文件
在百度知道里面有人问了这么一个问题: 有一个xml文件:<root>text <a/> <a/> ...这里省略n个<a/> <root>想 ...
- android sqlite3命令行检查自己的代码操作数据库是否正确
真机调试的话需要root ,否则没有访问目录的权限 在 linux 的终端 或者 windows的cmd 中输入 adb shell 进入shell 环境 cd /data/data/程序包名/dat ...
- rollback
savepoint test; rollback to savepoint test;