四、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.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.0.0.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Finchley.M8version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-actuatorartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-config-serverartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-consulartifactId>
        dependency>

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

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

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        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
四、SpringCloud使用Feign实现负载均衡_第1张图片
四、SpringCloud使用Feign实现负载均衡_第2张图片

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

你可能感兴趣的:(java,loadBalance,spring)