Spring Cloud 如何實現服務間的調用 4.2.3


   如果存在多個服務時,要怎么通過注冊中心來實現服務之間的調用呢?接下來將通過一個用戶和訂單之間的調用案例,來演示Eureka Server中服務之間的調用。

搭建訂單服務工程

  在父工程xcservice-springcloud中,創建Maven子模塊xcservice-eureka-order
  (1)在pom.xml中,添加spring-cloud-starter-eureka依賴,其代碼如下。
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

 

  (2)編寫配置文件。在配置文件中添加Eureka服務實例的端口號、服務端地址等信息,如文件4-8所示。
  文件4-8 application.yml
server:
  port: 7900 # 指定該Eureka實例的端口號

eureka:
  instance:
    prefer-ip-address: true  # 是否顯示主機的IP
    #instance-id: ${spring.cloud.client.ipAddress}:${server.port} #將Status中的顯示內容也以“IP:端口號”的形式顯示
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ # 指定Eureka服務端地址

spring:
  application:
    name: xcservice-eureka-order # 指定應用名稱

 

  (3)創建訂單實體類。
  文件4-9 Order.java
package com.xc.xcserviceeurekaorder.po;

public class Order {

    private String id;
    private Double price;
    private String receiverName;
    private String receiverAddress;
    private String receiverPhone;
    ...

}

 

  (4)創建訂單控制器類。
  文件4-10 OrderController.java
package com.xc.xcserviceeurekaorder.controller;

import com.xc.xcserviceeurekaorder.po.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    /**
     * 通過id查詢訂單
     */
    @GetMapping("/order/{id}")
    public String findOrderById(@PathVariable String id) {
        Order order = new Order();
        order.setId("123");
        order.setPrice(23.5);
        order.setReceiverAddress("beijing");
        order.setReceiverName("xiaoqiang");
        order.setReceiverPhone("13422343311");
        return order.toString();
    }

}

 

  (5)在引導類中添加@EnableEurekaClient注解。

 編寫用戶服務功能

  (1)在xcservice-eureka-user工程的引導類中,創建RestTemplate的Spring實例,其代碼如下:
@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

  在上述代碼中,RestTemplate是Spring提供的用於訪問Rest服務的客戶端實例,它提供了多種便捷訪問遠程Http服務的方法,能夠大大提高客戶端的編寫效率。


  (2)創建用戶控制器類
  文件4-11 UserController.java
package com.xc.xcserviceeurekauser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * http://localhost:8000/findOrdersByUser/1
     * 查找與用戶相關的訂單
     */
    @GetMapping("/findOrdersByUser/{id}")
    public String findOrdersByUser(@PathVariable String id) {
        // 假設用戶只有一個訂單,並且訂單id為123
        int oid = 123;
        return restTemplate.getForObject("http://localhost:7900/order/" + oid, String.class);
    }

}

  在上述代碼中,當用戶查詢訂單時,首先會通過用戶id查詢與用戶相關的所有訂單(由於這里主要是演示服務的調用,所以省略了查詢方法,並且自定義了一個oid為123的訂單,來模擬查詢出的結果)。然后通過restTemplate對象的getForObject()方法調用了訂單服務中的查詢訂單方法來查詢訂單id為123的訂單信息。


  3. 啟動服務應用,測試服務調用
  分別啟動服務注冊中心應用、訂單服務應用和用戶服務應用

 



  當通過瀏覽器訪問地址http://localhost:8000/findOrdersByUser/1(1表示用戶id)后,瀏覽器的顯示效果。
Order{id='123', price=23.5, receiverName='xiaoqiang', receiverAddress='beijing', receiverPhone='13422343311'}

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM