SPRINGCLOUD(EUREKA+RIBBON+FEIGN+HYSTRIX)
2017-08-18 17:58
891 查看
这周人身体不适也没去运动就研究了下SPRINGCLOUD 看了半本周立大神的《Spring Cloud与Docker微服务实战》 给个赞! 跟着写了点helloWorld,对微服务稍微有了点概念,直接进入主题!
项目结构
springcloud eureka做服务发现 ~~ (接触过dubbo的同学应该比较熟悉这个词)
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
<?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"> <parent> <artifactId>beacon</artifactId> <groupId>com.panchen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>beacon-eureka-node1</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
server: port: 8761 eureka: instance: hostname: localhost #Eureka实例的主机名 client: register-with-eureka: false #在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为 fetch-registry: false service-url: defaultZone: http://localhost:${server.port}/eureka
注册中心服务搭完后,是注册服务 springcloud 同样有 provider customer的概念
@EnableDiscoveryClient //注册服务 @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
@RestController public class HelloWorldController { //注入发现客户端 @Autowired private DiscoveryClient discoveryClient; /** * 注:@GetMapping("/{id}")是spring 4.3的新注解等价于: * @RequestMapping(value = "/id", method = RequestMethod.GET) * 类似的注解还有@PostMapping等等 * @param id * @return user信息 */ @GetMapping("/{id}") public String findById(@PathVariable Long id) { return "HelloWorld"+id; } }
server: port: 7070 spring: application: name: beacon-provider-test #命名应用 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka # 指定注册中心的地址 instance: preferIpAddress: true
<?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"> <parent> <artifactId>beacon</artifactId> <groupId>com.panchen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>beacon-provider-test</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> </project>
接下来是消费端
/** * RIBBON-NETFLIX * 提高客户端的软件负载均衡算法 * 1.先选择Eureka Server,它优先选择在同一个Zone且负载较少的Server * 2.根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址(策略:轮询,随机,根据响应时间加权等, * 与大底的负载均衡中间件相同) */ /** * FEIGN * 声明式的webService客户端 */ /** * HYSTRIX * 熔断器--实现快速失败 侦测到某个远程服务器多次调用失败后,不再访问服务器,从而防止应用服务器浪费CPU资源IO资源 * 个人理解:监听异常的代理中间件 */ @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableCircuitBreaker public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } // 实例化RestTemplate,开启均衡负载能力 @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
@RestController public class HelloWorldController { private static final org.slf4j.Logger log= LoggerFactory.getLogger(HelloWorldController.class); @Autowired private HelloWorldClient helloWorldClient; @Autowired private RestTemplate restTemplate; @GetMapping("/{id}") public String helloWorld(@PathVariable Long id){ return helloWorldClient.helloWorld(id); } @GetMapping("/again/{id}") public String helloWorldAgain(@PathVariable Long id){ return helloWorldClient.helloWorld(id); } }
//匹配provider的spring application name //hystrix fallback指定fallback类 @FeignClient(name = "beacon-provider-test",fallback = HelloWorldClient.HelloWorldFallBack.class ) public interface HelloWorldClient { @RequestMapping("/{id}") public String helloWorld(@RequestParam("id") Long id); @Component static class HelloWorldFallBack implements HelloWorldClient{ @Override public String helloWorld(Long id) { return "fallback!"; } } }
server: port: 8000 spring: application: name:beacon-customer-test eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka # 指定注册中心的地址 instance: prefer-ip-address: true ribbon: eureka: enabled:true #开启ribbon从eureka中获取服务列表
<?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"> <parent> <artifactId>beacon</artifactId> <groupId>com.panchen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>beacon-customer-test</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> </dependencies> </project>
hystrix-dashboard --hystrix的可视化监控页面
/** * 测试步骤: * 1. 访问http://localhost:8030/hystrix.stream 可以查看Dashboard * 2. 在上面的输入框填入: http://想监控的服务:端口/hystrix.stream进行测试 * 注意:首先要先调用一下想监控的服务的API,否则将会显示一个空的图表. */ @SpringBootApplication @EnableHystrixDashboard public class HystrixDashBoardApplication { public static void main(String[] args) { new SpringApplicationBuilder(HystrixDashBoardApplication.class).web(true).run(args); } }
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
配完以上模块后启动测试
eureka可视化页面
调用服务
fallback
hystrix监控页面
之前也使用过dubbo 感觉两者配置复杂度55开把 但开发调用的时候springcloud更好理清思路!完!
相关文章推荐
- springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin
- 微服务框架Spring Cloud介绍 Part4: 使用Eureka, Ribbon, Feign实现REST服务客户端
- Spring Cloud Netflix的3大组件应用 Eureka&Ribbon&Hystrix
- 玩转SpringCloud(F版本) 三.断路器(Hystrix)RestTemplate+Ribbon和Feign两种方式
- spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
- Spring Cloud Eureka+Ribbon+Hystrix
- Spring Cloud Netflix 教程(Feign+Ribbon+Hystrix)
- SpringCloud学习:Eureka、Ribbon和Feign
- SpringCloud之Ribbon和Feign断路器支持(Hystrix)|第四章-yellowcong
- 架构师入门:Spring Cloud系列,Hystrix与Eureka的整合
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十九):服务消费(Ribbon、Feign)
- 一起来学SpringCloud之 - 断路器Hystrix(Ribbon)
- Spring Cloud学习:02服务消费者(Ribbon&Feign)
- 微服务开发架构——Spring Cloud常见问题与总结<二>Hystrix/Feign 整合Hystrix后首次请求失败
- 一起来学SpringCloud之 - 断路器Hystrix(Feign)
- SpringCloud学习:Eureka、Feign(含示例代码)
- SpringCloud调研系列3.2:断路器-Feign集成Hystrix
- springcloud记录篇3-springcloud客户端ribbon和feign
- 关于Dalston.SR3版本的spring cloud,用feign客户端调用服务hystrix不能生效的问题
- SpringCloud 学习 | 第五篇: ribbon断路器使用(hystrix)