study-url:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.4.6.RELEASE/multi/multi_spring-cloud-feign.html#netflix-feign-starter

####################使用默认的Feign方式###########################

1、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 http://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.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>eureka-client-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-feign</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>

2、添加application.yml

spring:
application:
name: eureka-client-feign #应用名
logging: #logging日志配置
level:
root: INFO
org.hibernate: INFO
server:
port: 8666 #服务端口 eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8661/eureka

3、启动类添加注解

package com.test.eurekaclientfeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
public class EurekaClientFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientFeignApplication.class, args);
} }

4、创建UserFeignClient.java,这个类配置Feign远程调用的URL

package com.test.eurekaclientfeign.feign;

import com.test.eurekaclientfeign.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @FeignClient("eureka-client-user") //指明feign远程调用的虚拟主机名
public interface UserFeignClient {
//远程服务自己设置的urn,目前feign只支持RequestMapping注解
@RequestMapping(method = RequestMethod.GET, value = "/user/{id}")
User findById(@PathVariable("id") Long id);
}

5、创建MovieController.java,这里是本机提供服务的类,也是调用远程服务的调用方。

package com.test.eurekaclientfeign.controller;

import com.test.eurekaclientfeign.entity.User;
import com.test.eurekaclientfeign.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; @RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient;
/*
@Autowired
private FeignClient1 feignClient1;*/ @GetMapping(value = "/movie/{id}")
public User findById(@PathVariable("id") Long id) {
return this.userFeignClient.findById(id);
} /*@GetMapping(value = "/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName) {
System.out.println("11111111");
return this.feignClient1.findServiceInfoFromEurekaByServiceName(serviceName);
}*/ }

6、创建User.java

package com.test.eurekaclientfeign.entity;

import java.io.Serializable;
import java.math.BigDecimal; public class User implements Serializable { private Long id; private String name; private String username; private BigDecimal balance; private short age; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public BigDecimal getBalance() {
return balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
} public short getAge() {
return age;
} public void setAge(short age) {
this.age = age;
}
}

7、访问URL

http://localhost:8661/   #服务发现
http://192.168.137.1:8666/movie/1 #Feign测试接口

至此,已实现Feign的基本调用。

######################使用自己配置Feign方式#########################

注意:在上面的基础上,将上面的方式注释,然后再采用下面的方式

1、创建自己配置的UserFeignClientConfig.java Feign类

package com.test.eurekaclientfeign.feign;

import feign.Contract;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class UserFeignClientConfig {
/* @Bean
public Contract feignContract() {
return new feign.Contract.Default();
}*/ @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}

2、创建一个UserFeignClient1.java ,将自己创建的Feign 配置类通过@FeignClient(name = "eureka-client-user", configuration = UserFeignClientConfig.class)添加进去。

package com.test.eurekaclientfeign.feign;

import com.test.eurekaclientfeign.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name = "eureka-client-user", configuration = UserFeignClientConfig.class)
public interface UserFeignClient1 {
@RequestMapping(method = RequestMethod.GET, value = "/user/{id}")
User findById(@PathVariable("id") Long id);
}

3、控制类

package com.test.eurekaclientfeign.controller;

import com.test.eurekaclientfeign.entity.User;
import com.test.eurekaclientfeign.feign.UserFeignClient1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; @RestController
public class MovieController { /* @Autowired
private UserFeignClient userFeignClient;*/ @Autowired(required = true)
private UserFeignClient1 userFeignClient1; @GetMapping(value = "/config/{id}")
public User findById(@PathVariable("id") Long id) {
return this.userFeignClient1.findById(id);
} /*@GetMapping(value = "/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName) {
System.out.println("11111111");
return this.feignClient1.findServiceInfoFromEurekaByServiceName(serviceName);
}*/ }

