1 5 服務消費者

2021-08-16 03:30:51 字數 1711 閱讀 9464

每乙個註冊在微服務中的服務,既是服務提供者也是服務消費者。

我們建立乙個eureka-consumer專案作為服務消費者來消費eureka-client。

建立專案啟動類:

import org.springframework.cloud.client.discovery.enablediscoveryclient;

import org.springframework.context.annotation.bean;

import org.springframework.web.client.resttemplate;

@enablediscoveryclient

@bean

public resttemplate resttemplate()

public static void main(string args)

}

resttemplate:用來發起rest請求
建立消費者介面類:

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.cloud.client.serviceinstance;

import org.springframework.cloud.client.loadbalancer.loadbalancerclient;

import org.springframework.web.bind.annotation.restcontroller;

import org.springframework.web.client.resttemplate;

@restcontroller

public class dccontroller

}

注入了loadbalancerclientresttemplate,並在/consumer介面的實現中,先通過loadbalancerclientchoose函式來負載均衡的選出乙個eureka-client的服務例項,這個服務例項的基本資訊儲存在serviceinstance中,然後通過這些物件中的資訊拼接出訪問/dc介面的詳細位址,最後再利用resttemplate物件實現對服務提供者介面的呼叫。

分別啟動註冊中心、服務提供者、服務消費者專案,然後訪問

服務消費者RestTemplate Ribbon

目錄 簡介 pom.xml新增依賴 通過 loadbalanced註解表明這個restremplate開啟負載均衡的功能 這樣 resttemplate訪問介面就可以實現負載均衡功能了。spring cloud有兩種服務呼叫方式,一種是ribbon resttemplate,另一種是feign ri...

服務消費者Feign

feign是乙個宣告式的偽http客戶端,它使得寫http客戶端變得更簡單。使用feign,只需要建立乙個介面並註解。它具有可插拔的註解特性,可使用feign 註解和jax rs註解。feign支援可插拔的編碼器和解碼器。feign預設整合了ribbon,並和eureka結合,預設實現了負載均衡的效...

Rest,服務消費者

建立消費者,一般指定埠號為80 將resttemplate註冊到spring ioc容器中 package cn.lzm.springcloud.config import org.springframework.context.annotation.configuration import org...