项目设计对第三方开源的组件进行集成,使用的是Jetty开发的接口无法向SpringBoot的注册中心注册,又不想在项目中写大量的HTTP请求,感觉有点low。简单了看了下Feign原生的开源组件后由自己本地的SpringBoot服务来维护第三方服务的高可用性。有关其他实现可用参考github的Feign官方介绍。

一、服务发现

package com.ws.feign.lb;

import org.apache.commons.configuration.AbstractConfiguration;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;

import static com.netflix.config.ConfigurationManager.getConfigInstance;

/**
 * @description:
 * @author: 
 * @create: 2020-09-16 10:53
 **/
@Configuration
public class ServiceDiscovery {
    @PostConstruct
    public static void setServiceDiscovery(){
        
        // TODO-WS: 20:09 2020/9/16  在这里来实现监听对应的服务是否正常
        
        AbstractConfiguration configInstance = getConfigInstance();
        configInstance.setProperty("key.ribbon.listOfServers","192.168.101.44:8083");
        configInstance.setProperty("schema.ribbon.listOfServers","192.168.101.43:8081");

    }
}

二 、接口

package com.ws.feign.lb;

import feign.RequestLine;

/**
 * @description:
 * @author: 
 * @create: 2020-09-16 09:29
 **/
public interface LBService {
    @RequestLine("GET /connectors")
    public String getURL();
    @RequestLine("GET /subjects")
    public String getSchema();
}

三、Controller

package com.ws.feign.lb;

import feign.Feign;
import feign.ribbon.LoadBalancingTarget;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author:
 * @create: 2020-09-16 09:31
 **/
@RestController
public class LBController {
    @RequestMapping("/url")
    public void get() {
        LoadBalancingTarget<LBService> target =
                LoadBalancingTarget.create(LBService.class, "http://key");
        LBService api = Feign.builder().target(target);
        String url = api.getURL();
        System.out.println(url);
    }

    @RequestMapping("/schema")
    public void getSchema() {
        LoadBalancingTarget<LBService> target =
                LoadBalancingTarget.create(LBService.class, "http://schema");
        LBService api = Feign.builder().target(target);
        String url = api.getSchema();
        System.out.println(url);
    }
}

四、POM依赖

注意:pom的依赖肯定没有详细的处理,本人项目还要考虑到向注册中心注册,使用了eureka

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>tu_maimes</artifactId>
        <groupId>org.tu_maimes</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>feign</artifactId>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-openfeign</artifactId>
                <version>2.0.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
                <dependency>
                    <groupId>io.github.openfeign</groupId>
                    <artifactId>feign-ribbon</artifactId>
                    <version>9.5.0</version>
                </dependency>
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-gson</artifactId>
            <version>8.18.0</version>
        </dependency>
        <!-- 引入关于 eureka-server的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 引入关于 eureka-ribbon的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> 
        </dependency>
    </dependencies>

</project>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