Feign是一种声明式、模板化的HTTP客户端。这使得Web服务客户端的写入更加方便 要使用Feign建立一个界面并对其进行注释。它具备可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增长了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。这段话来源于官方文档,说白了就是经过Feign来调用Rest接口,而无需使用其余HTTP访问组件,而且同时还提供了负载均衡、编解码等功能,使用起来很方便。html
首先在A服务器上启动Eureka服务,而后在B、C两台服务器上分别启动ms-demo-provider服务,这里也能够部署在一台服务器上采用不一样的端口。访问Eureka界面查看服务注册状态,以后在本地新建客户端调用工程进行测试。此时A为注册中心,B、C分别为服务提供者(提供相同的接口),本地工程为服务消费者。java
在Idea中建立maven工程,ms-eurekaclient-demo工程代码结构以下: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>
<groupId>com.cloud.microservice</groupId>
<artifactId>ms-eurekaclient-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ms-eurekaclient-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- 加入断路器依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</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>${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>复制代码
spring.application.name=ms-eurekaclient-demo
server.port=9800
# 注册中心地址
eureka.client.serviceUrl.defaultZone=http://xx.xx.xx.xx:9000/eureka/
# Indicates whether this client should fetch eureka registry information from eureka server
# 客户端是否要从eureka server获取注册信息,默认为true
eureka.client.fetchRegistry=true
# Indicates how often(in seconds) to fetch the registry information from the eureka server
# 从eureka server获取注册信息的频率,默认为30秒,缩短配置时间能够缓解服务上线时间过长的问题
eureka.client.registryFetchIntervalSeconds=10复制代码
package com.cloud.microservice.eurekaclientdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignDemoApplication {
public static void main(String[] args) {
SpringApplication.run(FeignDemoApplication.class, args);
}
}复制代码
package com.cloud.microservice.eurekaclientdemo.FeignClient;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("ms-demo-provider")
public interface IUserFeignServiceClient {
//Feign定义服务提供者接口
@RequestMapping(value = "/demo/user/1.0/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
String findAll();
}复制代码
IUserService接口类以下:spring
package com.cloud.microservice.eurekaclientdemo.FeignClient;
public interface IUserService {
String findAll();
}复制代码
UserServiceImp实现类以下:apache
package com.cloud.microservice.eurekaclientdemo.FeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImp implements IUserService{
@Autowired
private IUserFeignServiceClient userFeignServiceClient;
public String findAll() {
return "Feign: " + userFeignServiceClient.findAll();
}
}复制代码
package com.cloud.microservice.eurekaclientdemo.FeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignDemoController {
@Autowired
private IUserService userService;
@RequestMapping(value = "/feigndemo/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
public String feignDemo() {
return userService.findAll();
}
}复制代码
启动工程,而后刷新Eureka界面,能够看到Feign已经注册到服务中心json
Honghu代码结构图:bash
更多详细源码参考来源服务器
Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六
app