Spring Cloud--Feign负载均衡

前言

Github:https://github.com/yihonglei/thinking-in-springcloud

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服务接口)

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();

}

@FeignClient注解指定所抽象的服务,name服务名称,path路径。 

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(启动类)

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,需要在启动类加上@EnableFeignClients注解,启用feign客户端。

执行main方法,启动服务。

六 feign客户端访问

1、注册中心

http://localhost:9000

注册中心可以看到eureka-provider-order-8001服务提供者,eureka-consumer-feign-8007服务消费者。

2、访问接口

http://localhost:8007/user/queryUserInfo

发布了502 篇原创文章 · 获赞 358 · 访问量 118万+

猜你喜欢

转载自blog.csdn.net/yhl_jxy/article/details/94391154