您的位置:首页 > 编程语言 > Java开发

【图文经典版】声明式调用服务SpringCloud之Feign实例讲解

2017-12-27 00:00 1591 查看
注意:在上两篇 创建SpringCloud服务,并向注册中心注册自己 SpringCloud创建Eureka注册中心

的基础上,我们建立hello-consumer服务

第一部分:创建SpringBoot工程

1、新建项目



2、选择创模板

然后,在弹出框中选择Spring Initializr,并使用默认的模板‘http://start.spring.io’。点击下一步(Next)



3、填写公司信息







4、勾选依赖



5、选择项目储存路径



6、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.learning.pocher</groupId>
<artifactId>hello-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>hello-consumer</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.RELEASE</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-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>


第二部分:修改和添加文件

1、修改application.properties文件

spring.application.name=hello-consumer
spring.port=2221
eureka.client.service-url.defaultZone=http://localhost:1110/eureka/


2、修改启动类,添加注解

添加注解@EnableDiscoveryClient和@EnableFeignClients



3、添加service层

package com.learning.pocher.helloconsumer.feignservice;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("hello-service1")
public interface ServiceClient {
@RequestMapping("/callToXiaoAI")
String getHelloService1();
}


4、添加Controller层

package com.learning.pocher.helloconsumer.controller;

import com.learning.pocher.helloconsumer.feignservice.ServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloConsumer {
@Autowired
ServiceClient serviceClient;

@RequestMapping("/feign-consumer")
public String helloConsumer(){
String returnMessage = serviceClient.getHelloService1();
System.out.println(returnMessage);
return "returnMessage";
}
}


5、目录结构



第二部分:依次启动各个服务

注意:启动顺序不要错了哦,先启动注册中心eureka-center,然后启动服务提供者hello-service1,最后启动服务消费者hello-consumer

1、启动注册中eureka-center服务

没有此工程的参考 SpringCloud创建Eureka注册中心 这篇文章,或者直接下载此eureka-center工程

github仓库地址:https://github.com/pocher/SpringCloudEurkaDemo



访问:localhost:1110,这时是没有服务可用的。



2、启动hello-service1服务

没有此工程的参考 创建SpringCloud服务,并向注册中心注册自己 这篇文章,或者直接下载此hello-service1工程

github仓库地址:https://github.com/pocher/SpringCloudEurka-helloservice1



刷新:localhost:1110,这时发现有一个hello-service1服务,就是我们启动的hello-service1服务。



3、启动hello-consumer工程



刷新:localhost:1110,这时发现hello-consumer服务也在服务列表里。

有红色英文字体提示



没有红色粗体英文提示



第三部分:Feign服务的调用

1、访问hello-consumer工程

访问地址:http://localhost:2221/feign-consumer

看到返回的数据



2、请求流程分析:

A.浏览器发送请求,通过指定域名和端口(http://localhost:2221)匹配服务,

B.然后通过@RequestMapping匹配路径(/feign-consumer)到指定Controller中的

方法helloConsumer()



第四部分:项目下载

github仓库地址:https://github.com/pocher/SpringCloudEurka-helloconsumer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息