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

注意:在上两篇 创建SpringCloud服务,并向注册中心注册自己  、SpringCloud创建Eureka注册中心

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

第一部分:创建SpringBoot工程

1、新建项目

164548_PUHG_2853666.png

2、选择创模板

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

164626_36Dx_2853666.png

3、填写公司信息

103341_btFE_2853666.png

103906_p9rj_2853666.png

103917_y5T2_2853666.png

4、勾选依赖

103838_rnNg_2853666.png

5、选择项目储存路径

103540_Kc8W_2853666.png

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

134958_3lGd_2853666.png

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、目录结构

112124_Sqa3_2853666.png

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

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

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

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

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

113030_mBpj_2853666.png

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

113606_UenN_2853666.png

2、启动hello-service1服务

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

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

113106_bGQF_2853666.png

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

113811_hU2Z_2853666.png

3、启动hello-consumer工程

113137_xDAw_2853666.png

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

有红色英文字体提示

113919_afre_2853666.png

没有红色粗体英文提示

114408_HWbN_2853666.png

 

第三部分:Feign服务的调用

1、访问hello-consumer工程

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

看到返回的数据

114852_0GqM_2853666.png

2、请求流程分析:

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

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

方法helloConsumer()

115729_iJKg_2853666.png

第四部分:项目下载

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

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值