zoukankan      html  css  js  c++  java
  • springCould:使用Feign 实现声明式服务调用

    一、Spring Cloud Feign概念引入
    通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,
    两者作为基础工具类框架应用在各种基础设施类微服务和业务类微服务中,并且成对存在,那么有没有更高层的封装,将两者的使用
    进一步简化呢? 有! 他就是Spring Cloud Feign。它基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,
    除了提供两者强大的功能外,还提供了一种声明式的Web服务客户端定义方式

    <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.sam</groupId>
        <artifactId>feign-consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
    
        <properties>
            <javaVersion>1.8</javaVersion>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR6</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    
        </dependencyManagement>
    
        <dependencies>
            <!-- 引入eureka 客户端依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!-- 引入feign 依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
    
        </dependencies>
    
    </project>

    properties文件:

    server.port=9001
    
    spring.application.name=feign-consumer
    
    eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

    启动类:

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

    service:

    /**
     * 通过@FeignClient注解指定服务名来绑定服务,这里的服务名字不区分大小写
     * 然后再通过@RequestMapping来绑定服务下的rest接口
     *
     */
    @FeignClient(name="hello-service")                         //这里的hello-service是服务提供方的名称,而 @RequestMapping的hlleo为提供方的接口,
    而此接口的返回类型为String

    public interface FeignConsumerService{

    @RequestMapping("/hello")
    public String hello();
     }

    controlller:

    @RestController
    public class FeiConsumerController {
    
        @Autowired
        FeignConsumerService consumerService;
    
        @RequestMapping("feign-consumer")
        public String feignConsumer() {
           return consumerService.hello();
            //return "feign consumer call finished!!!";
        }
    
    }

     

  • 相关阅读:
    delphi7下调用微软的Web Services的心得
    Asp.net组件设计浅论
    STC系统烧写及STC12C5A60S2最小系统
    ENET 1.3.3 VC2005 下使用
    ENet library compilation record
    51定时器
    可靠的UDP编程(ENET库)
    ASP.NET MVC3布局页与分布页调用方式概述
    排除JQuery通过HttpGet调用WebService返回Json时“parserror”错误
    AJAX数据源协调处理思路
  • 原文地址:https://www.cnblogs.com/leeego-123/p/10535926.html
Copyright © 2011-2022 走看看