欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud使用Feign实现服务调用

程序员文章站 2023-12-11 20:15:28
spring cloud feign简介 spring cloud feign也是一个基础工具类,它整合了spring cloud ribbon和spring clou...

spring cloud feign简介

spring cloud feign也是一个基础工具类,它整合了spring cloud ribbon和spring cloud hystrix,除了提供这两者的强大功能以外,它还提供了一种声明式的web服务客户端定义方式。使用它可以进行服务的消费,但是它的客户端负载平衡仍是通过ribbon实现的

使用spring cloud feign

创建一个springboot工程,作为服务调用方

1.pom.xml

<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-web</artifactid>
 </dependency>

 <dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-starter-eureka</artifactid>
 </dependency>
 <dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-starter-feign</artifactid>
 </dependency>

 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-test</artifactid>
  <scope>test</scope>
</dependency>

spring-cloud-starter-feign加入了feign的依赖

2.启动类

@enablefeignclients
@enablediscoveryclient
@springbootapplication
public class feignconsumerapplication {

 public static void main(string[] args) {
  springapplication.run(feignconsumerapplication.class, args);
 }
}

@enablefeignclients:开启spring cloud feign的支持功能

3.服务层

@feignclient("hello-service")
 public interface helloservice {

 @requestmapping(value = "/hello", method = requestmethod.get)
 string hello();
}

@feignclient(“hello-service”):制定服务名来绑定服务

注:服务名不区分大小写

@requestmapping:指定调用服务中的具体方法

4.controller层

@controller
public class consumercontroller {

 @autowired
 private helloservice helloservice;

 @requestmapping(value = "/feign-consumer", method = requestmethod.get)
 @responsebody
 public string helloconsumer() {
  return helloservice.hello();
 }
}

在方法中调用这个绑定了hello-service服务接口的客户端向该服务发起/hello接口的调用

5.配置类

server.port=9001

spring.application.name=feign-consumer
eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/

启动程序以后,在浏览器中输入http://localhost:9001/feign-consumer出现下图:

SpringCloud使用Feign实现服务调用

- 提升使用,带参数的请求
在具体业务中的接口会比之前程序的复杂得多,这里介绍一下feign对集中不同形式参数的绑定方法。有@requestparam、@requestheader、@requestbody

1.改造服务提供类的service层,添加几个方法

@requestmapping(value = "/hello1", method = requestmethod.get)
@responsebody
public string hello(@requestparam string name) {
 return "hello " + name;
}

@requestmapping(value = "/hello2", method = requestmethod.get)
@responsebody
public user hello(@requestheader string name, @requestheader integer age) {
 return new user(name, age);
}

@requestmapping(value = "/hello3", method = requestmethod.post)
@responsebody
public string hello(@requestbody user user) {
 return "hello " + user.getname() + ", " + user.getage();
}

2.添加一个javabean

public class user {
 private string name;
 private integer age;

 public user() {
 }

 public user(string name, integer age) {
  this.name = name;
  this.age = age;
 }

 public string getname() {
  return name;
 }

 public void setname(string name) {
  this.name = name;
 }

 public integer getage() {
  return age;
 }

 public void setage(integer age) {
  this.age = age;
 }

 @override
 public string tostring() {
  return "user{" +
    "name='" + name + '\'' +
    ", age=" + age +
    '}';
 }
}

3.修改服务调用类的接口

@requestmapping(value = "/hello1", method = requestmethod.get)
string hello(@requestparam("name") string name);

@requestmapping(value = "/hello2", method = requestmethod.get)
user hello(@requestheader("name") string name, @requestheader("age") integer age);

@requestmapping(value = "/hello3", method = requestmethod.post)
string hello(@requestbody user user);

这里在定义各参数绑定时@requestparam、@requestheader等可以指定参数名称的注解,但它们的value不能少,否则会报错,这和springmvc中有一点不同

4.在服务调用类controller层添加一个测试的接口

 @requestmapping(value = "/feign-consumer2", method = requestmethod.get)
 @responsebody
 public string helloconsumer2() {
  string s2 = helloservice.hello("dayday");
  return s2;
 }

启动以后访问浏览器:

SpringCloud使用Feign实现服务调用

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: