在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
2、添加依赖包
修改mycloud-service-provider1模块pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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,FeginTestResource.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 FeginTestResource {
@Autowired
FeignClientService feignClientService;
@GetMapping("/feginTest/{id}")
public String feginTest(@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/feginTest/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
https://spring.io/projects/spring-cloud-openfeign
https://www.imooc.com/article/299674
注:最新代码上传至https://github.com/csj50/mycloud