java spring cloud 版 b2b2c 社交电商-服务消费者(Feign)
社交電商平臺(tái)源碼請(qǐng)加企鵝求求:一零三八七七四六二六。Feign是一個(gè)聲明式的偽Http客戶(hù)端,它使得寫(xiě)Http客戶(hù)端變得更簡(jiǎn)單。使用Feign,只需要?jiǎng)?chuàng)建一個(gè)接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的編碼器和解碼器。Feign默認(rèn)集成了Ribbon,并和Eureka結(jié)合,默認(rèn)實(shí)現(xiàn)了負(fù)載均衡的效果。
簡(jiǎn)而言之:
Feign 采用的是基于接口的注解
Feign 整合了ribbon,具有負(fù)載均衡的能力
整合了Hystrix,具有熔斷的能力
二、創(chuàng)建一個(gè)feign的服務(wù)
新建一個(gè)spring-boot工程,取名為serice-feign,在它的pom文件引入Feign的起步依賴(lài)spring-cloud-starter-feign、Eureka的起步依賴(lài)spring-cloud-starter-netflix-eureka-client、Web的起步依賴(lài)spring-boot-starter-web,代碼如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.forezp</groupId><artifactId>service-feign</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-feign</name><description>Demo project for Spring Boot</description><parent><groupId>com.forezp</groupId><artifactId>sc-f-chapter3</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies></project> 復(fù)制代碼在工程的配置文件application.yml文件,指定程序名為service-feign,端口號(hào)為8765,服務(wù)注冊(cè)地址為http://localhost:8761/eureka/ ,代碼如下:
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/ server:port: 8765 spring:application:name: service-feign 復(fù)制代碼在程序的啟動(dòng)類(lèi)ServiceFeignApplication ,加上@EnableFeignClients注解開(kāi)啟Feign的功能:
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class ServiceFeignApplication {public static void main(String[] args) {SpringApplication.run( ServiceFeignApplication.class, args );} } 復(fù)制代碼定義一個(gè)feign接口,通過(guò)@ FeignClient(“服務(wù)名”),來(lái)指定調(diào)用哪個(gè)服務(wù)。比如在代碼中調(diào)用了service-hi服務(wù)的“/hi”接口,代碼如下:
@FeignClient(value = "service-hi") public interface SchedualServiceHi {@RequestMapping(value = "/hi",method = RequestMethod.GET)String sayHiFromClientOne(@RequestParam(value = "name") String name); } 復(fù)制代碼在Web層的controller層,對(duì)外暴露一個(gè)"/hi"的API接口,通過(guò)上面定義的Feign客戶(hù)端SchedualServiceHi 來(lái)消費(fèi)服務(wù)。代碼如下:
@RestController public class HiController {//編譯器報(bào)錯(cuò),無(wú)視。 因?yàn)檫@個(gè)Bean是在程序啟動(dòng)的時(shí)候注入的,編譯器感知不到,所以報(bào)錯(cuò)。@AutowiredSchedualServiceHi schedualServiceHi;@GetMapping(value = "/hi")public String sayHi(@RequestParam String name) {return schedualServiceHi.sayHiFromClientOne( name );} } 復(fù)制代碼啟動(dòng)程序,多次訪問(wèn)http://localhost:8765/hi?name=forezp,瀏覽器交替顯示:
hi forezp,i am from port:8762hi forezp,i am from port:8763 復(fù)制代碼轉(zhuǎn)載于:https://juejin.im/post/5cd4dae4f265da035f6ff2bb
總結(jié)
以上是生活随笔為你收集整理的java spring cloud 版 b2b2c 社交电商-服务消费者(Feign)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到尘土飞扬是吉兆吗
- 下一篇: 类的转换函数调用的优先级与是否用cons