上一篇文章主要介紹瞭如何搭建一個簡單的springcloud框架。不過,咱們搭建好框架就是爲了消費它使用它,那麼這篇文章就來看看如何去消費使用咱們以前搭建起來的服務吧!
首先本文是基於上一篇文章進行的。java
Feign是Netflix開發的聲明式、模板化的HTTP客戶端, Feign能夠幫助咱們更快捷、優雅地調用HTTP API。web
繼續用上一節的工程, 啓動eureka-server,端口爲8080; 啓動eureka-client兩次,端口分別爲8081 、8082.spring
咱們首先要新建一個spring-boot工程,取名爲serice-feign。apache
此次咱們要選擇三個依賴:瀏覽器
對應的pom.xml文件內容爲:app
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zhouxiaoxi</groupId> <artifactId>eureka-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-feign</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
接下來咱們就須要配置application.yml文件了:框架
server: #端口號 port: 8083 spring: application: #端口名稱 name: eureka-feign eureka: client: serviceUrl: #服務註冊地址 defaultZone: http://localhost:8080/eureka/
在程序的啓動類EurekaFeignApplication,加上@EnableFeignClients註解開啓Feign的功能:maven
package com.zhouxiaoxi.eurekafeign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class EurekaFeignApplication { public static void main(String[] args) { SpringApplication.run(EurekaFeignApplication.class, args); } }
如今咱們須要定義一個feign接口,經過@FeignClient(「服務名」)註解來指定調用哪一個服務。
好比在下面的代碼中調用了eureka-client服務的「/hello」接口,代碼以下:spring-boot
package com.zhouxiaoxi.eurekafeign.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "eureka-client") public interface SchedualServiceHello { @RequestMapping(value = "/hello",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name); }
在Web層的controller層,對外暴露一個」/hello」的API接口,經過上面定義的Feign客戶端SchedualServiceHello 來消費服務。代碼以下:ui
package com.zhouxiaoxi.eurekafeign.controller; import com.zhouxiaoxi.eurekafeign.service.SchedualServiceHello; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired SchedualServiceHello schedualServiceHello; @GetMapping(value = "/hello") public String sayHi(@RequestParam String name) { return schedualServiceHello.sayHiFromClientOne( name ); } }
啓動程序,屢次訪問http://localhost:8083/hello?name=feign,瀏覽器顯示內容爲:
hello feign ,i am from port:8081
hello feign ,i am from port:8082
至此咱們這個服務消費的框架就搭建完畢了。。。