zoukankan      html  css  js  c++  java
  • OpenFeign 使用

    <spring-cloud-openfeign.version>2.2.6.RELEASE</spring-cloud-openfeign.version>
    对应的SpringBoot
    <version>2.3.0.RELEASE</version>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>${spring-cloud-openfeign.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>archaius-core</artifactId>
                <groupId>com.netflix.archaius</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    简单调用

    //定义一个拦截器
    public class FeignInterceptor implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate requestTemplate) {
            requestTemplate.header("AppId", "AppId");
            requestTemplate.header("AppKey", "myuser1");
            requestTemplate.header("AppSecret", "mypassword");
        }
    }
    
    
    //接口类
    @Component
    @FeignClient(name = "demo-feign",
            url = "https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp/",
            configuration = FeignInterceptor.class)
    public interface IFeignTestService {
    
        @GetMapping(value = "/GetUser")
        String getUser(@RequestBody String words);
    
    }
    
    
    //调用
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class FeignTests {
        Logger logger = LoggerFactory.getLogger(this.getClass());
    
        @Autowired
        private IFeignTestService feignTestService;
    
        @Test
        void searchTest() {
            String result = feignTestService.getUser("VipSoft");
            logger.info(result);
    
        }
    
    }

    OpenFeign 动态URL

    //定义一个拦截器
    public class FeignInterceptor implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate requestTemplate) {
            requestTemplate.header("AppId", "AppId");
            requestTemplate.header("AppKey", "myuser1");
            requestTemplate.header("AppSecret", "mypassword");
        }
    }
    
    
    //接口类
    @Component
    @FeignClient(name = "demo-feign",
            url = "url必须有值,这里随便写,但不能为空",
            configuration = FeignInterceptor.class)
    public interface IFeignTestService {
    
        @PostMapping("/")
        String getWebsite(URI uri, @RequestBody String words); 
    }
    
    
    //调用
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class FeignTests {
        Logger logger = LoggerFactory.getLogger(this.getClass());
    
        @Autowired
        private IFeignTestService feignTestService;
    
        @Test
        void searchTest() {
           String ss1 = feignBuilderService.getWebsite(new URI("https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp/GetUser"), "");
            logger.error(ss1);
            String ss2 = feignBuilderService.getWebsite(new URI("https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp/hospital"), "");
            logger.error(ss2);
            String ss3 = feignBuilderService.getWebsite(new URI("https://www.fastmock.site/mock/e5738f58a04967320a772f1d69aa4a41/mp/device"), "");
            logger.error(ss3); 
        }
    
    }


  • 相关阅读:
    Android JSON解析
    相对靠谱的相册、拍照返回选择程序
    android strings.xml转义字符,注意细节解决
    设置progressBar 背景以及进度条
    Android Launcher简易Launcher开发
    android.os.NetworkOnMainThreadException 异常处理
    AspNetPager 多条件分页查询
    无法解析指定对象的 TargetProperty (UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)“的异常解决
    DefaultButton Deal with users hitting ENTER on your forms
    Setup SSRS in SharePoint Integrated Mode
  • 原文地址:https://www.cnblogs.com/vipsoft/p/14822752.html
Copyright © 2011-2022 走看看