最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微服務(wù)進(jìn)行遠(yuǎn)程調(diào)用其他服務(wù)方式

 更新時(shí)間:2026年01月23日 09:13:05   作者:裊沫  
文章主要介紹了如何將本地服務(wù)注冊到Nacos地址上,并詳細(xì)描述了服務(wù)調(diào)用和負(fù)載均衡的實(shí)現(xiàn)方法,包括使用`@EnableDiscoveryClient`注解開啟服務(wù)調(diào)用、編寫調(diào)用接口、創(chuàng)建RestTemplate配置類以及使用負(fù)載均衡依賴進(jìn)行服務(wù)調(diào)用

前置知識:將本地服務(wù)注冊到nacos地址上:

1、服務(wù)調(diào)用

現(xiàn)在我們需要使用service-order服務(wù)調(diào)用sercice-business服務(wù)

使用@EnableDiscoveryClient注解, 開啟服務(wù)調(diào)用

引入依賴

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

需要被調(diào)用的服務(wù)啟動類:

@EnableDiscoveryClient //開啟服務(wù)調(diào)用
@SpringBootApplication
public class BusinessMainApplication{
    public static void main(String[] args) {
        SpringApplication.run(BusinessMainApplication.class,args);
    }
}

編寫需要進(jìn)行調(diào)用的接口:

    @GetMapping("/business/{id}")
    public Business getById(@PathVariable("id") String id){
        return  businessService.getBusiness(id);
    }

創(chuàng)建RestTemplate配置類,防止new 多個(gè)對象

@Configuration
public class OrderConfig {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

在服務(wù)層進(jìn)行調(diào)用另外一個(gè)服務(wù)

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public Order getById(Long id) {
        Order order = new Order();
        order.setTotalAmount(new BigDecimal("1000"));
        order.setNickName("尚硅谷");
        order.setId(id);
        order.setProductList(Lists.newArrayList());
        return order;
    }

    @Override
    public Order createOrder(Long userId, Long productId) {
        //進(jìn)行遠(yuǎn)程調(diào)用獲取商品數(shù)據(jù)
        Business businessById = getBusinessById(productId.toString());

        Order order = new Order();
        order.setId(1L);
        order.setAddress("將軍大道108號");
        order.setTotalAmount(BigDecimal.valueOf(businessById.getPrice() * businessById.getCount()));
        order.setNickName("尚硅谷");
        order.setUserId(userId);
        order.setProductList(Arrays.asList(businessById));
        return order;
    }

    /**
     * 手動選擇需要的實(shí)例進(jìn)行調(diào)用
     * @param businessId
     * @return
     */
    public Business getBusinessById(String businessId){
        List<ServiceInstance> instances = discoveryClient.getInstances("service-business");

        //獲取服務(wù)的第幾個(gè)實(shí)例對象
        ServiceInstance instance = instances.get(0);
        //端口的URL
        String url = "http://" + instance.getHost() + ":"  + instance.getPort() + "/business/" + businessId;

        log.info("遠(yuǎn)程調(diào)用的URL:" + url);

        //進(jìn)行遠(yuǎn)程調(diào)用
        Business forObject = restTemplate.getForObject(url, Business.class);
        log.info("返回的數(shù)據(jù):" + forObject.toString());
        return forObject;
    }

}

2、負(fù)載均衡

如果被調(diào)用的服務(wù)有多個(gè)實(shí)例,現(xiàn)在我們?yōu)榱藴p輕某個(gè)服務(wù)的壓力,對服務(wù)進(jìn)行輪詢調(diào)用:

現(xiàn)在我們需要在調(diào)用方,使用loadBanlancer依賴

依賴引入:

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

