创建“服务消费者”

创建一个基础的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. UVA11388 GCD LCM

    (链接点这儿) 题目: The GCD of two positive integers is the largest integer that divides both the integers w ...

  2. 如何用Hexo+Github创建自己的技术博客

    注册一个github GitHub官网.按照一般的网站注册登录执行就好了,不详细说. 安装git 安装很简单,一直下一步 git安装教程 很多教程里都说要配置环境变量,我本人安装过5次左右的git,一 ...

  3. echarts3.x遇到的坑

    此文章用来记录echarts3.x遇到的坑,方便以后自己不再犯. 1.柱形图设置了yAxis.splitArea.show=true,后面设置的splitLine就会变不可见了.也没在官方文档中找到说 ...

  4. 使用webpack---安装webpack和webpack-dev-server

    1.先确保安装了最新版的Node.js和NPM,并已经了解NPM的基本用法 (以下使用cmd命令行进行) 2.安装webpack (1)全局安装 $ npm install webpack -g   ...

  5. python小白手册之远程链接转换

    访问顺序补充

  6. spring-boot-plus项目目录结构(六)

    spring-boot-plus项目目录结构 目录结构 bin:启动/重启命令脚本目录 logs:部署后记录日志目录 assembly:maven打包配置文件目录 java:源代码目录 resourc ...

  7. 10G的变态SQL文件,如何快速打开编辑?

    工作中,偶尔需要编辑一些大文件,比如 log 文件,后者一些变态的 SQL,此时用平常的编辑器就会显得力不从心,要么直接打不开,要么打开后卡得要死. 本文就给大家推荐几款可以操作大文件的编辑器,准备好 ...

  8. Linux命令行中好用的快捷键

    Ctrl + L:清屏,与clear同效: Ctrl + D:终结当前程序,向终端传递EOF(End Of File): Ctrl + A:光标移至一行命令开头: Ctrl + E:光标移至一行命令结 ...

  9. ASP.NET Core 2.2 : 二十六. 应用JWT进行用户认证

    本文将通过实际的例子来演示如何在ASP.NET Core中应用JWT进行用户认证以及Token的刷新方案(ASP.NET Core 系列目录) 一.什么是JWT? JWT(json web token ...

  10. MACOS安装使用kafka

    安装(会自动依赖安装zookeeper) brew install kafka 启动zookeeper cd /usr/local/Cellar/kafka/2.1.1/bin/ ./zookeepe ...