feign

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值