feign客户端
yml配置
spring:
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
feign:
hystrix:
enabled: true
启动入口
@EnableDiscoveryClient
@EnableFeignClients(basePackages= {"com.pcitc.mall"})
@SpringBootApplication(scanBasePackages = {"com.pcitc.eshop","com.pcitc.mall"})
public class MallAdminApplication {
public static void main(String[] args) {
SpringApplication.run(MallAdminApplication.class, args);
}
}
#com.pcitc.eshop为本地端的包的路径
#com.pcitc.mall为feign端包的路径
Controller层调用
@RestController
@Api(tags = "ApiProductController", description = "商品API")
@RequestMapping("/api/product")
public class ApiProductController {
@Resource
private ProductClient productClient;
@RequestMapping(value = "/feigngoodsdetail", method = RequestMethod.GET)
private FeignResult<Map> Feigngoodsdetail(Long productid,String tenantid, String siteid){
return productClient.getFeignGoodsdetailById(productid, tenantid, siteid);
}
pom文件
#feign客户端中间件
<dependency>
<groupId>com.pcitc.eshop</groupId>
<artifactId>feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
#注册中心客户端注册
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
#feign组件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Feign中间件
pom文件
<groupId>com.pcitc.eshop</groupId>
<artifactId>feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${feign.version}</version>
</dependency>
接口
@FeignClient(name = "pmss", fallback = ProductClient.PmsClientFallBack.class)
public interface ProductClient {
@GetMapping("/api/product/getProductByIds")
public FeignResult<List<ProductDTO>> findProductByIds(@RequestParam("ids") String ids);
@GetMapping("/api/category/all")
public FeignResult<List<CategoryDTO>> getAllCategory(@RequestParam("tid") String tid);
@GetMapping("/api/product/feigngoodsdetail")
public FeignResult<Map> getFeignGoodsdetailById( @RequestParam("productid") Long productid,@RequestParam("tenantid") String tenantid,@RequestParam("siteid") String siteid);
@Component
static class PmsClientFallBack implements ProductClient {
@Override
public FeignResult findProductByIds(String ids) {
return FeignResult.Failed("-1", "服务繁忙,请稍后再试");
}
@Override
public FeignResult<List<CategoryDTO>> getAllCategory(String tid) {
return FeignResult.Failed("-1", "服务繁忙,请稍后再试");
}
@Override
public FeignResult<Map> getFeignGoodsdetailById(Long productid, String tenantid, String siteid) {
return FeignResult.Failed("-1", "服务繁忙,请稍后再试");
}
}
}
发布到私服
clean deploy