四、SpringCloud使用Feign实现负载均衡

一、Feign简介
Feign是收到了 Retrofit, JAXRS-2.0, and WebSocket启发而产生的http客户端。极大的简化了restful风格的http API参数绑定。Feign默认集成了Ribbon,使用Feign可以指定编码解码,重请求等功能,需要进行配置
二、Feign的使用
1、创建项目consulclient4,引入依赖

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--用于监控与管理-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!--config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul</artifactId>
        </dependency>

        <!--服务发现依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!--用于consul配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>

        <!--Feign的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>

2、创建Feign接口

@FeignClient("consulservice3")//依然用我们的使用过的service consulservice3
public interface SayService {

    @RequestMapping(value = "say")
    String say();
}

3、使用Feign

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//必不可少,使用此注解才能扫描到Feign接口,负责不会注入SayService
@RestController
public class ConsulClient4App {

    @Autowired
    private SayService sayService;

    @RequestMapping("say")
    public String say(){
        return sayService.say();
    }

    public static void main(String[] args){
        SpringApplication.run(ConsulClient4App.class, args);
    }
}

4、以端口9999启动ConsulClient4App,调用接口localhost:9999/say
这里写图片描述
这里写图片描述

源码:
https://github.com/NapWells/spring_cloud_learn/tree/master/discover_server_with_consul/springcloudlearn/consulclient4

猜你喜欢

转载自blog.csdn.net/qq_36027670/article/details/79760843