【记录贴】SpringCloud自学之路-Feign

前言

记录一次学习SpringCloud的过程,不断积累经验,手撸方能熟能生巧。

新手上路难免有误,人非圣贤,欢迎各位指出不足之处,虚心听取各位的建议与意见。

项目源码:https://github.com/Ahua0918/hwa

一、Feign概念

Feigh是一个声明式web服务客户端。它能让开发web服务变得容易。使用Feign需要创建一个接口并注解它。它拥有包括Feign注解和JAX-RS注解的可插拔支持。在使用Feign时,Spring Cloud集成了Ribbon和Eureka来提供负载均衡的Htpp客户端。在前面注册数据微服务里,可以注册多个微服务,Feign会从注册中心获知这个信息,然后由Feign这个客户端自己决定调用哪个,这个叫客户端负载均衡。

二、环境准备

2.1 基于Maven父项目创建子项目,右击项目--Module--选择Maven--选择对应JDK版本--点击Next--输入模块名hwa-eureka-feign即可

2.2 Pom文件导入Feign依赖

<?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>
        <parent>
            <artifactId>hwa</artifactId>
            <groupId>com.lee</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>


        <artifactId>hwa-eureka-feign</artifactId>
        <packaging>jar</packaging>


        <description>SuperFeign</description>

        <dependencies>
            <!--  Feign客户端依赖  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>          
        </dependencies>
</project>

  2.3 创建Feign客户端

        Feign客户端,通过注解方式访问hwa-eureka-client的hi路径,hwa-eureka-client既不是域名也不是ip地址,而是数据服务在eureka注册中心的名称。

        这里只是指定了要访问的微服务名称,但是没有指定端口号到底是哪个。

package com.lee.feign.client;

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

@FeignClient(name = "hwa-eureka-client")
public interface HelloFeignClient {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromFeign(@RequestParam(value = "name") String name);
}

2.4 编写Controller

      在Web层的Controller层,对外暴露一个“/hi”的Api接口,通过上面定义的Feign客户端HelloFeignClient来消费服务。

package com.lee.feign.controller;


import com.lee.feign.client.HelloFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloFeignController {

    //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
    @Autowired
    HelloFeignClient helloFeignClient;

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name){
        return helloFeignClient.sayHiFromFeign(name);
    }
}

2.5 编写配置文件application.yml

#注册中心端口
server:
  port: 8884
spring:
  application:
    #服务名称
    name: hwa-eureka-feign
#主机名
eureka:
  client:
    #eureka服务地址
    #注明自己的服务注册中心的地址,与Eureka Server中的配置application.yml相对应
    service-url:
      defaultZone: http://localhost:1116/eureka/

2.6 编写Feign客户端启动类

package com.lee.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
@EnableDiscoveryClient
@EnableFeignClients     //通过注解@EnableFeignClients配置Feign客户端功能
public class HwaFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(HwaFeignApplication.class, args);
    }
}

2.7 启动并访问

      依次启动HwaEurekaApplication,HwaClientApplication(启动多个客户端,端口号分别是1118,1119),HwaFeignApplication,然后访问地址http://localhost:8884/hi?name=hwa,多刷新几遍,会发现浏览器交替显示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值