Ribbon RestTemplate和Feign调用比较

本文对比了服务调用的几种方式,包括HttpClient、Okhttp、HttpURLConnection和Spring的RestTemplate。接着详细阐述了Ribbon与RestTemplate结合使用实现负载均衡的步骤,并介绍了Feign的配置和使用,强调了Feign的简洁性和动态负载选择服务的优势。最后总结指出,Feign简化了请求编写,而RestTemplate虽需手动配置,但能调用集群外服务。
摘要由CSDN通过智能技术生成

一、服务调用几种方式

  1. Httpclient
    使用HttpClient发送请求主要分为以下几步骤:

     创建 CloseableHttpClient对象或CloseableHttpAsyncClient对象,前者同步,后者为异步
     创建Http请求对象
     调用execute方法执行请求,如果是异步请求在执行之前需调用start方法
    
  2. Okhttp
    使用OkHttp发送请求主要分为以下几步骤:

     创建OkHttpClient对象
     创建Request对象
     将Request 对象封装为Call
     通过Call 来执行同步或异步请求,调用execute方法同步执行,调用enqueue方法异步执行
    
  3. HttpURLConnection
    HttpURLconnection是基于http协议的,支持get,post,put,delete等各种请求方式,最常用的就是get和post

  4. RestTemplate
    RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。

二、Ribbon+RestTemplate

配置一个eureka server注册中心,创建一个springboot项目
添加依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

application.properties配置文件

server.port=8761

eureka.instance.hostname=localhost
eureka:
    client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类EurekaServerApplication

package com.boss.springcloud.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

创建一个服务提供者 (eureka client),新建一个spring boot项目,通过改变端口号运行多个提供者

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值