Spring Cloud多個微服務(wù)之間調(diào)用代碼實(shí)例
這篇文章主要介紹了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í)有所幫助,也希望大家多多支持腳本之家。
- Spring?Cloud?Alibaba?Nacos服務(wù)治理平臺服務(wù)注冊、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問負(fù)載均衡訪問的問題
- SpringCloud feign微服務(wù)調(diào)用之間的異常處理方式
- spring cloud eureka微服務(wù)之間的調(diào)用詳解
- springcloud gateway如何實(shí)現(xiàn)路由和負(fù)載均衡
- Spring Cloud 負(fù)載均衡器 Ribbon原理及實(shí)現(xiàn)
- spring cloud 之 客戶端負(fù)載均衡Ribbon深入理解
- Spring Cloud實(shí)現(xiàn)微服務(wù)調(diào)用的負(fù)載均衡(詳解)
相關(guān)文章
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)換,AOP也是我們常說的面向切面編程,AOP在我們開發(fā)過程中應(yīng)用也比較多,在這里我們就基于AOP來實(shí)現(xiàn)一個數(shù)據(jù)字典轉(zhuǎn)換的案例2022-06-06Spring?Boot之Validation自定義實(shí)現(xiàn)方式的總結(jié)
這篇文章主要介紹了Spring?Boot之Validation自定義實(shí)現(xiàn)方式的總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Spring Cloud Stream如何實(shí)現(xiàn)服務(wù)之間的通訊
這篇文章主要介紹了Spring Cloud Stream如何實(shí)現(xiàn)服務(wù)之間的通訊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10Spring?Cloud?Alibaba微服務(wù)組件Sentinel實(shí)現(xiàn)熔斷限流
這篇文章主要為大家介紹了Spring?Cloud?Alibaba微服務(wù)組件Sentinel實(shí)現(xiàn)熔斷限流過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06java8新特性-Stream入門學(xué)習(xí)心得
這篇文章主要介紹了java8新特性-Stream入門學(xué)習(xí)心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03