SpringCloud(六)调用Eureka中的服务之Feign
文章内索引
[显示]
Feign
Feign采用接口注解的形式,简化了Http客户端,使客户端可以基于接口进行开发。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
Simple
在http://start.spring.io/中,输入eureka discovery与feign快速生成eureka服务调用项目。
添加@EnableDiscoveryClient注解与@EnableFeignClients,启动springboot项目(此项目与其他Restful风格的springboot项目一致,只需要添加@EnableDiscoveryClien即可注册至服务中心)
声明@EnableFeignClients,启用feign客户端。
pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?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> <groupId>com.hanson</groupId> <artifactId>Eureka-Feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Eureka-Feign</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</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> |
conf
1 2 3 |
spring.application.name=eureka-consumer-feign server.port=8081 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ |
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.hanson.feign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * 模拟客户请求,接口写在了本类中。应该单独封装为一个单独接口 * @author THINK * */ @RestController public class ConsumerController { @Autowired HansonService service;//注入定义的接口 @RequestMapping("/") public String helloWorld(){ /** * 使用@FeignClient("hanson-service") 指定要访问的service为hanson-service,由于get()对应的mapping为@RequestMapping(value = "/", method = GET) 那么该方法实际调用url为 * http://hanson-service/,因此在执行此接口时,实际是访问了http://hanson-service/ */ String string = service.get(); System.out.println(string); return string; } /** * feigen接口 */ @FeignClient(value = "hanson-service")//声明此接口访问hanson-service-->与服务提供者的配置的spring.application.name一致 interface HansonService{ @RequestMapping(value = "/",method = RequestMethod.GET) String get(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.hanson.feign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * 模拟客户请求,接口写在了本类中。应该单独封装为一个单独接口 * @author THINK * */ @RestController public class ConsumerController { @Autowired HansonService service;//注入定义的接口 @RequestMapping("/") public String helloWorld(){ /** * 使用@FeignClient("hanson-service") 指定要访问的service为hanson-service,由于get()对应的mapping为@RequestMapping(value = "/", method = GET) 那么该方法实际调用url为 * http://hanson-service/,因此在执行此接口时,实际是访问了http://hanson-service/ */ String string = service.get(); System.out.println(string); return string; } /** * feigen接口 */ @FeignClient(value = "hanson-service")//声明此接口访问hanson-service-->与服务提供者的配置的spring.application.name一致 interface HansonService{ @RequestMapping(value = "/",method = RequestMethod.GET) String get(); } } |
启动服务
此时eureka服务中心上注册了,俩个服务8001、8003、以及ribbon调用端8080,feign调用端8081.
调用http://localhost:8081/ 可以看到feign轮询调用了hanson-service服务,结果与之前ribbon+restTemplate相一致。
1 2 3 4 5 |
你好,这是你要的XX商品,由8003为您提供服务。 你好,这是你要的XX商品,由8001为您提供服务。 你好,这是你要的XX商品,由8002为您提供服务。 你好,这是你要的XX商品,由8003为您提供服务。 你好,这是你要的XX商品,由8001为您提供服务。 |
©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!
发表评论