1.首先我们将生产者的接口暴露出来,一般是作为独立的对外接口,写在一个项目里。将对外的接口打包。

当然还有我们的pom文件依赖

<?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.qdsg.ylt</groupId>
<artifactId>ylt_api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<description>所有对外api接口由该项目负责</description>
<modules>
<module>vedio_api</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>



<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
  //注意该依赖一定要放在spring-cloud-starter-openfeign依赖的上边,不然消费方可能会出现一些问题
   <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>




</dependencies>

</project>

 

2.还有就是消费者,如何调用接口,

我们将打好的jar包引入到消费者的项目里边去,通过pom引入,(接口打包的时候会有相应的坐标)。

消费者的包结构

这个是消费者的启动类
@SpringBootApplication(scanBasePackages = {"com.qdsg","com.example"})
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.qdsg"})
public class FeignClientConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(FeignClientConsumerApplication.class, args);
}
}
看到启动类上扫面了两个包,一个是该项目的扫描,一个是引入jar的包扫描,你也可以简单来写(@SpringBootApplication(scanBasePackages = {"com"}))
还有就是@FeignClient注解的扫描,扫描的是对外接口所在的包。如下:

消费者调用提供的jar包(使用注入的方式)

 

这样就可以调用我们对外提供的接口了,但是要保证你的项目启动并且注册到eureka上。

如何注册呢,配置如下

eureka:
client:
service-url:
defaultZone: #这里是服务注册地址
register-with-eureka: true
fetch-registry: false
instance:
instance-id: 192.168.1.88
prefer-ip-address: true
lease-renewal-interval-in-seconds: 1
lease-expiration-duration-in-seconds: 5

 

下面就是对外提供的接口

对外的接口

 

 

异常回调

当然还有用到的实体类都要暴露出来

 消费者配置

##配置项目名字
eureka:
client:
service-url:
defaultZone: http://119.3.18.49:8071/eurekaservers/eureka/
fetch-registry: true
register-with-eureka: false
# #feign 对hytrix的支持
feign:
hystrix:
enabled: true
ribbon:
ReadTimeout: 15000
ConnectTimeout: 60000

hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 20000

 加上最后一个配置熔断才会有作用