Spring Cloud快速使用教程(一)


在研究spring cloud,下面是快速搭建方法

我使用的是IDEA 中文漢化版,大家可自行漢化
1、先創建一個工程

 

2、拉下來創建注冊組件

同樣的選擇springboot工程

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

 

配置eureka注冊中心配置文件 application.yml (注意縮進)

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

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

http://localhost:8081

 

 3、創建二個微服務 serviceA,serviceB

創建springboot模塊

配置微服務的入口文件 @EnableEurekaClient   (service-a和service-b的都要加)

 

 配置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}

開啟多個微服務。單個一個個開就很麻煩,我們使用RunDashboard就會方便很多

在工程目錄下找.idea文件夾下的workspace.xml,在其中增加如下組件

  <component name="RunDashboard">
    <option name="configurationTypes">
      <set>
        <option value="SpringBootApplicationConfigurationType" />
      </set>
    </option>
  </component>

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

出現端口號表示啟動成功

  編寫測試接口

package com.example.servicea.controller;

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

@RestController
public class IndexA {
    @RequestMapping("testA")
    public String test(){
        return "Hello A";
    }
}

重啟服務

 訪問下面地址即可訪問到

http://localhost:8082/testA

Hello A

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

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

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

ServiceAFeignClient:

package com.example.seaverb.controller;

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

// 填入注冊中心中的應用名, 也就是要調用的微服務的應用名
// 在eureka頁面中可以找到
@FeignClient("SERVICE-OBJCAT-A")
public interface ServiceAFeignClient {
   //路由名"testA"對應控制器IndexA的方法的路由名 例:此外對應是的IndexA @requestMapping("testA") @RequestMapping("testA") public String IndexA(); @RequestMapping("testA2") public String IndexA2(@RequestParam("id") String id); @PostMapping("testA3") public String IndexA3(@RequestParam("title") String title,@RequestParam("id") Integer id); }

在Index.java 我增加了一個GET方式及POST方式接收微服務器之間傳參方法可供參考

package com.example.seavera.controller;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.*;

@RestController
// 添加注解聲明是注冊中心客戶端
@EnableEurekaClient
// 實現不同子服務調用
@EnableFeignClients
public class IndexA {
    @RequestMapping("testA")
    public String test(){
        return "Hello A";
    }

    /**
     * 微服務GET接收傳參實例
     * @param id
     * @return
     */
    @RequestMapping("testA2")
    public String test2(@RequestParam("id") String id){
        return "Hello A2 "+ id;
    }
    /**
     * 微服務POST接收傳參實例
     * @param title
     * @param id
     * @return
     */
    @PostMapping(value = "testA3")
    public String test3(@RequestParam("title") String title,@RequestParam("id") Integer id){
        return "Hello A3 "+ title;
    }
}

在服務b中添加控制器

 

package com.example.seaverb.controller;

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.*;

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

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

    /**
     * 微服務之間GET傳參數 http://localhost:8083/call2?id=11
     * 雙方 @RequestParam("id") 必須指定相同的value值。只要不對應都會報錯。
     * @param id
     * @return
     */
    @RequestMapping("call2")
    public String call2(@RequestParam("id") String id){
        String test = "This is B";
        String result = serviceAFeignClient.IndexA2(id);
        return "b to a 訪問結果 ---" + result;
    }

    /**
     * 微服務之間POST傳參數
     * @param title
     * @param id
     * @return
     */
    @PostMapping("call3")
    public String call3(@RequestParam("title") String title,@RequestParam("id") Integer id){
        String result = serviceAFeignClient.IndexA3(title,id);
        return "b to a 訪問結果 ---" + result;
    }
}

解決@Autowired實例報錯

重新運行服務b 在瀏覽器上訪問試試吧

http://localhost:8083/call

可以看到             b to a 訪問結果 ---Hello A


使用GET傳參訪問
http://localhost:8083/call2?id=888

可以看到             b to a 訪問結果 ---Hello A2 888

使用POST傳參

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

 

spring cloud(二)簡單快速的實現負載均衡的功能 


免責聲明!

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



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