欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud使用Feign實現(xiàn)服務(wù)調(diào)用

 更新時間:2019年04月24日 09:53:34   作者:小氣鬼Sweet  
這篇文章主要為大家詳細(xì)介紹了SpringCloud使用Feign實現(xiàn)服務(wù)調(diào)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Spring Cloud Feign簡介

Spring Cloud Feign也是一個基礎(chǔ)工具類,它整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供這兩者的強大功能以外,它還提供了一種聲明式的Web服務(wù)客戶端定義方式。使用它可以進行服務(wù)的消費,但是它的客戶端負(fù)載平衡仍是通過Ribbon實現(xiàn)的

使用Spring Cloud Feign

創(chuàng)建一個SpringBoot工程,作為服務(wù)調(diào)用方

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.服務(wù)層

@FeignClient("hello-service")
 public interface HelloService {

 @RequestMapping(value = "/hello", method = RequestMethod.GET)
 String hello();
}

@FeignClient(“hello-service”):制定服務(wù)名來綁定服務(wù)

注:服務(wù)名不區(qū)分大小寫

@RequestMapping:指定調(diào)用服務(wù)中的具體方法

4.Controller層

@Controller
public class ConsumerController {

 @Autowired
 private HelloService helloService;

 @RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
 @ResponseBody
 public String helloConsumer() {
  return helloService.hello();
 }
}

在方法中調(diào)用這個綁定了hello-service服務(wù)接口的客戶端向該服務(wù)發(fā)起/hello接口的調(diào)用

5.配置類

server.port=9001

spring.application.name=feign-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

啟動程序以后,在瀏覽器中輸入http://localhost:9001/feign-consumer出現(xiàn)下圖:

- 提升使用,帶參數(shù)的請求
在具體業(yè)務(wù)中的接口會比之前程序的復(fù)雜得多,這里介紹一下Feign對集中不同形式參數(shù)的綁定方法。有@RequestParam、@RequestHeader、@RequestBody

1.改造服務(wù)提供類的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.修改服務(wù)調(diào)用類的接口

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

這里在定義各參數(shù)綁定時@RequestParam、@RequestHeader等可以指定參數(shù)名稱的注解,但它們的value不能少,否則會報錯,這和SpringMVC中有一點不同

4.在服務(wù)調(diào)用類Controller層添加一個測試的接口

 @RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET)
 @ResponseBody
 public String helloConsumer2() {
  String s2 = helloService.hello("dayday");
  return s2;
 }

啟動以后訪問瀏覽器:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論