第112天学习打卡(SpringCloud Feign:使用接口方式调用服务 Hystrix Dashboard) (5)

把启动的7001 端口 7002端口 springcloud-provider-dept-hystrix-8001端口80端口 中停止springcloud-provider-dept-hystrix-8001端口 然后启动springcloud-provider-dept-8001

测试结果

image-20210517095010332

访问不存在的出现的页面 会报错 而不是向上面启动熔断机制那样显示信息

image-20210517095026976

修改application.yml

server: port: 8001 mybatis: type-aliases-package: com.kuang.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml spring: application: name: springcloud-provider-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 3个服务名称一致, 只是请求不一样了 driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8 username: root password: 123456 # Eureka的配置,服务注册到哪里 eureka: client: service-url: defaultZone: :7001/eureka/,:7002/eureka/,:7003/eureka/ instance: instance-id: springcloud-provider-dept8001 # 修改eureka上的默认描述信息 prefer-ip-address: true # true 可以显示服务的IP地址 # info 配置 info: app.name: kuangshen-springcloud company.name: blog.kuangstudy.com

image-20210517095047800

image-20210517095103334

image-20210517095116261

Hystrix:服务降级 修改springcloud-api

DeptClientService.java

package com.kuang.springcloud.service; import com.kuang.springcloud.pojo.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import java.util.List; @Component @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT", fallbackFactory = DeptClientServiceFallbackFactory.class) public interface DeptClientService { @GetMapping("/dept/get/{id}") public Dept queryById(@PathVariable("id") Long id); @GetMapping("/dept/list") public List<Dept> queryAll(); @PostMapping("/dept/add") public boolean addDept(Dept dept); }

DeptClientServiceFallbackFactory.java

package com.kuang.springcloud.service; import com.kuang.springcloud.pojo.Dept; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; import java.util.List; // 降级 @Component public class DeptClientServiceFallbackFactory implements FallbackFactory { @Override public DeptClientService create(Throwable throwable) { return new DeptClientService() { @Override public Dept queryById(Long id) { return new Dept() .setDeptno(id) .setDname("id=>"+id+"没有对应的信息,客户端提供了降级的信息,这个服务现在已经被关闭") .setDb_source("没有数据~"); } @Override public List<Dept> queryAll() { return null; } @Override public boolean addDept(Dept dept) { return false; } }; } }

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

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