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); 
        }
    
    }


  • 相关阅读:
    C++: string的大小写转换
    c++自由的转换string和number
    C# 中的数据库操作~存储过程篇Mysql SqlServer
    Google Protocol Buffer Basics: C++
    C#中的扩展方法(向已有类添加方法,但无需创建新的派生类型)
    多态以及虚析构函数的使用
    【Java基础】String 相关知识点总结
    【设计模式】抽象工厂模式
    【设计模式】工厂方法模式
    【设计模式】简单工厂
  • 原文地址:https://www.cnblogs.com/vipsoft/p/14822752.html
Copyright © 2011-2022 走看看