SpringCloud-http客户端(Feign)

1.什么是Feign

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果,feign和rest都可以远程调用接口服务

2.如何使用feign调用member-server服务提供的接口

步骤一:pom文件导入所需要的依赖

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.itmayiedu</groupId>
  <artifactId>feign-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
  
  
</project>

步骤二:创建接口调用member-server(Feign 采用的是基于接口的注解)

package com.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "service-member")//该注解调用的是eureka注册服务中名字是 service-member 的服务
public interface MemberFeign {

    @RequestMapping("/getFeiginMemberMsg")//service-member 提供的接口名字 ,底层也是使用httpclient方法调用
    public String getToMemberAll();

}

步骤三:创建控制层,调用service层

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.service.MemberFeign;

@RestController
public class FeginController {
    @Autowired
    private MemberFeign memberFeign;

    @RequestMapping("/getToMemberAll")
    public String getToMemberAll() {
        return memberFeign.getToMemberAll();
    }

}

步骤四:编写启动类加入@EnableFeignClients注解

package com.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignApp {

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

 

步骤五:创建Feign-server的yml文件:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8765
  tomcat:
    max-threads: 50
spring:
  application:
    name: service-order-feign

 

步骤六:在member-server 编写提供外部访问的服务接口:

package com.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReqController {
    @Value("${server.port}")
    public int port;

    @RequestMapping("/getFeiginMemberMsg")
    public String getFeiginMemberMsg() {
        String str = "使用feign调用";
        return str;
    }
}

 

步骤七:启动eureka-server 和member-server,Feign-server

 

 

 

 步骤八:浏览器输入请求地址:http://127.0.0.1:8765/getToMemberAll

 

 

使用Figin远程调用成功

 

3.Feign自带有Ribbon负载均衡的功能  @EnableDiscoveryClient

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient//注册服务到eureka
@EnableFeignClients//表明是Feign客户端
@EnableDiscoveryClient//该注解可以 用于开启feign的负载均衡功能 如果没有该注解可能就集群情况下feign会报错
public class FeginApp {

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

}

 

 

  对member-server进行集群:

 

posted on 2019-05-17 14:59  年少不知愁  阅读(655)  评论(0编辑  收藏  举报