zoukankan      html  css  js  c++  java
  • SpringCloud之Feign(五)

    Feign简介

    Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign.

    声明式REST客户端:Feign

    先要启动eureka_register_service工程(注册中心)和biz-service-0工程(服务生产者)

    创建一个maven工程eureka_feign_client

    pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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>Brixton.SR5</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
        </dependencies>
    </dependencyManagement>

    在应用主类中通过@EnableFeignClients注解开启Feign功能

    启动文件FeignApplication.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    public class FeignApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(FeignApplication.class, args);
        }
     
    }

    定义服务接口类UserClient.java

    使用@FeignClient("biz-service-0")注解来绑定该接口对应biz-service-0服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @FeignClient("biz-service-0")
    public interface UserClient {
     
        @RequestMapping(method = RequestMethod.GET, value = "/getuser")
        public User getuserinfo();
         
        @RequestMapping(method = RequestMethod.GET, value = "/getuser")
        public String getuserinfostr();
         
        @RequestMapping(method = RequestMethod.GET, value = "/info")
        public  String  info();
     
    }

    在web层中调用上面定义的UserController,具体如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    @RestController
    public class UserController {
     
        @Autowired
        UserClient userClient;
     
        @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
        public User getuserinfo() {
            return userClient.getuserinfo();
        }
         
        @RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)
        public String getuserinfostr() {
            return userClient.getuserinfostr();
        }
         
        @RequestMapping(value = "/info", method = RequestMethod.GET)
        public String info() {
            return userClient.info();
        }
     
     
    }

    application.properties配置变量

    1
    2
    3
    spring.application.name=feign-consumer
    server.port=8004
    eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  • 相关阅读:
    windows上phpstudy配置memcache
    获取全站详情链接,并输出为txt文本
    Linux 宝塔面板免费版开启 waf 防火墙的方法
    where条件多种情况
    网站加https
    git常用命令
    缓存
    Stream转换成byte[] 、将 byte[] 转成 Stream 、Stream和文件的转换、从文件读取 Stream
    C#发送邮件
    Ref和Out的区别
  • 原文地址:https://www.cnblogs.com/laojiao/p/9506734.html
Copyright © 2011-2022 走看看