场景测试代码测试项目结构问题分析与解决
场景做一个项目的时候 服务老是触发熔断。项目的架构是nacos做注册发现中心,当然也做配置中心。使用openfeign实现远程调用测试代码
项目有些复杂,所以单独抽出openfeign做了测试 测试项目结构
- 子模块provider
- 实现一个普通的restful接口
@RestController
public class TestController {
@GetMapping(value= "getStr")
public String getString(@RequestParam("status") Integer status){
System.out.println("调用了provider资源");
return status == 1 ? "provider 正常的" : "provider 有问题的";
}
}
server:
port: 8180
spring:
application:
name: test-feign-provider
cloud:
nacos:
discovery:
server-addr: http://xxxx:8848/
cluster-name: ${discoveryspring.application.name}
- 子模块consumer
- 声明openfeign接口,熔断配置实现一个普通接口, 该接口通过openfeign调用provider接口
@Component
public class TestFeignFallBack implements TestFeign{
@Override
public String getString(Integer status) {
System.out.println("熔断:" + status);
return null;
}
}
@FeignClient(name="test-feign-provider", fallback=TestFeignFallBack.class)
public interface TestFeign {
@GetMapping("/getStr")
String getString(@RequestParam("status") Integer status);
}
@RestController
public class TestFeignController {
@Resource
private TestFeignService testFeignService;
@GetMapping("/getStr")
public String getStr(@RequestParam(name = "status", defaultValue = "1") Integer status){
System.out.println("传入的数据 staus:" + status);
return testFeignService.getTestStr(status);
}
}
public interface TestFeignService {
String getTestStr(Integer status);
}
@Service
public class TestFeignServiceImpl implements TestFeignService {
@Autowired
private TestFeign testFeign;
@Override
public String getTestStr(Integer status) {
return testFeign.getString(status);
}
}
server:
port: 8003
spring:
application:
name: server-consumer
cloud:
nacos:
discovery:
server-addr: http://xxxx:8848/
cluster-name: ${discoveryspring.application.name}
feign:
hystrix:
enabled: true
- 父模块pom文件
4.0.0 org.springframework.boot spring-boot-starter-parent2.2.2.RELEASE com.carsonlius spring-cloud-feignpom 1.0-SNAPSHOT provider consumer 8 8 org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-openfeigncom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
- 子模块pom配置
- 不需要配置
- http://localhost:8003/getStr请求 会触发熔断
1. 直接请求provider接口 http://localhost:8180/getStr?status=1是正常的 2. 观察nacos 两个服务有正常注册,对比服务名字也米有问题 3. @FeignClient(name="test-feign-provider", url = "http://localhost:8180/", fallback=TestFeignFallBack.class) 直接设置url 请求正常 4. 观察控制台 发现openfeign实际调用的本机IP是172.173.173.25, 尝试直接访问http://172.173.173.25:8180/getStr?status=1无法访问 5. 确认172.173.173.25有没有问题: ifconfig中eth0的确是172.173.173.25,所以这个ip没有问题。 6. 此时问题变成了为什么172.173.173.25不能访问的问题? 关掉代理,重启项目ok



