Feign負載均衡

前言

Github:https://github.com/yihonglei/SpringCloud-Study

Eureka註冊中心:eureka-server

服務提供者(訂單服務):eureka-provider-order

Feign-api(服務接口抽象):eureka-feign-api

Feign客戶端消費:eureka-consumer-feign

一 Feign概述

Feign是一個聲明式的僞RPC的REST客戶端,基於接口的註解方式,很方便客戶端配置。

Spring Cloud集成Ribbon和Eureka以在使用Feign時提供負載均衡的http客戶端。

二 啓動eureka-server(註冊中心)

執行eureka-server項目EurekaServerApplication類的main方法。

三 啓動eureka-provider-order(服務提供者)

執行eureka-provider-order項目EurekaOrderApplication類的main方法。

四 eureka-feign-api(feign接口抽象)

1、項目結構

2、pom.xml依賴

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

3、OrderApi(feign抽象order服務接口)

需要通過@FeignClient註解指定所抽象的服務。

package com.lanhuigu.order;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * order服務,FeignClient標註服務提供者信息
 *
 * @auther: yihonglei
 * @date: 2019-06-30 21:17
 */
@FeignClient(name = "eureka-provider-order", path = "/order")
public interface OrderApi {

    @RequestMapping("/queryOrderInfo")
    String queryOrdersByUserId();

}

4、打成jar包,供別的工程使用

五 啓動eureka-consumer-feign(feign客戶端消費order服務)

1、項目結構

2、pom.xml依賴

需要引入eureka-feign-api。

<!-- Eureka Client -->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!-- feign-api -->
<dependency>
   <groupId>com.lanhuigu</groupId>
   <artifactId>eureka-feign-api</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

3、application.properties

# 註冊到eureka服務端的微服務名稱
spring.application.name=eureka-consumer-feign

# 服務提供端口
server.port=8007
# 註冊到eureka服務端的地址
eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
# 顯示指定微服務的名稱,默認ip:應用名稱:端口(192.168.1.7:eureka-consumer-feign:8007)
eureka.instance.instance-id=eureka-consumer-feign-8007
eureka.instance.prefer-ip-address=true

4、UserController(控制層)

package com.lanhuigu.feign.controller;

import com.lanhuigu.order.OrderApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @RestController這個註解等價於spring mvc用法中的@Controller+@ResponseBody
 *
 * @auther: yihonglei
 * @date: 2019-06-17 22:30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private OrderApi orderApi;

    @RequestMapping(value = "/queryUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public String queryUserInfo() {
        String orderInfo = orderApi.queryOrdersByUserId();

        return orderInfo;
    }
}

5、EurekaConsumerFeignApplication(啓動類)

客戶端使用feign,需要在啓動類加上@EnableFeignClients註解,啓用feign客戶端。

執行main方法,啓動服務。

package com.lanhuigu.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @SpringBootApplication 啓動一個Spring Boot應用程序
 * @EnableDiscoveryClient 服務發現與註冊,當應用啓動時,將應用註冊到配置的註冊中心
 *
 * @auther: yihonglei
 * @date: 2019-06-19 11:27
 */
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients(basePackages = "com.lanhuigu")
public class EurekaConsumerFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerFeignApplication.class, args);
    }
}

六 feign客戶端訪問

1、註冊中心

http://localhost:9000

註冊中心可以看到eureka-provider-order-8001服務提供者,eureka-consumer-feign-8007服務消費者。

2、訪問接口

http://localhost:8007/user/queryUserInfo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章