`
huangyongxing310
  • 浏览: 491961 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

spring cloud feign方式使用服务

 
阅读更多

spring cloud feign方式使用服务


feign采用的是接口加注解
feign 整合了ribbon(有提供负载均衡功能)


<?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.midea</groupId>
	<artifactId>base</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>base</name>

	<!-- 设定仓库,按设定顺序进行查找. -->
	<!--<repositories> <repository> <id>public</id> <name>Team Nexus Repository</name> 
		<url>http://10.33.183.113:8081/nexus/content/groups/public</url> <snapshots> 
		<enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> 
		</repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> 
		<name>Team Nexus Repository</name> <url>http://10.33.183.113:8081/nexus/content/groups/public</url> 
		<snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> 
		</pluginRepository> </pluginRepositories> -->

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	
	<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>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.4.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.4.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
	<build>

	</build>
</project>



server.port=8765
#logging.pattern.level=INFO

#服务器路径
server.context-path=/feign


#
#eureka.instance.hostname=localHost
#表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false。
#eureka.client.registerWithEureka=false
#fetchRegistry表示是否从eureka服务器获取注册信息,同上,这里不需要
#eureka.client.fetchRegistry=false
#设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址。
#eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/


eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eurekaServer/eureka/

spring.application.name=feign




package com.Service;

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

@FeignClient(value = "eureka-Client") //服务名字
public interface CallService {
    @RequestMapping(value = "/eurekaClient/Test/test",method = RequestMethod.GET)
    String callService();
}


package com.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.Service.CallService;

import io.swagger.annotations.Api;

@Api(value = "/FeignTest", description = "测试接口", tags = "TestController")
@RequestMapping("/FeignTest")
@RestController
public class TestController {
	final static Logger logger = LogManager.getLogger(TestController.class);

	@Autowired
	CallService callService;

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test() {
		logger.info("test");

		// 调用服务进行远程调用
		String temp = callService.callService();

		logger.info("temp == " + temp);

		return temp;
	}
}




package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;



@SpringBootApplication //spring boot 开启应用
//@EnableDiscoveryClient //Discovery Service”有多种实现,比如:eureka, consul, zookeeper。
@EnableEurekaClient //只能为eureka作用
@EnableFeignClients //Feign
public class Application {

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



https://blog.csdn.net/forezp/article/details/73480304(深入理解Feign之源码解析)


https://www.jianshu.com/p/d9177fbc9c6b(@HystrixCommand的实现原理(AOP))
HystrixCommand的构造是AOP的ProceedingJoinPoint


http://blog.51cto.com/12980017/2140699(自定义Eureka集群负载均衡策略)


https://yq.aliyun.com/articles/624145(SpringCloud(Finchley版本)中Feign与Hystrix集成的配置总结,实现原理分析)
分享到:
评论

相关推荐

    Spring Cloud Feign统一设置验证token实现方法解析

    Spring Cloud Feign是一个基于Netflix的Feign组件,提供了一个简洁的方式来构建RESTful风格的微服务接口。Feign组件提供了一个统一的接口调用方式,使得微服务之间的调用变得更加简洁和高效。在微服务架构中, token...

    springcloud feign 服务消费者 类似 webservice

    总之,Spring Cloud Feign为微服务架构中的服务消费者提供了优雅的调用方式,降低了开发复杂性,提高了代码的可读性和可维护性。通过合理的配置和使用,我们可以构建出高可用、高并发的分布式系统。

    微服务springcloud之feign使用demo

    RS注解,SpringCloud又为Feign增加了对SpringMVC注解的支持,同时为了能够使用和Spring Web中默认使用的相同的httpMessageConverter,SpringCloud集成了Ribbon和Eureka,用来在使用Feign时能够为其提供一个负载均衡...

    springcloud feign服务间的相互调用.doc

    Spring Cloud Feign 服务间相互调用示例 在微服务架构中,服务间的...使用 Spring Cloud Feign 可以轻松地实现服务间的相互调用。通过使用 Feign 客户端,我们可以方便地调用其他服务的方法,从而实现服务间的集成。

    spring cloud feign实现远程调用服务传输文件的方法

    Spring Cloud Feign 是一个基于 Java 的声明式 RESTful 客户端,提供了一种简单、可靠的方式来调用远程服务。在本文中,我们将介绍如何使用 Spring Cloud Feign 实现远程调用服务传输文件的方法。 Feign 介绍 ...

    spring cloud feign demo

    Spring Cloud Feign 是一个声明式的 Web 服务客户端,它使得编写 Web 服务客户端变得简单。Feign 让消费者能够以一种声明式的方式定义接口,这些接口将被自动映射到 HTTP 请求。它整合了 Ribbon 和 Eureka,可以方便...

    springcloud feign整合hystrix(服务降级).doc

    在Spring Cloud微服务架构中,Feign和Hystrix是两个重要的组件,它们协同工作以实现服务间的调用和容错处理。Feign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得简单,而Hystrix则是一个用于处理延迟和...

    SpringCloud-创建服务消费者-Feign方式示例代码.zip

    在这个名为"SpringCloud-创建服务消费者-Feign方式示例代码.zip"的压缩包中,我们预计将看到一个简单的Spring Cloud应用,该应用展示了如何利用Feign作为服务消费者来调用其他服务。 首先,让我们了解Feign的基本...

    75-Spring Cloud Feign使用笔记1

    总结来说,Spring Cloud Feign 提供了一种声明式的微服务调用方式,通过简单的接口定义,实现了对远程服务的透明调用。结合 Ribbon 和 Hystrix,Feign 可以提供负载均衡、服务容错和降级功能,同时通过灵活的日志...

    springcloud-feign.zip

    springcloud整合openFeign,包括feign的基本使用、传参、指定特定的服务器、负载均衡等使用方法。 模块有: springcloud-feign-api springcloud-feign-consumer springcloud-feign-provider springcloud-feign-...

    SpringCloud之Feign

    【SpringCloud之Feign】是Spring Cloud生态体系中一个重要的组件,主要用于服务间的调用,实现了声明式的服务调用,极大地简化了微服务之间的通信。Feign基于Netflix Hystrix进行了整合,支持服务熔断,提高了系统的...

    第五章 SpringCloud 使用Feign调用服务.pdf

    本章节主要介绍了如何在SpringCloud项目中使用Feign来替代RestTemplate进行服务间的通信。 首先,我们了解项目的概要。在前几章的学习中,我们已经使用RestTemplate实现了服务调用,并且通过注册中心实现了服务的...

    Spring Cloud 之 Eureka集群整合Zuul、Feign-源码

    在实际开发中,还需要考虑安全、性能优化等因素,例如使用Ribbon进行客户端负载均衡,添加Hystrix熔断机制以防止服务雪崩,以及使用Spring Cloud Gateway替换Zuul以获取更多高级特性。 总之,通过Eureka集群实现...

    SpringCloud使用Feign做断路由

    在SpringCloud生态系统中,Feign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得简单。Feign的设计灵感来源于Retrofit和JAX-RS,它允许开发者通过接口定义来调用HTTP服务,提供了更优雅的远程调用方式。...

    Spring Cloud中关于Feign的常见问题总结

    SpringCloud中关于Feign的常见问题总结,包括常用的请求注解、@PathVariable、FeignClient多参数的构造等

    糞坑-SpringCloud中使用Feign的坑

    在SpringCloud生态系统中,Feign是一个非常重要的组件,它提供了声明式的服务调用方式,使得微服务间的通信变得更加简洁。然而,在实际应用中,我们可能会遇到一些“坑”,特别是当涉及到处理复杂请求体,如...

    38-Spring Cloud Feign重试1

    Spring Cloud Feign 重试机制是微服务架构中不可或缺的一部分,它确保了服务间的通信可靠性。Feign 是基于声明式的客户端,它通过 Ribbon 进行客户端负载均衡来调用其他服务。Ribbon 提供了自动重试的功能,使得在...

    spring cloud feign不支持@RequestBody+ RequestMethod.GET报错的解决方法

    spring cloud feign是一种基于HTTP的服务调用方式,而在使用spring cloud feign时,我们可能会遇到不支持@RequestBody+ RequestMethod.GET报错的问题。本文将介绍这个问题的解决方法。 问题梳理: 在使用spring ...

    Spring Cloud Feign 自定义配置(重试、拦截与错误码处理) 代码实践

    总结来说,Spring Cloud Feign通过简化远程服务调用的方式,极大地提高了开发效率。通过默认配置和灵活的自定义配置机制,开发者可以根据业务需求调整和优化Feign的行为,以应对各种复杂场景的需要。

Global site tag (gtag.js) - Google Analytics