上一篇我們學習了服務的注冊與發現,本篇博客是在上一篇的基礎上學習服務的調用。上一博客主要創建了Eureka的服務端和一個Client,該Client包含了一個Controller用來提供對外服務供外部調用,可以作為生產者。
一、引入依賴
前面創建了EurekaClient的項目,在項目中引入了spring-cloud-starter-netflix-eureka-client用來注冊服務,是生產者,這里調用屬於消費者,同樣也需要引入spring-cloud-starter-netflix-eureka-client,這里還使用了openfeign來調用生產者提供的服務。具體pom.xml如下,主要引入spring-cloud-starter-netflix-eureka-client、spring-boot-starter-web、spring-cloud-starter-openfeign、spring-cloud-openfeign-core。

<?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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>EurekaConsumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>EurekaConsumer</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> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-openfeign-core --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-openfeign-core</artifactId> <version>2.1.2.RELEASE</version> </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> <packaging>war</packaging> </project>
二、在main類中引入@EnableDiscoveryClient、@EnableFeignClients注解

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class EurekaConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaConsumerApplication.class, args); } }
@EnableDiscoveryClient :啟用服務注冊與發現
@EnableFeignClients:啟用feign進行遠程調用
Feign是一個聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡單, 它的使用方法是定義一個接口,然后在上面添加注解,同時也支持JAX-RS標准的注解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標准注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。
三、配置文件
spring.application.name=spring-cloud-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/
四、feign調用實現
package com.example.demo; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name= "spring-cloud-producer") public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name); }
name:遠程服務名,及spring.application.name配置的名稱,此類中的方法和遠程服務中contoller中的方法名和參數需保持一致。
五、web層調用遠程服務
這里使用@Autowired將遠程訪問類注入到Controller中用來調用服務。
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class ConsumerController { @Autowired HelloRemote HelloRemote; @RequestMapping("/hello") public String index(@RequestParam String name) { return HelloRemote.hello(name); } }
六、測試
分別啟動Server、Client、Consumer,依次輸入http://localhost:8088/、http://localhost:9000/hello?name=cuiyw、http://localhost:9001/hello?name=cuiyw。端口8088是Eureka Server端,9000是Eureka Client,作為生產者,端口9001也是Eureka Client,作為消費者,在端口9001中消費者調用生產者9000提供的服務。
七、負載均衡
修改EurekaClient項目中的Controller和配置文件。在Controller中的index方法增加了修改了return的值,用來區分生產者。在application.properties中修改了端口號。
package com.example.demo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is first messge 2"; } }
spring.application.name=spring-cloud-producer server.port=9002 eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/
啟動該Client,這時在Eureka Server中可以看到兩個生產者。
在瀏覽器中數次刷新url:http://localhost:9001/hello?name=cuiyw,可以看到不同的響應結果。
八、小結
本篇主要學習了下服務的調用以及簡單的使用openfeign進行的負載均衡,后面會參考純潔的微笑的博文學習熔斷器Hystrix。
本篇博客參考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html
九、號外
本人的新書已出版上架,書名Spring快速入門,天貓、京東也都能搜得到,這里也算是王婆賣瓜自賣自誇一下,大家如果感興趣可以了解一下,個人感覺對初學者來說還是值得一讀的。