依賴導(dǎo)入:

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Override
    public Order getById(Long id) {
        Order order = new Order();
        order.setTotalAmount(new BigDecimal("1000"));
        order.setNickName("尚硅谷");
        order.setId(id);
        order.setProductList(Lists.newArrayList());
        return order;
    }

    @Override
    public Order createOrder(Long userId, Long productId) {
        //進(jìn)行遠(yuǎn)程調(diào)用獲取商品數(shù)據(jù)
//        Business businessById = getBusinessById(productId.toString());
//        Business businessById = getBusinessByIdWithLoadBalancer(productId.toString());
        Business businessById = getBusinessByIdWithAnnotation(productId.toString());

        Order order = new Order();
        order.setId(1L);
        order.setAddress("將軍大道108號");
        order.setTotalAmount(BigDecimal.valueOf(businessById.getPrice() * businessById.getCount()));
        order.setNickName("尚硅谷");
        order.setUserId(userId);
        order.setProductList(Arrays.asList(businessById));
        return order;
    }

    /**
     * 手動選擇需要的實(shí)例進(jìn)行調(diào)用
     * @param businessId
     * @return
     */
    public Business getBusinessById(String businessId){
        List<ServiceInstance> instances = discoveryClient.getInstances("service-business");

        //獲取服務(wù)的第幾個(gè)實(shí)例對象
        ServiceInstance instance = instances.get(0);
        //端口的URL
        String url = "http://" + instance.getHost() + ":"  + instance.getPort() + "/business/" + businessId;

        log.info("遠(yuǎn)程調(diào)用的URL:" + url);

        //進(jìn)行遠(yuǎn)程調(diào)用
        Business forObject = restTemplate.getForObject(url, Business.class);
        log.info("返回的數(shù)據(jù):" + forObject.toString());
        return forObject;
    }


    /**
     * 使用LoadBalancer進(jìn)行負(fù)載均衡
     * @param businessId
     * @return
     */
    public Business getBusinessByIdWithLoadBalancer(String businessId){

        ServiceInstance choose = loadBalancerClient.choose("service-business");
        //端口的URL
        String url = "http://" + choose.getHost() + ":"  + choose.getPort() + "/business/" + businessId;

        log.info("IP:" + choose.getHost() + ":" + choose.getPort());
        log.info("遠(yuǎn)程調(diào)用的URL:" + url);

        //進(jìn)行遠(yuǎn)程調(diào)用
        Business forObject = restTemplate.getForObject(url, Business.class);
        log.info("返回的數(shù)據(jù):" + forObject.toString());
        return forObject;
    }


    /**
     * 使用注解@LoadBalancer進(jìn)行負(fù)載均衡
     * 需要在restTemplate上添加注解LoadBalancer
     * @param businessId
     * @return
     */
    public Business getBusinessByIdWithAnnotation(String businessId){

        String url = "http://service-business/business/" + businessId;


        //進(jìn)行遠(yuǎn)程調(diào)用
        Business forObject = restTemplate.getForObject(url, Business.class);
        log.info("返回的數(shù)據(jù):" + forObject.toString());
        return forObject;
    }


}

其中,第三種,使用注解的方式進(jìn)行負(fù)載均衡,需要將注解添加在restTemplate配置類上

