史上最簡單的SpringCloud教程 | 第三篇: 服務消費者(Feign)

轉載請標明出處:
http://blog.csdn.net/forezp/a...
本文出自方誌朋的博客html

上一篇文章,講述了經過restTemplate+ribbon去消費服務,這篇文章主要講述經過feign去消費服務。java

1、Feign簡介

Feign是一個聲明式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只須要建立一個接口並註解。它具備可插拔的註解特性,包括Feign 註解和JAX-RS註解。Feign同時支持可插拔的編碼器和解碼器。Spring cloud對Spring mvc添加了支持,同時在spring web中次用相同的HttpMessageConverter。當咱們使用feign的時候,spring cloud 整和了Ribbon和eureka去提供負載均衡。git

簡而言之:github

  • feign採用的是接口加註解web

  • feign 整合了ribbonspring

2、準備工做

繼續用上一節的工程: 啓動eureka-server,端口爲8761; 啓動service-hi 兩次,端口分別爲8762 、8773.apache

3、建立一個feign的服務

建立一個spring-boot工程,取名爲:serice-feign,它的pom文件爲:瀏覽器

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.forezp</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-feign</name>
    <description>Demo project for Spring Boot</description>

    <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-feign</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>
    </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>

向服務註冊中心註冊它本身,這時service-ribbon既是服務提供者,也是服務消費者,配置文件application.ymlmvc

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign

在程序的入口類,須要經過註解@EnableFeignClients來開啓feign:app

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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

定義一個feign接口類,經過@ FeignClient(「服務名」),來指定調用哪一個服務:

/**
 * Created by fangzhipeng on 2017/4/6.
 */
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

在web層的controllrt:

@RestController
public class HiController {

    @Autowired
    SchedualServiceHi schedualServiceHi;
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam String name){
        return schedualServiceHi.sayHiFromClientOne(name);
    }
}

訪問http://localhost:8765/hi?name...瀏覽器交替顯示:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

4、更改feign的配置

在聲明feignclient的時候,不只要指定服務名,同時須要制定服務配置類:

@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
    //..
}

重寫配置,須要加@Configuration註解,並重寫下面的兩個bean,栗子:

@Configuration
public class FooConfiguration {
    @Bean
    public Contract feignContractg() {
        return new feign.Contract.Default();
    }

    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("user", "password");
    }
}

本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter3

5、參考資料

spring-cloud-feign

優秀文章推薦:

相關文章
相關標籤/搜索