首页 > 其他分享 >springboot 集成feign 实现远程调用

springboot 集成feign 实现远程调用

时间:2023-03-05 21:13:44浏览次数:46  
标签:feign 调用 springboot public blog new import CommonFeignService

1. springboot项目引入pom相关依赖

        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
            <version>11.8</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-jackson</artifactId>
            <version>11.8</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <version>11.8</version>
        </dependency>
@RestController
@Api(value = "博客页面", tags = "博客页面")
public class BlogController {

    @Autowired
    private CommonFeignService commonFeignService;


    @GetMapping("/feign/blog")
    public Object blog(){
        Object blog = commonFeignService.blog();
        System.out.println(blog);
        return blog;
    }
}

 

 2. feign远程调用地址相关配置

import cn.csbit.dataassets.feign.service.CommonFeignService;
import feign.Contract;
import feign.Feign;
import feign.Request;
import feign.Retryer;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述: feign远程调用
 *
 */
@Configuration
public class OpenFeignConfig {

    @Bean
    public Contract useFeignAnnotations(){
        return new Contract.Default();
    }

    @Bean
    CommonFeignService getFeignService(){
        return Feign.builder()
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .options(new Request.Options(2000,5000))
                .retryer(new Retryer.Default(5000,5000,3))
                .target(CommonFeignService.class,"http://127.0.0.1:8082");
    }

}

3. 编写调用接口

package com.pab.data.datasource.service;

import feign.RequestLine;

public interface CommonFeignService {

    @RequestLine("GET /blog")
    Object blog();
}

4.测试调用接口

 

标签:feign,调用,springboot,public,blog,new,import,CommonFeignService
From: https://www.cnblogs.com/gylhaut/p/17181637.html

相关文章