Spring Cloud Feign 入门

2019-03-30  本文已影响0人  歌哥居士

示例中是Feign客户端在服务消费者中。实际上Feign客户端应该在服务提供者上,由服务提供者选择暴露接口,然后消费者引入相应接口jar包,接着可以像调用自己方法一样调用远程方法。

添加依赖

<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.baozi</groupId>
    <artifactId>cloud-feign</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>feign-registration</module>
        <module>feign-provider</module>
        <module>feign-consumer</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</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>${spring-cloud.version}</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>

</project>
<?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>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-feign</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>feign-registration</artifactId>

</project>
<?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>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-feign</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>feign-provider</artifactId>

</project>
<?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>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-feign</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>feign-consumer</artifactId>

</project>

注册中心

spring.application.name=feign-registration
server.port=9000

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class FeignRegistrationApplication {

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

}

服务提供者

spring.application.name=feign-provider
server.port=10000

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class FeignProviderApplication {

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

}
import org.springframework.web.bind.annotation.*;


@RestController
public class ProviderController {

    // 传统形式 ------------------------------

    @GetMapping("/m1")
    public String m1(@RequestParam String id) {
        return "m1: " + id;
    }

    @PostMapping("/m2")
    public String m2(@RequestParam String id) {
        return "m2: " + id;
    }

    @PostMapping("/m3")
    public String m3(@RequestBody String id) {
        return "m3: " + id;
    }


    // REST形式 ------------------------------

    @GetMapping("/m4/{id}")
    public String m4(@PathVariable String id) {
        return "m4: " + id;
    }

    @PostMapping("/m5/{id}")
    public String m5(@PathVariable String id) {
        return "m5: " + id;
    }

    @PostMapping("/m6")
    public String m6(@RequestBody String id) {
        return "m6: " + id;
    }


    // 传统形式参数可以使用Map ------------------------------

    @GetMapping("/map")
    String map(@RequestParam String id) {
        return "map: " + id;
    }

}

服务消费者

spring.application.name=feign-consumer
server.port=11000

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // RestTemplate和@LoadBalancer都不需要了
public class FeignConsumerApplication {

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

}
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@FeignClient("feign-provider") // 绑定服务提供方,名称要和服务提供方的spring.application.name一致
public interface IProviderController {

    // 传统形式 ------------------------------

    @GetMapping("/m1")
    String m1(@RequestParam("id") String id);

    @PostMapping("/m2")
    String m2(@RequestParam("id") String id);

    @PostMapping("/m3")
    String m3(@RequestBody String id);


    // REST形式 ------------------------------

    @GetMapping("/m4/{id}")
    String m4(@PathVariable("id") String id);

    @PostMapping("/m5/{id}")
    String m5(@PathVariable("id") String id);

    @PostMapping("/m6")
    String m6(@RequestBody String id);


    // 传统形式参数可以使用Map ------------------------------

    @GetMapping("/map")
    String map(@RequestParam Map<String, Object> map);


}
import org.baozi.iprovider.IProviderController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ConsumerController {

    @Autowired
    private IProviderController iprovider;

    // 传统形式 ------------------------------

    @GetMapping("/m1/{id}")
    public String m1(@PathVariable String id) {
        return iprovider.m1(id);
    }

    @GetMapping("/m2/{id}")
    public String m2(@PathVariable String id) {
        return iprovider.m2(id);
    }

    @GetMapping("/m3/{id}")
    public String m3(@PathVariable String id) {
        return iprovider.m3(id);
    }


    // REST形式 ------------------------------

    @GetMapping("/m4/{id}")
    public String m4(@PathVariable String id) {
        return iprovider.m4(id);
    }

    @GetMapping("/m5/{id}")
    public String m5(@PathVariable String id) {
        return iprovider.m5(id);
    }

    @GetMapping("/m6/{id}")
    public String m6(@PathVariable String id) {
        return iprovider.m6(id);
    }


    // 传统形式参数可以使用Map ------------------------------

    @GetMapping("/map/{id}")
    public String map(@PathVariable String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return iprovider.map(map);
    }

}
上一篇下一篇

猜你喜欢

热点阅读