国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Cloud Feign簡單使用詳解

Spring Cloud Feign簡單使用詳解

2021-04-06 13:18Sam哥哥 Java教程

本篇文章主要介紹了Spring Cloud Feign簡單使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

概述

Spring Cloud EureKa Ribbon 服務注冊-發(fā)現(xiàn)-調(diào)用一文中簡單的介紹了在Spring Cloud中如何使用EureKa和Ribbon。文章中使用了RestTemplate去訪問其他的restful微服務接口。其實在Spring Cloud還可以使用Feign來訪問其他的restful微服務接口。使用起來更加的簡潔明了。

集成Feign

修改一下Spring Cloud EureKa Ribbon 服務注冊-發(fā)現(xiàn)-調(diào)用中order service的pom配置,把Fegin引入進來即可。

?
1
2
3
4
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

修改OrderApplication類,刪除如下代碼:

?
1
2
3
4
5
@Bean
@LoadBalanced
RestTemplate restTemplate() {
  return new RestTemplate();
}

并加上@EnableFeignClients注解。完整代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.springboot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
 
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class OrderApplication {
  public static void main(String[] args) {
    SpringApplication.run(OrderApplication.class, args);
  }
}

新增接口UserService,并使用@FeignClient注解。

?
1
2
3
4
5
6
7
8
9
10
package com.springboot;
 
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
 
@FeignClient(name="user")
public interface UserService {
  @GetMapping(value="/getUser")
  String getUser();
}

這里的@FeignClient(name="user")中的name=user表示要訪問user這個微服務。由于order這個微服務已經(jīng)集成了Eureka和Ribbon。那么使用@FeignClient(name="user")訪問user微服務的時候,已經(jīng)自動支持客戶端路由了。并且會從注冊中心中找到user這個微服務。

修改OrderController,注入UserService。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
 
  @Autowired
  private UserService userService;
 
  @GetMapping("/getOrderUser")
  public String getOrderUser() {
    return userService.getUser();
  }
}

這樣就無需使用

?
1
restTemplate.getForEntity("http://user/getUser",String.class).getBody();

來調(diào)用user服務中的getUser接口了。而是直接使用userService.getUser()就可以了。

啟動注冊中心以及user和order這兩個微服務。使用http://localhost:8883/getOrderUser

訪問一下。是可以返回

I am user list.

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

原文鏈接:http://blog.csdn.net/linsongbin1/article/details/79363400

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜精品久久久久久久久久久久 | 国产一级视频免费观看 | 依人免费视频 | 午夜欧美| 在线免费观看a视频 | 免费看一级电影 | 国产成人精品一区二区三区网站观看 | 91天堂| 国产日韩一区 | 综合色久| 久草热8精品视频在线观看 久久亚洲精品中文字幕 | 欧美亚洲一区 | 黄色免费av | 日本免费视频 | 久久久久高清视频 | 精品亚洲一区二区 | 中文字幕一区二区三区乱码图片 | 精品伊人 | 国产欧美一区二区三区在线看 | 老黄网站在线观看 | 亚洲免费视频观看 | 亚洲视频第一页 | 国产在线观 | 精品国产一区二区三区久久久蜜月 | 精品黑人一区二区三区久久 | 一级免费片 | 99久久视频 | 精品福利视频网站 | 成人欧美| 91视频进入 | 日本精品一区二区三区在线观看 | 国产黄色大全 | 精品一区视频 | 国产精品区一区二区三区 | 天天摸天天干 | 国产中文字幕在线免费观看 | 久久亚洲一区 | 最新高清无码专区 | 精品一二三四区 | 亚洲三级视频 | 日韩有码在线观看 |