建立消費者,一般指定埠號為80
將resttemplate註冊到spring ioc容器中
package cn.lzm.springcloud.config;
import org.springframework.context.annotation.configuration;
import org.springframework.web.client.resttemplate;
@configuration
public class webconfig
}
編寫控制器,消費者不應該有服務層
package cn.lzm.springcloud.controller;
import cn.lzm.springcloud.pojo.person;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.responsebody;
import org.springframework.web.client.resttemplate;
import j**a.util.list;
/** * 理解:消費者,不應該有service層
* resttemplate... 直接呼叫註冊到spring中
* (string url, classresponsetype, mapurivariables)路徑 響應類位元組碼 map請求引數
*/@controller
public class personcontroller
@responsebody
public person getpersonbyid(@pathvariable("id") int id)",person.class,id);
}@responsebody
public listgetpersonall()
}
測試一 1 5 服務消費者
每乙個註冊在微服務中的服務,既是服務提供者也是服務消費者。我們建立乙個eureka consumer專案作為服務消費者來消費eureka client。建立專案啟動類 import org.springframework.cloud.client.discovery.enablediscoveryc...
服務消費者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結合,預設實現了負載均衡的效...