SpringCloud入门教学|第三篇:服务消费者(Feign)

  • Feign介绍
  • 创建一个feign服务

上一篇主要介绍了消费方法的一种restTemplate+Ribbon去消费,这篇将介绍Feign进行消费服务。

Feign介绍

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

创建一个feign服务

  • 新建一个moudle工程,这里取名为service-feign。并向上节一样 修改父pom和自身pom
    
    <modules>
        <module>eureka_clientmodule>
        <module>eureka_servermodule>
        <module>service-ribbonmodule>
        <module>service-feignmodule>
    modules>

  • 设置子pom内容为:增加spring-cloud-starter-feign

<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.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>service-feignartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>service-feignname>
    <description>Demo project for Spring Bootdescription>

    
    <parent>
        <groupId>com.examplegroupId>
        <artifactId>springcloudartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <relativePath>../pom.xmlrelativePath>
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Edgware.SR2spring-cloud.version>
    properties>
        
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-feignartifactId>
        dependency>


    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

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


project>
  • 在工程的配置文件中,指定程序名为service-feign,端口号为8795:如下
eureka.client.service-url.defaultZone= http://localhost:8761/eureka/

server.port=8765

spring.application.name= service-feign
  • 在程序入口类ServiceFeignApplication,加上@EnableFeignClients开启Feign功能
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }
}
  • 接下来我们需要定义一个feign的接口。
@FeignClient(value = "service-hi")
interface SchedualServiceHi {
    // 这里的/hello 是你注册的Eureka中的生命接口
    @RequestMapping(value = ["/hello"], method = [(RequestMethod.GET)])
    fun sayHiFromClientOne(@RequestParam(value = "name") name:String): String
}

说明:

  • @FeignClient指定了调用那个服务,这里调用 service-hi的服务,同时我们需要调用此服务下的/hello接口,因此我们声明的映射也是get请求,映射到/hello

  • 声明web层的controller,对外暴露一个”/hi”的API接口。因此用来消费服务


@RestController
class HiController {
    @Autowired
    private lateinit var schedualServiceHi: SchedualServiceHi

    @RequestMapping(value = ["/hi"],method = [(RequestMethod.GET)])
    fun sayHi(@RequestParam name: String): String {
        return schedualServiceHi.sayHiFromClientOne(name)
    }



}

此时启动应用多次访问http://localhost:8765/hi?name=houshuai ,浏览器会交替显示

hi houshuai ,I am from port:8762

hi houshuai ,I am from port:8763

源码地址:
https://github.com/houshuai0816/SpringCloudDemo

你可能感兴趣的:(后端--开发,SpringCloud入门教程)