Giter VIP home page Giter VIP logo

tinyurl-client's Introduction

短连接服务客户端

短连接仓库地址:短连接服务
https://github.com/TheLandscapeBe/tinyurl

#使用方法 1 maven依赖

<dependency>
    <groupId>com.github.fofcn.tinyurl</groupId>
    <artifactId>tinyurl-client</artifactId>
    <version>v2.0.2</version>
</dependency>

2 直接调用客户端

public class TinyUrlClientTest {

    private static TinyUrlClientConfig clientConfig;

    private static HttpPoolConfig httpPoolConfig;

    private static TinyUrlClient tinyUrlClient;

    @BeforeClass
    public static void beforeClass() {
        clientConfig = TinyUrlClientConfig.builder()
                .appId("1594708959736")
                .key("1594708959736")
                .host("http://localhost:80")
                .build();

        httpPoolConfig = HttpPoolConfig.builder()
                .connectTimeout(60000)
                .readTimeout(6000)
                .maxTotal(100)
                .maxPerRoute(100)
                .defaultMaxRoute(100)
                .build();

        tinyUrlClient = new TinyUrlClientImpl(clientConfig, httpPoolConfig);
        tinyUrlClient.initialize();
    }

    @Test
    public void testShorten() {
        TinyUrlParam tinyUrlParam = new TinyUrlParam();
        tinyUrlParam.setDomain("s.xxx.com");
        tinyUrlParam.setUrl("https://mvnrepository.com/artifact/org.slf4j/slf4j-api/1.7.30");
        TinyUrlObject tinyUrlObject = tinyUrlClient.shorten(tinyUrlParam);
        if (ObjectUtil.isNull(tinyUrlObject)) {
            Assert.fail();
        }

        Assert.assertNotNull(tinyUrlObject.getUrl());
        System.out.println(tinyUrlObject.getUrl());
    }
}

3 在spring boot中使用
3.1 配置RestTemplate

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate createAndConfigRestTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(60000);
        requestFactory.setReadTimeout(6000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }
}

3.2 配置TinyUrl客户端

@Data
@Configuration
@ConfigurationProperties(prefix = "tinyurl")
public class TinyUrlClientConfigurer {

    /**
     * 域名,多个域名以逗号分割
     */
    private String host;

    /**
     * appid
     */
    private String appId;

    /**
     * app密钥
     */
    private String key;

    /**
     * 连接超时时间
     */
    private int connectTimeout = 60 * 1000;

    /**
     * 读取超时时间
     */
    private int readTimeout = 60 * 1000;

    /**
     * 最大连接数
     */
    private int maxTotal = 100;

    /**
     * 默认最大路由
     */
    private int defaultMaxRoute = 100;

    /**
     * 每个route最大连接数
     */
    private int maxPerRoute = 100;

    /**
     * 重试次数,默认为0
     */
    private int retryTimes = 0;

    /**
     * 链接资源释放周期,默认为1分钟
     */
    private int evictInterval = 60 * 1000;

    /**
     * 默认域名
     */
    private String domain;

    @Resource
    private RestTemplate restTemplate;


    @Bean
    public TinyUrlClient create() {
        TinyUrlClientConfig clientConfig = TinyUrlClientConfig.builder()
                .appId(appId)
                .key(key)
                .host(host)
                .restTemplate(restTemplate)
                .build();

        HttpPoolConfig httpPoolConfig = HttpPoolConfig.builder()
                .connectTimeout(connectTimeout)
                .readTimeout(readTimeout)
                .maxTotal(maxTotal)
                .maxPerRoute(maxPerRoute)
                .defaultMaxRoute(defaultMaxRoute)
                .evictInterval(evictInterval)
                .retryTimes(0)
                .build();

        TinyUrlClient tinyUrlClient = new TinyUrlClientImpl(clientConfig, httpPoolConfig);
        tinyUrlClient.initialize();
        return tinyUrlClient;
    }
}

3.3 调用TinyUrlClient

TinyUrlParam tinyUrlParam = new TinyUrlParam();
tinyUrlParam.setDomain(domain);
tinyUrlParam.setUrl(realUrl);
TinyUrlObject tinyUrlObject = tinyUrlClient.shorten(tinyUrlParam);
System.out.println(tinyUrlObject.getUrl())

tinyurl-client's People

Contributors

fofcn avatar

Stargazers

arboret avatar

Watchers

James Cloos avatar  avatar arboret avatar  avatar

Forkers

olaytree

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.