前言声明:
如果您有更好的技术与作者分享,或者商业合作;请访问作者个人网站 http://www.esqabc.com/view/message.html 留言给作者。
如果该案例触犯您的专利,请在这里:http://www.esqabc.com/view/message.html 留言给作者说明原由,作者一经查实,马上删除。
一、Feign简介
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
总的来说:
Feign 采用的是基于接口的注解
Feign 整合了ribbon,具有负载均衡的能力
整合了Hystrix,具有熔断的能力
二(2),搭建服务消费者使用(Feign)
开始前准备工作:
1,创建两个服务生产者(esq-provider-a和esq-provider-b)
2,创建一个服务消费者(esq-consumer)
架构如下:
服务生产者一(esq-provider-a) :
1,pom配置文件:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- 父项目 -->
<parent>
<groupId>com.esq.master</groupId>
<artifactId>esq-master</artifactId>
<version>1.0.0</version>
</parent>
<!-- 本项目信息 -->
<groupId>com.esq.provider</groupId>
<artifactId>esq-provider-a</artifactId>
<packaging>pom</packaging>
<name>esq-provider-a</name>
<description>服务产生者1</description>
<!-- 配置编码格式 begin -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- 配置编码格式 end -->
<!-- JAR包配置 begin-->
<dependencies>
<!-- 引入关联实体与公共方法-->
<dependency>
<groupId>com.esq.common</groupId>
<artifactId>esq-common</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Cloud 服务需要的jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Springboot web服务器的jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- JAR包配置 end-->
<!-- 插件配置 begin -->
<build>
<plugins>
</plugins>
</build>
<!-- 插件配置 end -->
</project>
2,application.properties配置文件:
#配置启动端口号
server.port=9011
#配置项目访问路径
#server.context-path=/eureka
#配置服务名称
spring.application.name=esq-service
#配置服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/
3,创建一个controler(UserControler):
package org.esq.provider.controler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户控制器
* @author esq
* @Create 2019-09-09 09:09:09
* @Website www.esqabc.com
* @WeChat
*/
@RestController
public class UserControler {
@Value("${server.port}")
String port;
@RequestMapping("/esquser")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "欢迎" + name + "访问,来自服务端口:" + port+"的服务。";
}
}
4,启动文件(ProviderAApplication):
package org.esq.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.CommandLineRunner;
import java.util.Arrays;
/**
* 服务产生者1启动类
* @author esq
* @Create 2019-09-09 09:09:09
* @Website www.esqabc.com
* @WeChat
*/
@SpringBootApplication
@EnableEurekaClient
public class ProviderAApplication{
public static void main(String[] args) {
System.out.println("。。。。。。服务产生者程序开始启动。。。。。。");
long startTime = System.currentTimeMillis(); //获取开始时间
SpringApplication.run(ProviderAApplication.class, args);
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("服务产生者程序启动用时:" + (endTime - startTime) + "毫秒");//输出程序运行时间
System.out.println("服务产生者程序启动一共用时:" + (endTime - startTime)/60 + "秒。。。。。。");
System.out.println("。。。。。。服务产生者程序启动成功。。。。。。");
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
Arrays.stream(beanNames).forEach(System.out::println);
};
}
}
服务生产者二(esq-provider-b) :
1,pom配置文件:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- 父项目 -->
<parent>
<groupId>com.esq.master</groupId>
<artifactId>esq-master</artifactId>
<version>1.0.0</version>
</parent>
<!-- 本项目信息 -->
<groupId>com.esq.provider</groupId>
<artifactId>esq-provider-b</artifactId>
<packaging>pom</packaging>
<name>esq-provider-b</name>
<description>服务产生者2</description>
<!-- 配置编码格式 begin -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- 配置编码格式 end -->
<!-- JAR包配置 begin-->
<dependencies>
<!-- 引入关联实体与公共方法-->
<dependency>
<groupId>com.esq.common</groupId>
<artifactId>esq-common</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Cloud 服务需要的jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Springboot web服务器的jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- JAR包配置 end-->
<!-- 插件配置 begin -->
<build>
<plugins>
</plugins>
</build>
<!-- 插件配置 end -->
</project>
2,application.properties配置文件:
#配置启动端口号
server.port=9022
#配置项目访问路径
#server.context-path=/eureka
#配置服务名称
spring.application.name=esq-service
#配置服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/
3,创建一个controler(UserControler):
package org.esq.provider.controler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户控制器
* @author esq
* @Create 2019-09-09 09:09:09
* @Website www.esqabc.com
* @WeChat
*/
@RestController
public class UserControler {
@Value("${server.port}")
String port;
@RequestMapping("/esquser")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "欢迎" + name + "访问,来自服务端口:" + port+"的服务。";
}
}
4,启动文件(ProviderBApplication):
package org.esq.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.CommandLineRunner;
import java.util.Arrays;
/**
* 服务产生者2启动类
* @author esq
* @Create 2019-09-09 09:09:09
* @Website www.esqabc.com
* @WeChat
*/
@SpringBootApplication
@EnableEurekaClient
public class ProviderBApplication{
public static void main(String[] args) {
System.out.println("。。。。。。服务产生者程序开始启动。。。。。。");
long startTime = System.currentTimeMillis(); //获取开始时间
SpringApplication.run(ProviderAApplication.class, args);
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("服务产生者程序启动用时:" + (endTime - startTime) + "毫秒");//输出程序运行时间
System.out.println("服务产生者程序启动一共用时:" + (endTime - startTime)/60 + "秒。。。。。。");
System.out.println("。。。。。。服务产生者程序启动成功。。。。。。");
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
Arrays.stream(beanNames).forEach(System.out::println);
};
}
}
创建一个服务消费者(esq-consumer):
1,pom配置文件:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- 父项目 -->
<parent>
<groupId>com.esq.master</groupId>
<artifactId>esq-master</artifactId>
<version>1.0.0</version>
</parent>
<!-- 本项目信息 -->
<groupId>com.esq.consumer</groupId>
<artifactId>esq-consumer</artifactId>
<name>esq-consumer</name>
<url>http://maven.apache.org</url>
<description>服务消费者</description>
<!-- 配置编码格式 begin -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- 配置编码格式 end -->
<!-- JAR包配置 begin-->
<dependencies>
<!-- 引入关联实体与公共方法-->
<dependency>
<groupId>com.esq.common</groupId>
<artifactId>esq-common</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Cloud 服务需要的jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Springboot web服务器的jar-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 负载均衡客户端 的jar -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<!-- JAR包配置 end-->
<!-- 插件配置 begin -->
<build>
<plugins>
</plugins>
</build>
<!-- 插件配置 end -->
</project>
2,application.properties配置文件:
# 配置启动端口号
server.port=9002
# 配置项目访问路径
#server.context-path=/eureka
#配置服务名称
spring.application.name=esq-consumer
#服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/
3,创建一个controler(UserControler):
package com.esq.consumer.controler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.esq.consumer.service.UserService;
/**
* 用户控制器
* @author esq
* @Create 2019-09-09 09:09:09
* @Website www.esqabc.com
* @WeChat
*/
@RestController
public class UserControler {
@Autowired
UserService userService;
@GetMapping(value = "/esquser")
public String hi(@RequestParam String name) {
System.out.println(name);
System.out.println("进入消费者==》》用户控制器==》》调用esquser服务");
return userService.getServiceInfo(name);
}
}
package com.esq.consumer.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 通过@ FeignClient("服务名"),来指定调用哪个服务。比如在代码中调用了esq-service服务的"esquser"接口
* @author esq
* @Create 2019-09-09 09:09:09
* @Website www.esqabc.com
* @WeChat
*/
@FeignClient(value = "esq-service")
public interface UserService {
@RequestMapping(value = "/esquser",method = RequestMethod.GET)
public String getServiceInfo(@RequestParam(value = "name") String name);
}
4,启动文件(ConsumerApplication):
package com.esq.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.boot.CommandLineRunner;
import java.util.Arrays;
/**
* 服务消费者启动类
* @author esq
* @Create 2019-09-09 09:09:09
* @Website www.esqabc.com
* @WeChat
*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
/**
* 通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean:restTemplate;
* 并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
*/
public class ConsumerApplication{
public static void main(String[] args) {
System.out.println("。。。。。。服务消费者程序开始启动。。。。。。");
long startTime = System.currentTimeMillis(); //获取开始时间
SpringApplication.run(ConsumerApplication.class, args);
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("服务消费者程序启动用时:" + (endTime - startTime) + "毫秒");//输出程序运行时间
System.out.println("服务消费者程序启动一共用时:" + (endTime - startTime)/60 + "秒。。。。。。");
System.out.println("。。。。。。服务消费者程序启动成功。。。。。。");
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
Arrays.stream(beanNames).forEach(System.out::println);
};
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
注意:项目启动顺序为:先启动注册中心(esq-registry) ,然后启动(服务生产者1,2)(esq-provider-a,esq-provider-b) 最后启动服务消费者(esq-consumer):
在浏览器输入:http://localhost:9000/ 可以看到同一个服务有两个端口 9011 9022:
在浏览器输入:http://localhost:9002/esquser?name=12312 会交替显示:
欢迎12312访问,来自服务端口:9011的服务。
欢迎12312访问,来自服务端口:9022的服务。
总结:
一个服务注册中心(esq-registry)端口为9000
跑了两个实例,端口分别为9011,9022,分别向服务注册中心,注册同一个服务名称端口不同的服务
消费者(esq-consumer)端口为9002,向服务注册中心注册
当esq-consumer通过restTemplate调用实例的esquser接口时,因为用Ribbon进行了负载均衡,会轮流的调用实例:9011和9022 两个端口的esquser接口;
===============================
更多请访问:https://blog.csdn.net/esqabc/article/details/87804603
=================================