前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springboot2整合openFegin客户端

Springboot2整合openFegin客户端

作者头像
用户5640963
发布2020-11-12 11:28:23
1.1K0
发布2020-11-12 11:28:23
举报
文章被收录于专栏:卯金刀GG卯金刀GG卯金刀GG

1、创建springboot项目,略;

2、添加pom依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.7.2</version>
        </dependency>

3、启动类

@SpringBootApplication
@EnableFeignClients
public class FeginClientDemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(FeginClientDemoApplication.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder   applicationBuilder){
            return applicationBuilder.sources(Application.class);
    }
}

4、okhttp配置

/**
 * @Author: Liu Yue
 * @Descripition:
 * @Date; Create in 2020/11/4 13:36
 **/


@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {

    @Bean
    public okhttp3.OkHttpClient okHttpClient(){
        return new okhttp3.OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(120, TimeUnit.SECONDS)
                .connectionPool(new ConnectionPool())
                .build();
    }
}

5、代理类

/**
 * @Author: Liu Yue
 * @Descripition: 阈值设置的service
 * @Date; Create in 2020/11/4 13:20
 **/
@FeignClient(name = "feignClientProxy",url = "http://localhost:9005/rocketmq-producer/")
public interface ThresholdServiceProxy {

    @GetMapping(value = "/fegin/server/send/{id}/{thresholdval}")
    String invoke(@PathVariable("id") String id, @PathVariable("thresholdval") String thresholdval);

    /**
     * 容错处理类,当调用失败时 返回空字符串
     */
    @Component
    class DefaultFallback implements ThresholdServiceProxy {
        @Override
        public String invoke(@PathVariable("id") String id, @PathVariable("thresholdval")String thresholdval){
            return "ERROR";
        }
    }
}

6、接口类

/**
 * @Author: Liu Yue
 * @Descripition: 阈值设置的controller
 * @Date; Create in 2020/11/4 13:58
 **/
@RestController
@RequestMapping("/threshold")
public class ThresholdController {

    @Resource
    ThresholdServiceProxy thresholdServiceProxy;

    @GetMapping("/send/{id}/{thresholdval}")
    public String sendThresholdVal(@PathVariable("id") String id,
                                                 @PathVariable("thresholdval")String thresholdval){
        String rs = thresholdServiceProxy.invoke(id, thresholdval);

        return "SUCCESS";
    }
}

设置路径: http://localhost:9011/feginDemo/threshold/send/01/FA

每天提高一点点!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档