8、服务发现&服务消费者Feign

栏目: Java · 发布时间: 4年前

内容简介:公众号:java乐园spring cloud的Netflix中提供了两个组件实现软负载均衡调用,分别是Ribbon和Feign。上一篇和大家一起学习了Ribbon。Ribbon :Spring Cloud Ribbon是基于HTTP和TCP的客户端负载工具,它是基于Netflix Ribbon实现的,它可以在客户端配置 ribbonServerList(服务端列表),然后轮询请求以实现均衡负载。

公众号:java乐园

spring cloud的Netflix中提供了两个组件实现软负载均衡调用,分别是Ribbon和Feign。上一篇和大家一起学习了Ribbon。

Ribbon :Spring Cloud Ribbon是基于HTTP和TCP的客户端负载工具,它是基于Netflix Ribbon实现的,它可以在客户端配置 ribbonServerList(服务端列表),然后轮询请求以实现均衡负载。

Feign :spring cloud feign 是一个使用起来更加方便的 HTTP 客戶端。 在使用ribbon时,通常会使用RestTemplate实现对http请求的封装,形成了模板化的调用方法。spring cloud feign在此基础上做了进一步的封装,Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,完全感知不到这是远程方法,更感知不到这是个HTTP请求。

1、 新建项目sc-eureka-client-consumer-feign,对应的pom.xml文件如下

<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>spring-cloud</groupId>
    <artifactId>sc-eureka-client-consumer-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-eureka-client-consumer-feign</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
    
        <!-- 说明是一个 eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- <dependency> 
                <groupId>org.springframework.cloud</groupId> 
                <artifactId>spring-cloud-starter-feign</artifactId> 
                <version>1.4.5.RELEASE</version> 
           </dependency> -->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        
    </dependencies>
</project>

备注:spring cloud 2.x后spring-cloud-starter-feign已经被标识为过期,推荐使用spring-cloud-starter-openfeign

8、服务发现&服务消费者Feign

2、 新建spring boot启动类ConsumerFeignApplication.java

package sc.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

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

}

3、 创建配置文件bootstrap.yml和application.yml,对应的内容如下

bootstrap.yml

server:
  port: 5800

application.yml

spring:
  application:
    name: sc-eureka-client-consumer-feign

eureka:
  client:
    registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true
    fetchRegistry: true #是否从Eureka中获取注册信息,默认为true
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/

4、 编写feign客户端

package sc.consumer.service;

import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import sc.consumer.model.User;

@FeignClient(value="sc-eureka-client-provider")
public interface UserService {

    @GetMapping("/user/getUser/{id}")
    Map<String, Object> getUser(@PathVariable(value ="id") Long id);

    @RequestMapping("/user/listUser")
    Map<String, Object> listUser();

    @PostMapping("/user/addUser")
    Map<String, Object> addUser(@RequestBody User user);

    @PutMapping("/user/updateUser")
    Map<String, Object> updateUser(@RequestBody User user);

    @DeleteMapping("/user/deleteUser/{id}")
    Map<String, Object> deleteUser(@PathVariable(value ="id") Long id);

}

5、 分别启动注册中心项目sc-eureka-server和服务提供者sc-eureka-client-provider

6、 启动项目sc-eureka-client-consumer-feign,并验证是否启动成功

方法一

8、服务发现&服务消费者Feign

方法二

8、服务发现&服务消费者Feign

7、 使用postman验证

查询:

http://127.0.0.1 :5800/feign/user/getUser/4

8、服务发现&服务消费者Feign

列表:

http://127.0.0.1 :5800/feign/user/listUser

8、服务发现&服务消费者Feign

添加:

http://127.0.0.1 :5800/feign/user/addUser

8、服务发现&服务消费者Feign

更新:

http://127.0.0.1 :5800/feign/user/updateUser

8、服务发现&服务消费者Feign

删除:

http://127.0.0.1 :5800/feign/user/deleteUser/6

8、服务发现&服务消费者Feign

备注:

sc-eureka-client-provider项目的UserController.java 需要修正

源码

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-eureka-client-consumer-feign

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Smashing Book

The Smashing Book

Jacob Gube、Dmitry Fadeev、Chris Spooner、Darius A Monsef IV、Alessandro Cattaneo、Steven Snell、David Leggett、Andrew Maier、Kayla Knight、Yves Peters、René Schmidt、Smashing Magazine editorial team、Vitaly Friedman、Sven Lennartz / 2009 / $ 29.90 / € 23.90

The Smashing Book is a printed book about best practices in modern Web design. The book shares technical tips and best practices on coding, usability and optimization and explores how to create succes......一起来看看 《The Smashing Book》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具