springcloud项目创建笔记8 之《feign客户端》

在springboot中,我们使用restTemplate来发送http请求。而在springcloud中,提供了feign客户端

1、建立一个内部应用mycloud-inside-app1,提供controller调用类InsideApp1Resource.java

package com.example.mycloud.resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/insideApp1")
public class InsideApp1Resource {

	@GetMapping("/remoteApi/{id}")
	public String remoteApi(@PathVariable String id) {
		return "hello:"+id;
	}
}

测试:http://localhost:8015/insideApp1/remoteApi/123
返回:hello:123

----------provider1调用inside-app1----------
2、添加依赖包
修改mycloud-service-provider1模块pom.xml

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
	<groupId>io.github.openfeign</groupId>
	<artifactId>feign-okhttp</artifactId>
</dependency>

3、启动类添加注解@EnableFeignClients
注意包扫描路径

@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan("com.example.mycloud")
@EnableFeignClients("com.example.mycloud")
public class ServiceProvider1Application {
	public static void main(String[] args) {
        SpringApplication.run(ServiceProvider1Application.class, args);
    }
}

4、application.yml文件添加feign配置

#feign client配置
feign:
  client:
    config:
      #或者为想要调用的微服务名称
      default:
        connectTimeout: 30000
        readTimeout: 30000
        loggerLevel: basic
  okhttp:
    #使用okhttp作为client
    enabled: true
  hystrix:
    #禁用hystrix
    enabled: false

5、添加包com.example.mycloud.feign,在包下添加
客户端调用接口类FeignClientService.java,其中inside-app1表示内部应用mycloud-inside-app1在eureka注册时的服务名称

package com.example.mycloud.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "inside-app1")
public interface FeignClientService {
	
	@GetMapping("/insideApp1/remoteApi/{id}")
	public String remoteApi(@PathVariable String id);
}

6、添加测试controller,FeignTestResource.java

package com.example.mycloud.resource;

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;

import com.example.mycloud.feign.FeignClientService;

@RestController
public class FeignTestResource {

	@Autowired
	FeignClientService feignClientService;
	
	@GetMapping("/feignTest/{id}")
	public String feignTest(@PathVariable String id) {
		return feignClientService.remoteApi(id);
	}
}

7、报错com.netflix.client.ClientException: Load balancer does not have available server for client: inside-app1
需要在application.yml文件中修改eureka client的配置:
fetchRegistry: true
因为原来是false,就不会从注册中心拉取服务列表

8、测试
启动模块:
mycloud-eureka
mycloud-inside-app1
mycloud-service-provider1
访问浏览器:
http://localhost:8011/feignTest/233
返回:
hello:233

9、初始化日志

2021-03-04 21:00:04.537  INFO 19708 --- [nio-8011-exec-2] c.netflix.config.ChainedDynamicProperty  : Flipping property: inside-app1.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2021-03-04 21:00:04.552  INFO 19708 --- [nio-8011-exec-2] c.n.u.concurrent.ShutdownEnabledTimer    : Shutdown hook installed for: NFLoadBalancer-PingTimer-inside-app1
2021-03-04 21:00:04.552  INFO 19708 --- [nio-8011-exec-2] c.netflix.loadbalancer.BaseLoadBalancer  : Client: inside-app1 instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=inside-app1,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2021-03-04 21:00:04.558  INFO 19708 --- [nio-8011-exec-2] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2021-03-04 21:00:04.572  INFO 19708 --- [nio-8011-exec-2] c.netflix.config.ChainedDynamicProperty  : Flipping property: inside-app1.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2021-03-04 21:00:04.573  INFO 19708 --- [nio-8011-exec-2] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client inside-app1 initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=inside-app1,current list of Servers=[localhost:8015],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;	Instance count:1;	Active connections count: 0;	Circuit breaker tripped count: 0;	Active connections per server: 0.0;]
},Server stats: [[Server:localhost:8015;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@38ccb981

参考资料:
https://github.com/OpenFeign/feign
Spring Cloud OpenFeign
SpringBoot 2.x 开发案例之整合HTTP客户端Feign_慕课手记

注:最新代码上传至https://github.com/csj50/mycloud

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值