SpringCloud使用Fegin远程调用

SpingCould使用Fegin远程调用

SpringCloud是一个完整的微服务解决方案 将许多优秀的框架以及组件集成进来 所以说它是一个集大成者

它基于SpringBoot构建 SpringCloud是离不开Springboot的

SqiangCloud技术栈

服务发现——Netflix Eureka
服务调用——Netflix Feign
配置中心——Spring Cloud Config
服务网关——Netflix Zuul
消息总线——Spring Cloud Bus
熔断器——Netflix Hystrix
负载均衡
数据监控等

本篇文章实现SpringCloud使用Fegin组件远程调用一个简单demo

1.先创建 一个Maven父工程不用添加任何依赖

2.在父工程下创建一个 eureka-server SpringInitializr项目作为子项目

这里需要开启一个Eureka-Server依赖

之后配置yml

spring:
  application:
    name: eureka-server
server:
  port: 8080
eureka:
  server:
    enableSelfPreservation: true
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在eureka-Server项目启动类使用注解开启服务

@EnableEurekaServer

在这里插入图片描述
之后启动项目访问 http://localhost:8080/
在这里插入图片描述
这里便是启动服务端 Eureka的服务注册中心
出现服务注册中心后就是要创建客户端向Eureka服务中心注册

创建第二个SpringInitializr 模块名为 eureka-client

添加客户端依赖
在这里插入图片描述
在eureka-client项目启动类处开启注解

@EnableEurekaClient

在这里插入图片描述
配置yml文件

spring:
  application:
    name: eureka-client
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/kt_edu?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai
      username: root
      password: hzy
      driverClassName: com.mysql.jdbc.Driver
      initialSize: 5  #初始建立连接数量
      minIdle: 5  #最小连接数量
      maxActive: 20 #最大连接数量
      maxWait: 10000  #获取连接最大等待时间,毫秒
      testOnBorrow: true #申请连接时检测连接是否有效
      testOnReturn: false #归还连接时检测连接是否有效
      timeBetweenEvictionRunsMillis: 60000 #配置间隔检测连接是否有效的时间(单位是毫秒)
      minEvictableIdleTimeMillis: 300000  #连接在连接池的最小生存时间(毫秒)
eureka:
  client:
    registerWithEureka: true #服务注册开关
    fetchRegistry: true #服务发现开关
    serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔
      defaultZone: http://localhost:8080/eureka/
  instance:
    prefer-ip-address:  true  #将自己的ip地址注册到Eureka服务中
    ip-address: ${IP_ADDRESS:127.0.0.1}
    instance-id: ${spring.application.name}:${server.port} #指定实例id
server:
  port: 8090

OK后再启动 eureka-client项目

去注册中心看是否存在
加粗样式
这里们看到已经成功注册到了Server注册中心

红色的地方 是我没有关闭自我保护机制
配置文件加上或者修改这个配置
eureka.server.enable-self-preservation=false

下面就可以在Client子工程写我们的方法了
这里的一个查询方法就跳过了
在这里插入图片描述
最后一步再创建一个客户端工程

同eureka-client创建一样的工程 添加一样的依赖
加上一个依赖 使用Fegin

     <!-- 使用eureka -->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>

配置yml

spring:
  application:
    name: eureka-client
  datasource:
    druid:
      url: jdbc:mysql://121.199.47.53:3306/kt_edu?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai
      username: root
      password: hzy
      driverClassName: com.mysql.jdbc.Driver
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 10000
      testOnBorrow: true
      testOnReturn: false
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
  instance:
    prefer-ip-address:  true
    ip-address: ${IP_ADDRESS:127.0.0.1}
    instance-id: ${spring.application.name}:${server.port}
    hostname: localhost
server:
  port: 8091



这里实体类就没放一个Module了
把前面的实体类扣过来

创建包 FeignClient 下创建 接口 IProductClient

package com.xiaohe.FeignClient;

import com.xiaohe.pojo.Teacher;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@FeignClient(name = "EUREKA-CLIENT")
public interface IProductClient {

    @GetMapping("/edu/teacher/findAll")
    List<Teacher> findAll();

}


@FeignClient(name = “EUREKA-CLIENT”)

里面的就是注册到服务中心的名字
路径就是第一个客户端Controller的全访问路径
在这里插入图片描述
最后这边一个Controller访问这个方法调用第一个客户端的查询方法
在这里插入图片描述
具体就是这个结构

测试 我们就可以通过这个8091端口去访问 到 8090端口的方法

使用Feign可使得Web服务客户端的写入更加方便

以上就实现了SpringCloud强大组件Fegin的远程调用

源码联系 2010944641@qq.com

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值