上一篇咱們學習了服務的註冊與發現,本篇博客是在上一篇的基礎上學習服務的調用。上一博客主要建立了Eureka的服務端和一個Client,該Client包含了一個Controller用來提供對外服務供外部調用,能夠做爲生產者。html
1、引入依賴java
前面建立了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。web
<?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>
2、在main類中引入@EnableDiscoveryClient、@EnableFeignClients註解spring
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 :啓用服務註冊與發現apache
@EnableFeignClients:啓用feign進行遠程調用
Feign是一個聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡單, 它的使用方法是定義一個接口,而後在上面添加註解,同時也支持JAX-RS標準的註解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標準註解和HttpMessageConverters。Feign能夠與Eureka和Ribbon組合使用以支持負載均衡。瀏覽器
3、配置文件app
spring.application.name=spring-cloud-consumer server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/
4、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中的方法名和參數需保持一致。maven
5、web層調用遠程服務ide
這裏使用@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); } }
6、測試
分別啓動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提供的服務。
7、負載均衡
修改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,能夠看到不一樣的響應結果。
8、小結
本篇主要學習了下服務的調用以及簡單的使用openfeign進行的負載均衡,後面會參考純潔的微笑的博文學習熔斷器Hystrix。
本篇博客參考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html
9、號外
本人的新書已出版上架,書名Spring快速入門,天貓、京東也都能搜獲得,這裏也算是王婆賣瓜自賣自詡一下,你們若是感興趣能夠了解一下,我的感受對初學者來講仍是值得一讀的。