springcloud学习入门 (2)

轮询规则
在启动类中restTemplate()方法加入注解@LoadBalanced
RestTemplate 是由 Spring Web 模块提供的工具类,与 SpringCloud 无关,是独立存在的,因 SpringCloud 对 RestTemplate 进行了一定的扩展,所以 RestTemplate 具备了负载均衡的功能

@Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }

在启动类上加注解

@RibbonClient(name = "provider-user") 3. 2. 方式二(配置文件自定义)

在application.yml中加入以下配置

#使用配置文件方式实现负载均衡,优先级,配置文件>注解或java代码配置>cloud默认配置 provider-user: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule 3. 3. 方式三(Java代码自定义)

自定义一个配置类,返回规则

@RibbonClient(name = "provider-user",configuration = RibbonConfiguration.class) public class RibbonConfiguration { @Bean public IRule getRule(){ return new RandomRule(); } } 4. Feign学习

什么是feign,是声明式的webservice客户端,解决远程调用,支持JAX-RS,即RestFulWebService

4. 1. 引入依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> 4. 2. 使用注解@FeignClient编写feign调用的客户端接口 @FeignClient("provider-user") public interface UserFeignClient { @RequestMapping (value = "/user/{id}", method = RequestMethod.GET) public User getUser(@PathVariable Long id); @RequestMapping (value = "/user", method = RequestMethod.POST) public User postUser(@RequestBody User user); } 4. 3. 在启动类加注解@EnableFeignClients 4. 4. Controller层的调用方法 @Autowired private UserFeignClient userFeignClient; @GetMapping (value = "/user/{id}") public User getUser(@PathVariable Long id){ //获取数据 return this.userFeignClient.getUser(id); } @GetMapping (value = "/user") public User postUser(User user){ return this.userFeignClient.postUser(user); } 5. hystrix学习

hystrix是Netflix的一个类库,在微服务中,具有多层服务调用,主要实现断路器模式的类库

5. 1. 引入依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> 5. 2. 在启动类上加注解 @EnableCircuitBreaker

在Controller层类的方法上加注解,并编写退回方法,需同名

@HystrixCommand(fallbackMethod = "findByIdFallBack") public User getOrder(@PathVariable Long id){ //获取数据 User user = new User(); user.setId(id); user.setDate(new Date()); user = restTemplate.getForObject("http://provider-user/user/"+id,User.class); System.out.println(Thread.currentThread().getId()); return user; } public User findByIdFallBack(Long id){ System.out.println(Thread.currentThread().getId()); User user = new User(); user.setId(1L); return user; } 6. springboot的健康监控actuator

actuator主要用于服务健康监控,springboot 1.X和2.x有所不同,本次为2.X

6. 1. 引入依赖包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 6. 2. 配置 #健康监控配置 management: endpoint: health: show-details: always #是否健康监控显示细节 endpoints: web: exposure: include: hystrix.stream #hystrix保护机制,不直接暴露监控状态 base-path: / #暴露的端点链接 6. 3. 访问

1.X版本

localhost:8080/health

2.X版本

localhost:8080/actuator/health 7. feign配合Hystrix使用 7. 1. 配置文件 feign: hystrix: enabled: true # 总开关,可以通过java单独控制client 7. 2. 启动类注解 @EnableFeignClients 7. 3. 控制层 @RestController public class OrderFeignController { @Autowired private UserFeignClient userFeignClient; @Autowired private UserFeignNotHystrixClient userFeignNotHystrixClient; @GetMapping (value = "/order/{id}") public User getUser(@PathVariable Long id){ //获取数据 return userFeignClient.getUser(id); } /** * 测试Feign客户端单独控制 * @param id * @return */ @GetMapping(value = "/user/{id}") public User getUserNotHystrix(@PathVariable Long id){ //获取数据 return userFeignNotHystrixClient.getUserNotHystrix(id); } } 7. 4. 两个FeignClient

一个加了configuration一个没有,加了可以通过注解重写feignBuilder方法单独控制,默认是返回HystrixFeignBuilder

@FeignClient(name = "provider-user", fallback = HystrixClientFallback.class) public interface UserFeignClient { @RequestMapping (value = "/user/{id}", method = RequestMethod.GET) User getUser(@PathVariable Long id); } @Component public class HystrixClientFallback implements UserFeignClient{ @Override public User getUser(Long id) { System.out.println(Thread.currentThread().getId()); User user = new User(); user.setId(1L); return user; } } @FeignClient(name = "provider-user1",configuration = ConfigurationNotHystrix.class,fallback = HystrixClientNotHystrixFallback.class) public interface UserFeignNotHystrixClient { @RequestMapping (value = "/user/{id}", method = RequestMethod.GET) User getUserNotHystrix(@PathVariable Long id); } @Component public class HystrixClientNotHystrixFallback implements UserFeignNotHystrixClient{ @Override public User getUserNotHystrix(Long id) { System.out.println(Thread.currentThread().getId()); User user = new User(); user.setId(1L); return user; } } 7. 5. 配置类 @Configuration public class ConfigurationNotHystrix { @Bean @Scope("prototype") public Feign.Builder feignBuilder(){ return Feign.builder(); } } 7. 6. 获取异常信息代码 @FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class) protected interface HystrixClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello iFailSometimes(); } @Component static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> { @Override public HystrixClient create(Throwable cause) { return new HystrixClient() { @Override public Hello iFailSometimes() { return new Hello("fallback; reason was: " + cause.getMessage()); } }; } } 持续更新中

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwydw.html