Feign服务消费者
Posted cppdy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Feign服务消费者相关的知识,希望对你有一定的参考价值。
Feign的优点:面向接口,完全不用管实现,传入规定格式的数据就可以了
搭建消费者项目(FeignDemo)
1、创建pom.xml
<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.cppdy</groupId> <artifactId>FeignDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.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-web</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>Dalston.RC1</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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2、创建application.yml配置文件
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9000/eureka/
server:
port: 9004
spring:
application:
name: cppdy-feign
3、创建HelloService接口
package com.cppdy.service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "cppdy-hello") public interface HelloService { @RequestMapping("hello") String hello(@RequestParam("name") String name); }
4、创建HelloController类
package com.cppdy.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.cppdy.service.HelloService; @RestController public class HelloController { @Autowired private HelloService helloService; @RequestMapping("hello") public String hello(String name) { return helloService.hello(name); } }
5、创建启动类(FeignApp)
package com.cppdy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication //开启服务发现 @EnableDiscoveryClient //开启feign @EnableFeignClients public class FeignApp { public static void main(String[] args) { SpringApplication.run(FeignApp.class, args); } }
6、先启动EurekaDemo(注册中心项目),再启动2个ProduceDemo(服务提供者项目)端口号分别设置为9001、9002,再启动FeignDemo(消费者项目)端口号设置为9004,一直访问http://localhost:9004/hello,会自动轮流调用9001和9002的hello方法(负载均衡)
以上是关于Feign服务消费者的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud服务提供者 Eureka + 服务消费者 Feign
企业分布式微服务云SpringCloud SpringBoot mybatis 服务消费者(Feign)
SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)