eureka實現服務之間的呼叫

liliblue發表於2019-12-18

存在多個服務時,通過註冊中心實現服務間的呼叫。

1.搭建訂單服務工程

在父工程microservice-springcloud中,建立Maven子模組microservice-eureka-order。

<1>在pom.xml中,新增spring-cloud-starter-eureka依賴

<2>編寫配置檔案

<3>建立訂單實體類

<4>建立訂單控制器類

2.編寫使用者服務功能

<1>在microservice-eureka-user工程的引導類中建立spring例項,用於訪問Rest服務的客戶端例項

<2>建立使用者控制類

3.啟動服務應用,測試服務呼叫

詳情:

1.搭建訂單服務工程

在父工程microservice-springcloud中,建立Maven子模組microservice-eureka-order。

<1>在pom.xml中,新增spring-cloud-starter-eureka依賴

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 </dependency>

<2>編寫配置檔案

server:
  port:7900  #指定該Eureka例項的埠號

eureka:
  instance:
    prefe-ip-address:true   #是否顯示主機的IP
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://registry:8761/eureka/ #指定Eureka服務端地址
spring:
    application:
        name:microservice-eureka-order  #模組的名稱

<3>建立訂單實體類。建立com.itheima.spring.po包,並在包中建立訂單實體類Order。

public class order {
    private String id;
    private Double price;
    private String receivePhone;
    private String receiveAddress;
    private String receiverName;
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getReceiverName() {
        return receiverName;
    }

    public void setReceiverName(String receiverName) {
        this.receiverName = receiverName;
    }

    public String getReceiveAddress() {
        return receiveAddress;
    }

    public void setReceiveAddress(String receiveAddress) {
        this.receiveAddress = receiveAddress;
    }

    public String getReceivePhone() {
        return receivePhone;
    }

    public void setReceivePhone(String receivePhone) {
        this.receivePhone = receivePhone;
    }

    @Override
    public String toString(){
        return "Order [id="+ id +",price="+price+","+"receiverName="+ receiverName +","+
                "receiveAddress="+ receiveAddress +",receivePhone="+ receivePhone +"]";
    }
}

<4>建立訂單控制器類

@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("xioaqiang");
        order.setreceiverPhone("123456879");
        return order.toString();
    }
}

<5>在引導類中加入@EnableEurekaClient

2.編寫使用者服務功能

<1>在microservice-eureka-user工程的引導類中建立spring例項,用於訪問Rest服務的客戶端例項

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

RestTemplate是spring提供的用於訪問Rest服務的客戶端例項,它提供了多種方便快捷訪問遠端Http服務的方法,能夠大大提高客戶端的編寫效率。

<2>建立使用者控制類,並在類中編寫查詢方法。

@RestController
public class UserController{
    @Autowired
    private RestTemplate restTemplate;
    /*
     *查詢與使用者相關的訂單
     */
    @Getmapping("findOrdersByUser/{id}")
    public String findOrdersByUser(@PathVariable String id){
        //假設使用者只有一個訂單,並且訂單id為132
        int oid = 123;
        return this.restTemplate
.getForObject("http://localhost:79000/order/"+oid,String.class);
    }
}

 

3.啟動服務應用,測試服務呼叫

通過瀏覽器訪問地址http:localhost:8000/findOrderByUser/1(1表示使用者id)。

 

 

 

相關文章