第七章 SpringCloud之非声明式RestClient:Feign的更多相关文章

  1. (转)SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

    springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事事务,引入它们依赖的时候,事物就 ...

  2. SpringBoot非官方教程 | 第七篇:springboot开启声明式事务

    转载请标明出处: http://blog.csdn.net/forezp/article/details/70833629 本文出自方志朋的博客 springboot开启事务很简单,只需要一个注解@T ...

  3. SpringCloud学习笔记:声明式调用Feign(4)

    1. Feign简介 Feign采用声明式API接口的风格,将Java HTTP客户端绑定到它的内部. Feign的首要目标是简化Java HTTP客户端调用过程. 2.Feign客户端示例 Feig ...

  4. spring boot 2.0.3+spring cloud (Finchley)3、声明式调用Feign

    Feign受Retrofix.JAXRS-2.0和WebSocket影响,采用了声明式API接口的风格,将Java Http客户端绑定到他的内部.Feign的首要目标是将Java Http客户端调用过 ...

  5. spring cloud深入学习(四)-----eureka源码解析、ribbon解析、声明式调用feign

    基本概念 1.Registe 一一服务注册当eureka Client向Eureka Server注册时,Eureka Client提供自身的元数据,比如IP地址.端口.运行状况指标的Uri.主页地址 ...

  6. 微服务深入浅出(5)-- 声明式调用Feign

    Feign的使用 Feign采用了声明式的API接口的风格,将Java Http客户端绑定到它的内部,从而调用过程变的简单. 配置文件: spring: application: name: eure ...

  7. spring cloud微服务快速教程之(三)声明式访问Feign、负载均衡Ribbon

    0-前言 eureka实际上已经集成了负载均衡调度框架Ribbon: 我们有了各个微服务了,那怎么来调用他们呢,一种方法是可以使用 RestTemplate(如:String str= restTem ...

  8. Spring Cloud声明式调用Feign负载均衡FeignClient详解

    为了深入理解Feign,下面将从源码的角度来讲解Feign.首先来看看FeignClient注解@FeignClient的源码,代码如下: FeignClient注解被@Target(ElementT ...

  9. SpringCloud之声明式服务调用 Feign(三)

    一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样.我们知道,阿里巴巴的doubbo采 ...

随机推荐

  1. Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)

    众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题.网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页. ...

  2. Ubuntu中用bitbake core-image-minimal时,出错:from bb import data

    问题描述: 在准备ARM交叉编译环境时,执行完命令: DISTRO=fsl-imx-x11 MACHINE=imx6qsabresd source fsl-setup-release.sh -b bu ...

  3. iperf和iperf3详解

    一.iperf server端: iperf -s -p 25001 -B 192.168.33.103 (-u) -s  指定server端 -p 指定端口(要和客户端一致) -B 绑定ip地址 - ...

  4. 小黄车ofo法人被限制出境,它究竟还能撑多久?

    因为季节的原因,现在正是骑车的好时候,而且北京也开通了一条自行车的专用路.但就是在这么好的时候,我们发现,路边的小黄车却越来越少了,而且它的麻烦还不断! ofo法人被限制出境 6月12日消息,据上海市 ...

  5. 指针、数组与sizeof运算符

    指针.数组与sizcof运算符 (1)sizeof是c语言的一个运算符(主要sizeof不是函数,虽然用法很像函数),sizeof的作用是用来返同()里面的变量或者数据类型占用的内存字节数. (2)s ...

  6. FTP部署与使用

    1.1 环境检查 [root@www ~]# cat /etc/redhat-release #系统版本,6系列等区别不大,都可以 CentOS Linux release 7.5.1804 (Cor ...

  7. 牛客练习赛44 B 小y的线段 (思维)

    链接:https://ac.nowcoder.com/acm/contest/634/B 来源:牛客网 题目描述 给出n条线段,第i条线段的长度为a_ia i ​ ,每次可以从第i条线段的j位置跳到第 ...

  8. shell_hive

    (1)获取参数:从shell文件传来参数,调用:$1,$2,$3 load_date=$1 clearn_date=`date -d"$2 day ago $load_date" ...

  9. 我说CMMI之二:CMMI里有什么?--转载

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/dylanren/article/deta ...

  10. chattr&lsattr

    chattr 和 lsattr 用来查看和改变文件属性,chmod 只是用来改变文件的读写与可执行权限,而文件的属性控制是由 chattr 来完成 常用用法: chattr +i /home/test ...