创建“服务消费者”

创建一个基础的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 /  :

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

最新文章

  1. J2EE项目修改编码问题
  2. 数据库连接池原理 与实现(动脑学院Jack老师课后自己的练习有感)
  3. an important difference between while and foreach on Perl
  4. 实验一报告 20135238&amp;20135207
  5. Linux下如何查找可执行文件
  6. BNUOJ 1006 Primary Arithmetic
  7. jQuery阻止冒泡和HTML默认操作
  8. dt dd 如何在同一行上
  9. cf602A Two Bases
  10. c++如何生成随机数
  11. Session与Caching
  12. 【译】The Accidental DBA:SQL Server Backup
  13. ansible playbook实践(二)-基础相关命令
  14. 怎样在Ubuntu 14.04中搭建gitolite git服务器
  15. 全新定义!免费开源ERP平台如何玩转工业互联网
  16. Android开发 android沉浸式状态栏的适配(包含刘海屏)转载
  17. Mycat&#160;中间件配置初探与入门操作
  18. finfo_file
  19. ajax,jsonp跨域访问数据
  20. mysql在windows下命令行启动与关闭服务

热门文章

  1. 带你剖析WebGis的世界奥秘----瓦片式加载地图
  2. 【POJ - 3255】Roadblocks(次短路 Dijkstra算法)
  3. 最短路径Dijkstra算法模板题---洛谷P3371 【模板】单源最短路径(弱化版)
  4. 实现API管理系统的几个重要关键词
  5. netty源码解解析(4.0)-18 ChannelHandler: codec--编解码框架
  6. 基于sparksql collect_list的udf定义踩坑
  7. go 学习笔记之go是不是面向对象语言是否支持面对对象编程?
  8. Mybatis多表查询之一对一查询的多种实现-XML配置
  9. input样式重置(outline:none)
  10. Spring-Boot:Profile简单示例