@Configuration
public class OrderConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java8如何使用Lambda表達(dá)式簡化代碼詳解

    Java8如何使用Lambda表達(dá)式簡化代碼詳解

    這篇文章主要給大家介紹了關(guān)于Java8如何使用Lambda表達(dá)式簡化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java中HashTable和HashMap的區(qū)別_動力節(jié)點(diǎn)Java學(xué)院整理

    Java中HashTable和HashMap的區(qū)別_動力節(jié)點(diǎn)Java學(xué)院整理

    HashTable和HashMap主要的區(qū)別有:線程安全性,同步(synchronization),以及速度。接下來通過本文給大家簡單介紹下HashTable和HashMap的區(qū)別,需要的的朋友參考下吧
    2017-04-04
  • 有關(guān)Java常見的誤解小結(jié)(來看一看)

    有關(guān)Java常見的誤解小結(jié)(來看一看)

    下面小編就為大家?guī)硪黄嘘P(guān)Java常見的誤解小結(jié)(來看一看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • 使用curl快速驗(yàn)證Maven依賴是否存在的方法

    使用curl快速驗(yàn)證Maven依賴是否存在的方法

    在Java開發(fā)中,Maven是我們管理項(xiàng)目依賴的利器,但你是否遇到過這種情況:添加了一個(gè)依賴坐標(biāo),卻總是下載失???或者想確認(rèn)某個(gè)新版本是否已經(jīng)發(fā)布到中央倉庫?所以本文給大家介紹了使用curl快速驗(yàn)證Maven依賴是否存在的方法,需要的朋友可以參考下
    2026-04-04
  • SpringBoot項(xiàng)目中使用redis緩存的方法步驟

    SpringBoot項(xiàng)目中使用redis緩存的方法步驟

    本篇文章主要介紹了SpringBoot項(xiàng)目中使用redis緩存的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot項(xiàng)目中rest接口定義與參數(shù)傳遞和接收方式

    SpringBoot項(xiàng)目中rest接口定義與參數(shù)傳遞和接收方式

    本文介紹了使用RestClient測試接口的兩種方式:form-data和content-type,form-data適用于所有請求方式,但URL長度有限制,content-type只適用于POST/DELETE/PUT請求,處理application/json格式參數(shù),對于參數(shù)較多的情況,建議使用JavaBean接收
    2026-04-04
  • Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡及Eureka與Ribbon的應(yīng)用小結(jié)

    Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡及Eureka與Ribbon的應(yīng)用小結(jié)

    這篇文章主要介紹了Java中的服務(wù)發(fā)現(xiàn)與負(fù)載均衡:Eureka與Ribbon的應(yīng)用,通過使用Eureka和Ribbon,我們可以在Java項(xiàng)目中實(shí)現(xiàn)高效的服務(wù)發(fā)現(xiàn)和負(fù)載均衡,需要的朋友可以參考下
    2024-08-08
  • IDEA?+?Maven環(huán)境下的SSM框架整合及搭建過程

    IDEA?+?Maven環(huán)境下的SSM框架整合及搭建過程

    這篇文章主要介紹了IDEA?+?Maven環(huán)境下的SSM框架整合及搭建過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Java流程控制break和continue

    Java流程控制break和continue

    這篇文章主要介紹了Java流程控制break和continue,下面文章圍繞break和continue的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2021-12-12
  • 在SpringBoot項(xiàng)目中正確實(shí)現(xiàn)順序消費(fèi)的方法示例

    在SpringBoot項(xiàng)目中正確實(shí)現(xiàn)順序消費(fèi)的方法示例

    在分布式系統(tǒng)與微服務(wù)架構(gòu)中,消息隊(duì)列已成為實(shí)現(xiàn)異步通信與服務(wù)解耦的核心基礎(chǔ)設(shè)施,然而,在許多業(yè)務(wù)場景中,消息的順序性是一個(gè)不可忽視的需求,那么,分區(qū)機(jī)制是如何工作的?在SpringBoot項(xiàng)目中如何正確實(shí)現(xiàn)順序消費(fèi)?本文將深入探討這些問題,需要的朋友可以參考下
    2026-04-04

最新評論

曲阜市| 临沂市| 股票| 民权县| 且末县| 沧州市| 顺平县| 高邑县| 德昌县| 焦作市| 五家渠市| 昆明市| 沅陵县| 千阳县| 金华市| 维西| 中山市| 双鸭山市| 当雄县| 西乌珠穆沁旗| 汤阴县| 尉氏县| 平利县| 响水县| 栖霞市| 六盘水市| 东台市| 贵南县| 武山县| 德江县| 南丹县| 武夷山市| 白山市| 万全县| 赤峰市| 淄博市| 武强县| 两当县| 蒙山县| 巴塘县| 金山区|