開啟一個spring Cloud 工程 -第三步 -建立服務消費者

程式芒發表於2020-10-18

其實服務消費者,服務提供者只是業務層面上的定義,他們本身的身份是可以有兩層的,例如 服務A、B、C , A呼叫B時,A作為消費者,B作為提供者 ,C呼叫A時,C作為消費者,A作為提供者,這裡的服務A就有兩重身份。所以提供者、消費者程式碼層面上,其實沒有本質區別。只有業務上的區分而已。

建立服務消費者(與建立服務消費者步驟相同)

第一步:從上一步建立的父工程裡 建立一個新的module

建立module ,並且取名為consumer
在這裡插入圖片描述

在這裡插入圖片描述在新建的consumer工程的pom.xml中新增提供者依賴:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

第二步:建立yml檔案,新增提供者配置資訊

在consumer的resource資料夾中建立application.yml檔案,並新增以下資訊:

server:
  port: 8020
spring:
  application:
      name: consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

配置資訊說明:

  • server-port: 服務提供者的埠號
  • spring-application-name: 當前服務註冊在EurekaServer的名稱
  • eureka-client-service-url-defaultZone: 註冊中心的地址
  • eureka-instance-prefer-ip-address: 是否將當前服務的ip註冊到Eureka Server中

第三步,配置啟動類,以及配置呼叫資訊,啟動服務提供者

在consumer 的java 資料夾中,建立一個名字以Application 結尾的java 類,b並且把RestTemplate 例項注入到 IoC容器中, 這是能實現模組間呼叫的關鍵 新增如下資訊:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args){
        SpringApplication.run(ConsumerApplication.class);
    }

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

在java 資料夾中,新建controler 檔案,建立ConsumerHandler 類,使用RestTemplate 類實現模組間的呼叫
在這裡插入圖片描述

ConsumerHandler 程式碼如下:

package com.southwind.controller;


import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

@RestController
@RequestMapping("/consumer")
public class ConsumerHandler {
    @Autowired
    private RestTemplate restTemplate;

    //在這裡通過RestTemplate的方法呼叫 eruekaclient 模組的方法
    //第一種方法,使用getForEntity,不是返回 ResponseEntity<T>型別,所以需要通過呼叫.getBody()方法
    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody();
    }
    //第二種方法,使用getForObject ,因為此方法返回值就是泛型 ,所以直接返回即可
    @GetMapping("/findAll2")
    public Collection<Student> findAll2(){
        return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
    }
    @GetMapping("/findById/{id}")
    public Student  findById(@PathVariable("id") long id){
        return restTemplate.getForEntity("http://localhost:8010/student//findById/{id}",Student.class,id).getBody();
    }

    @GetMapping("/findById2/{id}")
    public Student findById2(@PathVariable("id") long id){
        return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);
    }

    @PostMapping("/save")
    public void save(@RequestBody Student student){
        restTemplate.postForEntity("http://localhost:8010/student/save",student,null).getBody();
    }

    @PostMapping("/save2")
    public void save2(@RequestBody Student student){
        restTemplate.postForObject("http://localhost:8010/student/save",student,void.class);
    }
    @PutMapping("/update")
    public void update(@RequestBody Student student){
        restTemplate.put("http://localhost:8010/student/update",student);

    }
    @DeleteMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
    }
}

最後,先啟動註冊中心eurekaserver ,然後再啟動eurekaclient ,再啟動 consumer,在網頁上輸入 http://localhost:8761
在這裡插入圖片描述

即可看到服務消費者已經註冊進了註冊中心
在這裡插入圖片描述
至此,服務消費者已經實現。

這裡說明一下,服務消費者 與上文 RestTemplate 實現模組間的呼叫的程式碼是一樣的,唯一的區別是,消費者是把自己註冊進了註冊中心,也就是多了一個yml配置。實際上,實現消費者呼叫提供者,依舊是通過RestTemplate 實現的。 這是嵌在spring boot依賴裡面的方法,也是實現模組間呼叫的關鍵,這就是為啥spring Cloud 不能脫離於springboot 框架的原因之一

這裡測試一下:
查詢:
在這裡插入圖片描述
測試成功

相關文章