Spring Cloud 入门Eureka -Consumer服务消费(声明式Feign)(三)

  Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。我们只需要通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

1、pom.xml,这里有所不同dependencyManagement有变化,之前的跑不通,依赖标红处为修改

<?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.example</groupId>
    <artifactId>eurekaRibbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eurekaRibbon</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.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.SR3</version> -->
                <version>Camden.SR4</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>

2、主类:通过@EnableFeignClients注解开启扫描Spring Cloud Feign客户端的功能

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
//扫描com.example.demo.service下的FeignClient
@EnableFeignClients(basePackages={"com.example.demo.service"})
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaFeignApplication {

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

3、ClientFeignController

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.service.IFeign;

@RestController
public class ClientFeignController {

    @Autowired
    IFeign iFeign;

    @GetMapping("/consumer")
    public String all() {
        // 发起REST请求
        return iFeign.all();
    }

}

4、IFeign   一个FeignClient 声明 服务提供者的信息

package com.example.demo.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//声明服务提供名
@FeignClient("eurekaClient")
public interface IFeign {
    //指定要消费的接口名称
//    @GetMapping("/consumer")
    @RequestMapping(value="/all",method={RequestMethod.GET})
    String all();
}

 比上篇使用Ribbon @LoadBalanced,更加具有可配置性

 如果使用Dalston版,FeignClient使用GetMapping  会报java.lang.NoClassDefFoundError: feign/Feign$Builder,网上查了很多 暂时没有找到原因,  故而 换成Camden版。

原文地址:https://www.cnblogs.com/zwdx/p/9141367.html

时间: 2024-08-27 01:45:54

Spring Cloud 入门Eureka -Consumer服务消费(声明式Feign)(三)的相关文章

Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入how的范畴. 1)编程式实现: 每一个要素(where,what,how)都需要用具体代码实现来表示.传统的方式一般都是编程式实现,业务开发者需要关心每一处逻辑 2)声明式实现: 只需要声明在哪里(where )做什么(what),而无需关心如何实现(how).Spring的AOP就是一种声明式实现,

Spring Cloud 入门教程(一): 服务注册

1.  什么是Spring Cloud? Spring提供了一系列工具,可以帮助开发人员迅速搭建分布式系统中的公共组件(比如:配置管理,服务发现,链路开关,智能路由,微代理,控制总线,一次性令牌,全局锁,主节点选举, 分布式session, 集群状态).协调分布式环境中各个系统,为各类服务提供模板性配置.使用Spring Cloud, 开发人员可以搭建实现了这些样板的应用,并且在任何分布式环境下都能工作得非常好,小到笔记本电脑, 大到数据中心和云平台. Spring Cloud官网的定义比较抽象

Spring cloud入门Eureka篇(一)

一.前言 springcloud是一系列框架的有序集合.它利用spring boot 的开发便利性巧妙的简化了分布式系统基础设施的开发,如服务发现与注册.配置中心.消息总线.负载均衡.断路器.数据监控等,都可以用spring boot 的风格做到一键启动. 本篇介绍spring cloud 中的eureka快速入门. 二.简单介绍 Eureka是Netflix的子模块,它是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移. 原文地址:https://www.cnblogs

Spring Cloud入门 (6) - 微服务与消息驱动

1.Spring Cloud Stream 介绍 Spring Cloud Stream 是一个用于构建消息驱动微服务的框架.使用Stream 框架,我们不必关心如何连接各个消息代理中间件,也不必关系如何进行消息的发送与接收,只需要简单的进行配置就可以实现这些功能. 2.消息代理中间件 Spring Cloud Stream 的绑定器提供了 RabbitMQ 与 Kafka 两个消息代理中间件的实现 3.安装 RabbitMQ 我这里直接使用 docker 安装,记得要安装 management

Spring Cloud中Eureka的服务注册

Eureka中的服务注册,分为服务注册中心,和服务注册这. 一,服务注册者 服务注册者在启动的时候会通过发送rest请求的方式将自己注册到Eureka Server上.Eureka Server将服务注册的原信息保存在双层结构的map上,第一层的key是服务名,第二层的key是具体服务实例名. 在注册完成后服务提供者会维护一个心跳用来持续的,调用续约服务的时间间隔通过eureka.Instance.less-renewal-interval-in-seconds来设置,默认是30秒. 要开启服务

Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务

前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息"hello world". 但自己的配置文件中必须配置config server的URL(http://localhost:8888), 如果把config server搬到另外一个独立IP上, 那么作为一个client的hello world应用必须修改自己的bootstrap.yml中的config server的URL地址.这明显是不够方便的. 既然confi

Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡

接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改为8763.然后启动这两个Spring Boot应用, 就可以得到两个Hello World服务.这两个Hello world都注册到了eureka服务中心.这时候再访问http://localhost:8761, 可以看到两个hello world服务已经注册.(服务与注册参见Spr

微服务Spring Cloud 入门

什么是微服务? 微服务就是把原本臃肿的一个项目的所有模块拆分开来并做到互相没有关联,甚至可以不使用同一个数据库.   比如:项目里面有User模块和Power模块,但是User模块和Power模块并没有直接关系,仅仅只是一些数据需要交互,那么就可以吧这2个模块单独分开来,当user需要调用power的时候,power是一个服务方,但是power需要调用user的时候,user又是服务方了, 所以,他们并不在乎谁是服务方谁是调用方,他们都是2个独立的服务,这时候,微服务的概念就出来了. 微服务和分

《Spring Cloud与Docker微服务架构实战》配套代码

不才写了本使用Spring Cloud玩转微服务架构的书,书名是<Spring Cloud与Docker微服务架构实战> - 周立,已于2017-01-12交稿.不少朋友想先看看源码,现将代码放出. 本次放出的代码: 共计70+个DEMO 覆盖Eureka.Ribbon.Feign.Hystrix.Zuul.Spring Cloud Config.Spring Cloud Bus.Spring Cloud Sleuth.Docker.Docker Compose等. 1-11章代码地址: ht