Spring Cloud 入门教程第二篇: 服务消费者(Feign)(Hoxton.M3 版本)

环境:
IDEA
JDK1.8
Spring Cloud Hoxton.M3
Spring Boot 2.2.0
一、Feign简介

  Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息,而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。Feign整合了Ribbon和Hystrix。

  总起来说,Feign具有如下特性:

1.可插拔的注解支持,包括Feign注解和JAX-RS注解;
2.支持可插拔的HTTP编码器和解码器;
3.支持Hystrix和它的Fallback;
4.支持Ribbon的负载均衡;
5.支持HTTP请求和响应的压缩。
二、创建项目

1、File ----- New -----Project
Spring Cloud 教程
2、Spring Initializr ----- Next
Spring Cloud 教程
3、输入 Group 和 Artifact
点击 Next
Spring Cloud 教程
4、选择Spring cloud Discovery -----Eureka Discovery Client---- Next
Spring Cloud 教程
5、创建完成 点击 Finish 则在新窗口打开新建的工程
Spring Cloud 教程

三、完善项目

1、由于刚刚创建的文件是初始化默认的项目,需要添加相应注解和配置文件。具体详情如下:

a.Eureka的依赖spring-cloud-starter-eureka
<!-- spring-cloud-starter-eureka -->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
b.Feign的依赖spring-cloud-starter-feign
<!-- spring-cloud-starter-feign -->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
c.Web的依赖spring-boot-starter-web
<!-- spring-boot-starter-web -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2、ClientserverApplication.java添加注解@EnableEurekaClient
Spring Cloud 教程

3、完善配置文件application.properties 需要制定应用名称,应用名称为:Client_Server1

#服务的端口
server.port=8762
#注册到服务中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#是否注册到eureka服务器,
eureka.client.registerWithEureka=true 
#是否从eureka服务器获取注册信息
eureka.client.fetchRegistry=true
#是否开启自我保护模式,默认为true。
eureka.server.enable-self-preservation=true
#续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eureka.server.eviction-interval-timer-in-ms=10000
spring.application.name=Client-Server1

4、创建ClientController

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2019/10/18.
 */
@RestController
public class ClientController {
    @RequestMapping("/test")
    //    @ResponseBody
    public String test(@RequestParam String  name){
        return  "Hello World!==port:8762==="+name;
    }

}

Spring Cloud 教程

四、运行项目。

1、进入FeignclientserverApplication.java 启动类,右击选择Run
Spring Cloud 教程
2、进入服务注册中心:http://localhost:8761/ 显示服务已经注册成功(显示两个服务,是由于把其中一个复制了一下,修改端口号:8763,两个Feign项目所以就显示两个。)
Spring Cloud 入门教程
3、输入http://localhost:8762/test?name=123
返回数据如下:
Hello World!port:8762=123
Spring Cloud 教程
最后上一张启动三个服务的图:
Spring Cloud 教程以上就是一个Spring Cloud 的服务消费者(Feign)的创建过程。欢迎交流学习。

五、项目代码:
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.mcus</groupId>
	<artifactId>feignclientserver1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>feignclientserver</name>
	<description>Demo project for Spring Boot</description>

	<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>Hoxton.M3</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- spring-cloud-starter-eureka -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<!-- spring-cloud-starter-feign -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<!-- spring-boot-starter-web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</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>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>

</project>
application.properties文件
server.port=8762
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#是否注册到eureka服务器,
eureka.client.registerWithEureka=true 
#是否从eureka服务器获取注册信息
eureka.client.fetchRegistry=true
#是否开启自我保护模式,默认为true。
eureka.server.enable-self-preservation=true
#续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eureka.server.eviction-interval-timer-in-ms=10000
spring.application.name=Client-Server1

ClientserverApplication.java
package cn.scpro;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ClientserverApplication {

	public static void main(String[] args) {
		SpringApplication.run(ClientserverApplication.class, args);
	}

}

Controller
package cn.scpro.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2019/10/18.
 */
@RestController
public class ClientController {
    @RequestMapping("/test")
//    @ResponseBody
    public String test(@RequestParam String  name){

        return  "Hello World!==port:8762==="+name;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值