创建“服务消费者”

创建一个基础的Spring Boot工程,命名为springboot-consumer,并在pom.xml中引入需要的依赖内容:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

创建一个controller

@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass()); @Autowired
RestTemplate restTemplate; @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}

通过@@EnableDiscoveryClient注解启动一个服务注册中心提供给其他应用进行对话:

@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class ApplicationConsumer { /**
* 负载均衡
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ApplicationConsumer.class, args);
} }

application.properties配置文件中增加如下信息:

server.port=9000
spring.application.name=ribbon-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

访问:http://localhost:1111/ ,如图所示:

访问http://localhost:9000/ribbon-consumer /

成功调用了hello-service.

添加熔断机制

pom.xml中引入需要的依赖内容:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

创建一个service:

@Service
public class HelloService { @Autowired
RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "helloFallback")
public String helloService() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
} public String helloFallback() {
return "奥,NO!";
}
}

在controller里调用helloService:

@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass()); @Autowired
HelloService helloService; @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return helloService.helloService();
}
}

启动程序,访问http://localhost:9000/ribbon-consumer /  :

当hello-service服务挂了以后,再访问http://localhost:9000/ribbon-consumer /  :

我们的熔断机制作用突显出来了。

Spring Cloud(二):服务消费者的更多相关文章

  1. spring cloud(服务消费者(利用feign实现服务消费及负载均衡)——初学三)

    Feign是一个声明式的Web Service客户端,我们只需要使用Feign来创建一个接口并用注解来配置它既可完成. 它具备可插拔的注解支持,包括Feign注解和JAX-RS注解.Feign也支持可 ...

  2. spring cloud(二)服务(注册)中心Eureka

    Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...

  3. Spring Cloud Eureka 服务消费者

    参考<spring cloud 微服务实战> 现在已经构建了服务注册中心和服务提供中心,下面就来构建服务消费者: 服务消费者主要完成:发现服务和消费服务.其中服务的发现主要由Eureka的 ...

  4. Spring Cloud (2) 服务消费者-基础

    LoadBalancerClient 使用Spring Cloud提供的负载均衡器客户端来实现服务的消费. 首先创建一个服务消费者工程,命名为com.david.consumer,并在pom.xml中 ...

  5. Spring Cloud (4) 服务消费者-Feign

    Spring Cloud Feign Spring Cloud Feign 是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单,我们只需要创建接口并 ...

  6. Spring Cloud (3) 服务消费者-Ribbon

    在上一篇中使用LoadBalancerClient接口实现了获取某个服务的具体实例,并根据实例信息发起服务接口消费请求.但是这样的做法需要我们手工的区编写服务选取.连接拼接等繁琐的工作,对于开发人员来 ...

  7. spring cloud(服务消费者(利用ribbon实现服务消费及负载均衡)——初学二)

    Ribbon是一个基于HTTP和TCP客户端的负载均衡器,利用ribbon实现服务消费,并实现客户端的负载均衡. 一.准备工作(利用上一节的内容) 启动服务注册中心 启动computer-servic ...

  8. Spring Cloud 网关服务 zuul 二

    有一点上篇文章忘了 讲述,nacos的加载优先级别最高.服务启动优先拉去配置信息.所以上一篇服务搭建我没有讲述在nacos 中心创建的配置文件 可以看到服务端口和注册中心都在配置文件中配置化 属性信息 ...

  9. Spring Cloud 微服务二:API网关spring cloud zuul

    前言:本章将继续上一章Spring Cloud微服务,本章主要内容是API 网关,相关代码将延续上一章,如需了解请参考:Spring Cloud 微服务一:Consul注册中心 Spring clou ...

  10. spring cloud微服务实践二

    在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...

随机推荐

  1. 第二十二章 跳出循环-shift参数左移-函数的使用 随堂笔记

    第二十二章 跳出循环-shift参数左移-函数的使用 本节所讲内容: 22.1 跳出循环 22.2 Shift参数左移指令 22.3 函数的使用 22.4 实战-自动备份mysql数据库和nginx服 ...

  2. 用命令将本地jar包导入到本地maven仓库

    [**前情提要**]在日常开发过程中,我们总是不可避免的需要依赖某些不在中央仓库,同时也不在本地仓库中的jar包,这是我们就需要使用命令行将需要导入本地仓库中的jar包导入本地仓库,使得项目依赖本地仓 ...

  3. Go中的命名规范

    1.命名规范 1.1 Go是一门区分大小写的语言. 命名规则涉及变量.常量.全局函数.结构.接口.方法等的命名. Go语言从语法层面进行了以下限定:任何需要对外暴露的名字必须以大写字母开头,不需要对外 ...

  4. n的阶乘 -牛客

    题目描述 输入一个整数n,输出n的阶乘(每组测试用例可能包含多组数据,请注意处理) 输入描述: 一个整数n(1<=n<=20) 输出描述: n的阶乘 解题思路 采用递归求解,也可以使用循环 ...

  5. HTML/CSS:图片居中(水平居中和垂直居中)

    css图片居中(水平居中和垂直居中) css图片居中分css图片水平居中和垂直居中两种情况,有时候还需要图片同时水平垂直居中, 下面分几种居中情况分别介绍: css图片水平居中 1.利用margin: ...

  6. 超实用,Linux中查看文本的小技巧

    日常开发中,我们经常需要在服务器上进行各种文本,日志的查看操作,本文主要对常用的文本,日志查看技巧进行了一番总结和归纳,方便大家收藏起来后续查看使用: tail命令查看日志信息 实时监控日志: tai ...

  7. Liunx之nginx代理

    一.代理 正向代理 正向代理,也就是传说中的代理,他的工作原理就像一个跳板(VPN),简单的说: 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的 ...

  8. vue组件传值之$attrs、$listeners

    当有父组件A,子组件B,孙子组件C的时候 A-B B-C 的传值想必大家应该都非常熟悉了,通过props和$emit和$on来进行传值 那么A-C之间的传值要怎么做呢? 1.event.bus总线传值 ...

  9. 天天都用消息队列,却不知道为啥要用MQ,这就有点尴尬了

    1.为什么要使用消息队列? 分析:一个用消息队列的人,不知道为啥用,有点尴尬.没有复习这点,很容易被问蒙,然后就开始胡扯了. 回答:这个问题,咱只答三个最主要的应用场景(不可否认还有其他的,但是只答三 ...

  10. Knative 基本功能深入剖析:Knative Serving 之服务路由管理

    导读:本文主要围绕 Knative Service 域名展开,介绍了 Knative Service 的路由管理.文章首先介绍了如何修改默认主域名,紧接着深入一层介绍了如何添加自定义域名以及如何根据 ...