Spring Cloud(二):服务消费者
2024-09-01 08:06:02
创建“服务消费者”
创建一个基础的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 / :
我们的熔断机制作用突显出来了。
最新文章
- J2EE项目修改编码问题
- 数据库连接池原理 与实现(动脑学院Jack老师课后自己的练习有感)
- an important difference between while and foreach on Perl
- 实验一报告 20135238&;20135207
- Linux下如何查找可执行文件
- BNUOJ 1006 Primary Arithmetic
- jQuery阻止冒泡和HTML默认操作
- dt dd 如何在同一行上
- cf602A Two Bases
- c++如何生成随机数
- Session与Caching
- 【译】The Accidental DBA:SQL Server Backup
- ansible playbook实践(二)-基础相关命令
- 怎样在Ubuntu 14.04中搭建gitolite git服务器
- 全新定义!免费开源ERP平台如何玩转工业互联网
- Android开发 android沉浸式状态栏的适配(包含刘海屏)转载
- Mycat&#160;中间件配置初探与入门操作
- finfo_file
- ajax,jsonp跨域访问数据
- mysql在windows下命令行启动与关闭服务
热门文章
- 带你剖析WebGis的世界奥秘----瓦片式加载地图
- 【POJ - 3255】Roadblocks(次短路 Dijkstra算法)
- 最短路径Dijkstra算法模板题---洛谷P3371 【模板】单源最短路径(弱化版)
- 实现API管理系统的几个重要关键词
- netty源码解解析(4.0)-18 ChannelHandler: codec--编解码框架
- 基于sparksql collect_list的udf定义踩坑
- go 学习笔记之go是不是面向对象语言是否支持面对对象编程?
- Mybatis多表查询之一对一查询的多种实现-XML配置
- input样式重置(outline:none)
- Spring-Boot:Profile简单示例