您的位置:首页 > 编程语言 > Java开发

SpringCloud学习笔记(三),Hystix熔断、Feign以及ZUUL网关学习总结

2019-08-21 08:48 1786 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/Ruyi_squad/article/details/99934164

Hystix熔断、Feign以及ZUUL网关

  1. Hystix
  1. Hystix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败。
  2. 工作机制

简单来讲就是针对!!!!!!

 

  1. Hystix实现 添加依赖

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

 

  1. 消费者启动类添加注解

使用@SpringBootApplication

@EnableDiscoveryClient

@EnableCircuitBreaker

或者使用@SpringCloudApplication代替前三个

因为这是一个混合注解包括前三个

该注解源码

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Inherited

@SpringBootApplication

@EnableDiscoveryClient

@EnableCircuitBreaker

public @interface SpringCloudApplication {

}

 

  1. 消费者模块控制层方法需要添加该注解

@GetMapping("/{userId}")

    @HystrixCommand(

            //声明一个失败回滚处理函数

            fallbackMethod = "fallback01",

            commandProperties={

            //更改超时时间,默认1000 /1秒,针对当前方法使用

            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1000")

    }

)

 public UserInfo findById(@PathVariable("userId") int userId) {

        System.out.println("这是消费端接收的ID是 " + userId);

        String url = "http://user-service/" + userId ;

        System.out.println("Url"+url);

       UserInfo userInfo = restTemplate.getForObject(url, UserInfo.class);

        System.out.println("消费者接收的数据"+userInfo.getUsername());

       return userInfo ;

    }

//下面就是fallbackMethod 指定的方法

//下面@PathVariable("userId")在该方法有没有不影响   

public UserInfo fallback01( @PathVariable("userId") int userId) {

        System.out.println("接收的参数ID是:"+userId);

        UserInfo userInfo = new UserInfo();

        userInfo.setUserId(110);

        userInfo.setUsername("服务器炸了!");

        return userInfo;

    }

 

  1. 优化配置

配置全局超时时间

没成功

 

 

  1. Feign

2.1 添加依赖

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>

2.2 编写接口

@FeignClient("user-service")

public interface UserFeignClient {

    @GetMapping("/{userId}")

    UserInfo findById(@PathVariable int userId);

}

2.3 启动类配置

添加注解

@SpringCloudApplication

3ff7 @EnableFeignClients

public class UserConsumerApplication {

    //Feign中已经自动集成了Ribbon负载均衡,因此我们不需要自己定义RestTemplate了。    /*@Bean

    @LoadBalanced

    public RestTemplate restTemplate(){

        return new RestTemplate();

    }*/

    public static void main(String[] args) {

        SpringApplication.run(UserConsumerApplication.class, args);

    }

}

2.4 客户端调用

@RestController

@DefaultProperties(defaultFallback = "fallback01")

public class UserController {

    @Autowired

    private UserFeignClient userFeignClient ;

    @GetMapping("/{userId}")

    public UserInfo findById(@PathVariable("userId") int userId) {

        return userFeignClient.findById(userId);

    }

 

    /*@Autowired

    private RestTemplate restTemplate;*/

    /*@GetMapping("/{userId}")

    @HystrixCommand(

            //指定请求超时后去响应其他方法

            fallbackMethod = "fallback01",

            commandProperties={

            //更改超时时间,默认1000 /1秒

            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1000")

    }

    )

    public UserInfo findById(@PathVariable("userId") int userId) {

        System.out.println("这是消费端接收的ID是 " + userId);

        String url = "http://user-service/" + userId ;

        System.out.println("Url"+url);

       UserInfo userInfo = restTemplate.getForObject(url, UserInfo.class);

        System.out.println("消费者接收的数据"+userInfo.getUsername());

       return userInfo ;

    }*/

    public UserInfo fallback01( int userId) {

        System.out.println("接收的参数ID是:"+userId);

        UserInfo userInfo = new UserInfo();

        userInfo.setUserId(110);

        userInfo.setUsername("服务器炸了!");

        return userInfo;

    }

}

2.5 负载均衡

Feign中本身已经集成了Ribbon依赖和自动配置

2.6 FeignHystix支持

Feign默认也有对Hystix的集成,只不过,默认情况下是关闭的

开启Hystix配置:feign.hystrix.enabled=true

2.6.1 编写熔断器实现接口

//这是个组件

@Component

public class UserFallBack implements UserFeignClient {

    @Override

    public UserInfo findById(int userId) {

        UserInfo userInfo = new UserInfo();

        userInfo.setUserId(110);

        userInfo.setUsername("这是采用Feign对Hystix的支持实现的熔断器");

        return userInfo;

    }

}

2.6.2 修改实现类

@FeignClient(value = "user-service",fallback = UserFallBack.class)

public interface UserFeignClient {

    @GetMapping("/{userId}")

    UserInfo findById(@PathVariable int userId);

}

 

 

2.6.3 当使用Feign的时候,不能够再加入来自于Spring自带的包下面的@HystrixCommand注解,会产生冲突。

 

2.6.4 测试运行

 

 

 

  1. ZUUL网关

3.1 添加依赖

<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>

        </dependency>

3.2 设置启动类

@SpringBootApplication

@EnableZuulProxy

public class GatewayApplication {

    public static void main(String[] args) {

        SpringApplication.run(GatewayApplication.class, args);

    }

}

 

3.3 配置application.properties

server.port=10086

spring.application.name=gateway

3.4 编写路由规则

server.port=10086

spring.application.name=gateway

#eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka

#路由配置

#如果不写映射规则,系统会提供一套默认的配置规则

#映射路径

zuul.routes.user-service.path=/user-service/**

#映射路径实际对应的url

zuul.routes.user-service.url=http://127.0.0.1:8081

zuul.routes.user-service.serviceId=user-service

4 面向服务的路由

4.1 添加依赖

<dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>

4.2 设置启动类

@SpringBootApplication

@EnableZuulProxy

@EnableDiscoveryClient

public class GatewayApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(GatewayApplication.class, args);

    }

}

4.3 application.properties配置

server.port=10086

spring.application.name=gateway

eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka

#路由配置

#如果不写映射规则,系统会提供一套默认的配置规则

#映射路径

zuul.routes.user-service.path=/user-service/**

#映射路径实际对应的url

#zuul.routes.user-service.url=http://127.0.0.1:8081

zuul.routes.user-service.serviceId=user-service

 

#将以上的url,更改为serviceId,不再面向于IP地址,更改为面向于服务,通过Eureka自动查找对应的服务名称,底层会应用负载均衡,实现服务的调用。

4.4 简化路由配置

server.port=10086

spring.application.name=gateway

eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka

#映射路径

zuul.routes.user-service.path=/user-service/**

#设置忽略服务的集合a,b,c

zuul.ignored-services=user-consumer

 

 

 

 

 

 

 

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