springcloud學習之路: (一) 最簡單的搭建springcloud的方法


參考資料: [JavaEE] 五分鍾搭建SpringCloud環境, 進入微服務時代

感謝上篇博文大佬帶領走進springcloud世界, 本博文主要目的為記錄自己學習springcloud的點點滴滴, 給自己的知識進行整理, 如果能幫助更多的小伙幫那就更好了.

ps: 本文主要作為備忘及快速搭建springcloud之用, 只記錄主要步驟. 原文講述更加細致, 想深入學習的同學推薦點擊上方連接學習.

操作系統: Windows 10

IDE: IntelliJ IDEA 2018.3.6

JAVA: JDK 1.8.X

Meave: 3.6.0

SpringBoot: 2.1.7

1. 創建一個工程

創建springboot工程

 2. 創建Eureka注冊中心

 依然選擇springboot工程

 選擇eureka服務器

 導包, 開啟服務管理器

 為eureka注冊中心添加注解開啟服務

 配置eureka注冊中心配置文件

server:
  # 配置服務端口
  port: 8081
eureka:
  client:
    service-url:
      # 配置eureka服務器地址
      defaultZone: http://127.0.0.1:8081/eureka
    #是否需要將自己注冊到注冊中心(注冊中心集群需要設置為true)
    register-with-eureka: false
    #是否需要搜索服務信息 因為自己是注冊中心所以為false
    fetch-registry: false

注意縮進, 因為yml使用縮進來區分不同字段的.

運行ServiceEurekaApplication文件啟動項目, 訪問注冊中心

http://localhost:8081

 3. 創建兩個微服務service-a,service-b

創建一個springboot模塊

 起個名字

 

 選擇web

 選擇客戶模塊

 路由設置

 配置微服務的入口文件 @EnableEurekaClient

 配置application.yml

service-a

server:
  # 服務端口號
  port: 8082
spring:
  application:
    # 服務名稱 - 服務之間使用名稱進行通訊
    name: service-objcat-a
eureka:
  client:
    service-url:
      # 填寫注冊中心服務器地址
      defaultZone: http://localhost:8081/eureka
    # 是否需要將自己注冊到注冊中心
    register-with-eureka: true
    # 是否需要搜索服務信息
    fetch-registry: true
  instance:
    # 使用ip地址注冊到注冊中心
    prefer-ip-address: true
    # 注冊中心列表中顯示的狀態參數
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

service-b

server:
  # 服務端口號
  port: 8083
spring:
  application:
    # 服務名稱 - 服務之間使用名稱進行通訊
    name: service-objcat-b
eureka:
  client:
    service-url:
      # 填寫注冊中心服務器地址
      defaultZone: http://localhost:8081/eureka
    # 是否需要將自己注冊到注冊中心
    register-with-eureka: true
    # 是否需要搜索服務信息
    fetch-registry: true
  instance:
    # 使用ip地址注冊到注冊中心
    prefer-ip-address: true
    # 注冊中心列表中顯示的狀態參數
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

運行微服務

 分別運行注冊中心及微服務模塊

 出現端口號表示啟動成功

 編寫測試接口

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestAController {

    @RequestMapping("testA")
    public String TestAController(){
        return "Hello,SpringCloud for TestA";
    }
}

重啟服務

 

 

 訪問下面地址

http://localhost:8082/testA

訪問成功

 

 

使用服務b調用服務a的接口
這時我們就需要用到eurka(注冊中心)feign客戶端了
首先我們在service-b中創建interface

在微服務b中, 創建一個ServiceAFeignClient接口

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

// 填入注冊中心中的應用名, 也就是要調用的微服務的應用名
// 在eureka頁面中可以找到
@FeignClient("SERVICE-OBJCAT-A")
public interface ServiceAFeignClient {
    @RequestMapping("testA")
    public String TestAController();
}

應用名可以在eureka中找到
http://localhost:8081

在服務b中添加個控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// 添加注解聲明是注冊中心客戶端
@EnableEurekaClient
// 實現不同子服務調用
@EnableFeignClients
public class TestBController {

    @Autowired
    private ServiceAFeignClient serviceAFeignClient;

    @RequestMapping("call")
    public String call(){
        String result = serviceAFeignClient.TestAController();
        return "b to a 訪問結果 ---" + result;
    }
}

解決@Autowired實例報錯

 

 

重新運行服務b 在網站上訪問試試吧

http://localhost:8083/call

PS: 在springcloud中一個子服務調用另一個子服務默認超時時間是1s, 也就是說要是被調用的子服務返回超過一秒就會出現錯誤, 針對此問題需要修改調用服務的yml文件. 

舉例: 在本案例中, service-a是被調用者, service-b是調用者, 則在service-b的yml文件中加入

 

ribbon:
  #建立連接超時時間
  ReadTimeout: 5000
  #讀取資源超時間
  ConnectTimeout: 5000

 

注意首行縮進, service-b完整配置如下

server:
  # 服務端口號
  port: 8083
spring:
  application:
    # 服務名稱 - 服務之間使用名稱進行通訊
    name: service-objcat-b
eureka:
  client:
    service-url:
      # 填寫注冊中心服務器地址
      defaultZone: http://localhost:8081/eureka
    # 是否需要將自己注冊到注冊中心
    register-with-eureka: true
    # 是否需要搜索服務信息
    fetch-registry: true
  instance:
    # 使用ip地址注冊到注冊中心
    prefer-ip-address: true
    # 注冊中心列表中顯示的狀態參數
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
ribbon:
  #建立連接超時時間
  ReadTimeout: 5000
  #讀取資源超時間
  ConnectTimeout: 5000

 文中案例下載


免責聲明!

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



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