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

Spring Cloud多個微服務(wù)之間調(diào)用代碼實(shí)例

 更新時間:2019年12月17日 11:56:29   作者:指尖,寫不盡  
這篇文章主要介紹了Spring Cloud多個微服務(wù)之間調(diào)用代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了Spring Cloud多個微服務(wù)之間調(diào)用代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

現(xiàn)在又一個學(xué)生微服務(wù) user 和 學(xué)校微服務(wù) school,如果user需要訪問school,我們應(yīng)該怎么做?

1.使用RestTemplate方式

添加config

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTempldateConfig {
  @Bean
  @Scope("singleton")
  @LoadBalanced
  public RestTemplate restTempldate(){

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().clear();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    return restTemplate;
  }

}

@LoadBalanced是一個負(fù)載均衡注解,默認(rèn)是線性輪詢策略找到服務(wù)

調(diào)用:

Result result = restTemplate.postForObject("http://SPRING-SCHOOL/school/findAll", null,Result.class);
return result;

SPRING-SCHOOL 為school應(yīng)用名稱

2.使用 openfeign 實(shí)現(xiàn)系統(tǒng)見調(diào)用

引入包

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

編寫調(diào)用端代碼

import com.lvlvstart.spring.demo.common.entity.School;
import com.lvlvstart.spring.demo.common.msg.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@FeignClient("SPRING-SCHOOL")
public interface SchoolClient {

  @PostMapping(value = "/school/findAll")
  public Result<List<School>> findAll();

  @PostMapping(value = "/school/findById")
  public Result<School> findById(String schoolId);
}

啟動類添加注解 @EnableFeignClients

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients("com.lvlvstart.spring.demo.common.client")
public class SpringUserApplication {

  public static void main(String[] args) {
  SpringApplication.run(SpringUserApplication.class, args);
  }

}

調(diào)用

@Autowired
private SchoolClient schoolClient;

@PostMapping("findAllSchool")
public Result findAll(){
  return schoolClient.findAll();
}

完整代碼請訪問: https://github.com/halouprogramer/spring-cloud-demo

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

相關(guān)文章

  • 詳解JAVA設(shè)計模式之代理模式

    詳解JAVA設(shè)計模式之代理模式

    這篇文章主要介紹了JAVA設(shè)計模式之代理模式的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • SpringBoot web開發(fā)源碼深入分析

    SpringBoot web開發(fā)源碼深入分析

    Web開發(fā)的核心內(nèi)容主要包括內(nèi)嵌的Servlet容器和SpringMVCSpringBoot使用起來非常簡潔,大部分配置都有SpringBoot自動裝配,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • mybatis createcriteria和or的區(qū)別說明

    mybatis createcriteria和or的區(qū)別說明

    這篇文章主要介紹了mybatis createcriteria和or的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 如何利用Java使用AOP實(shí)現(xiàn)數(shù)據(jù)字典轉(zhuǎn)換

    如何利用Java使用AOP實(shí)現(xiàn)數(shù)據(jù)字典轉(zhuǎn)換

    這篇文章主要介紹了如何利用Java使用AOP實(shí)現(xiàn)數(shù)據(jù)字典轉(zhuǎn)換,AOP也是我們常說的面向切面編程,AOP在我們開發(fā)過程中應(yīng)用也比較多,在這里我們就基于AOP來實(shí)現(xiàn)一個數(shù)據(jù)字典轉(zhuǎn)換的案例
    2022-06-06
  • IDEA 使用 SpotBugs 找出你代碼中的bug問題

    IDEA 使用 SpotBugs 找出你代碼中的bug問題

    這篇文章主要介紹了IDEA 使用 SpotBugs 找出你代碼中的bug問題,重點(diǎn)給大家介紹SpotBugs 在 idea 中的安裝和使用,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • Spring?Boot之Validation自定義實(shí)現(xiàn)方式的總結(jié)

    Spring?Boot之Validation自定義實(shí)現(xiàn)方式的總結(jié)

    這篇文章主要介紹了Spring?Boot之Validation自定義實(shí)現(xiàn)方式的總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • spring boot教程之全局處理異常封裝

    spring boot教程之全局處理異常封裝

    這篇文章主要給大家介紹了關(guān)于spring boot教程之全局處理異常封裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Spring Cloud Stream如何實(shí)現(xiàn)服務(wù)之間的通訊

    Spring Cloud Stream如何實(shí)現(xiàn)服務(wù)之間的通訊

    這篇文章主要介紹了Spring Cloud Stream如何實(shí)現(xiàn)服務(wù)之間的通訊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Spring?Cloud?Alibaba微服務(wù)組件Sentinel實(shí)現(xiàn)熔斷限流

    Spring?Cloud?Alibaba微服務(wù)組件Sentinel實(shí)現(xiàn)熔斷限流

    這篇文章主要為大家介紹了Spring?Cloud?Alibaba微服務(wù)組件Sentinel實(shí)現(xiàn)熔斷限流過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • java8新特性-Stream入門學(xué)習(xí)心得

    java8新特性-Stream入門學(xué)習(xí)心得

    這篇文章主要介紹了java8新特性-Stream入門學(xué)習(xí)心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論