feign

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

 

 

### Feign技术介绍 Feign 以其声明式的使用方式和灵活的扩展能力,成为微服务架构中实现 HTTP 通信的利器。随着微服务架构的不断发展,Feign 在远程调用中的地位将更加重要[^1]。 Feign 的基本原理是通过接口和注解的方式来定义服务之间的调用,它会根据定义的接口和注解生成代理对象,这个代理对象会处理 HTTP 请求的发送和响应的接收。 ### Feign使用指南 #### 基本使用 定义一个 Feign 客户端接口,使用 `@FeignClient` 注解指定服务名。示例代码如下: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name="systemClient") public interface SystemClient { @RequestMapping(path="/llsydn/importExcel", consumes={"multipart/form-data"}, method = RequestMethod.POST) JsonResult importExcel(@RequestPart(name="file") MultipartFile file); } ``` #### 超时时间设置 可以针对指定的 Feign client 进行超时时间设置,不过文档中未详细给出超时设置代码,通常可以在配置文件中设置,示例如下(不同版本可能有差异): ```yaml feign: client: config: systemClient: connectTimeout: 5000 readTimeout: 5000 ``` #### 日志级别设置 基于配置文件修改 Feign 的日志级别可以针对单个服务,示例如下: ```yaml feign: client: config: userservice: # 针对某个微服务的配置,此处是服务名称 loggerLevel: FULL # 日志级别 ``` ### Feign与SSO结合使用 在实际应用中,若要将 Feign 与 SSO 结合使用,通常需要在 Feign 的请求拦截器中添加 SSO 的认证信息。例如,在请求头中添加 SSO 的 token。示例代码如下: ```java import feign.RequestInterceptor; import feign.RequestTemplate; import org.springframework.stereotype.Component; @Component public class SSOFeignInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { // 从某个地方获取 SSO 的 token,这里只是示例 String ssoToken = getSSOToken(); template.header("Authorization", "Bearer " + ssoToken); } private String getSSOToken() { // 实现获取 SSO token 的逻辑 return "your_sso_token"; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值