SpringCloud Feign声明式服务调用

1. 加入pom依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

2. Application.java上声明@EnableFeignClients

表示允许声明式feign调用

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = {
    
     "com.service.client.feign.inter" })
public class Application {
    
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Application.class, args);
    }
}

3. @FeignClient声明接口调用服务名,(SpirngMVC绑定方法,可有可无)

接口@FeignClient注解指定服务名来绑定服务,然后再使用Spring MVC的注解来绑定具体该服务提供的REST接口。

@FeignClient("hello-service-provider")
public interface HelloServiceFeign {
    
    

    @RequestMapping(value = "/demo/getHost", method = RequestMethod.GET)
    public String getHost(String name);

    @RequestMapping(value = "/demo/postPerson", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public Person postPerson(String name);

注意,(SpirngMVC绑定方法,可有可无),若写则@RequestMapping需要写全面,假如XXInterface中Controller层也有 @RestController, @RequestMapping("/hello")

则应该为:

@FeignClient("hello-service-provider")
public interface HelloServiceFeign {
    
    

    @RequestMapping(value = "/hello/demo/getHost", method = RequestMethod.GET)
    public String getHost(String name);

    @RequestMapping(value = "/hello/demo/postPerson", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public Person postPerson(String name);
}

4. Feign负载均衡是通过ribbon实现的

Spring Cloud Feign的客户端负载均衡是通过Spring Cloud Ribbon实现的,可以直接配置Ribbon客户端的方式来自定义各个服务客户端调用参数。

Spring Cloud Ribbon默认负载均衡策略是轮询策略,不过该不一定满足我们的需要。Ribbon一共提供了7种负载均衡策略,如果我们需要ZoneAvoidanceRule,首先要在application.properties文件中添加配置,如下所示:

ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.ZoneAvoidanceRule

不过,只是添加了如上配置,还无法实现负载均衡策略的更改。还需要实例化该策略,可以在应用主类中直接加入IRule实例的创建,如下:

/**
 * 服务调用者,,eureka客户端 feign调用
 *
 * @version
 * @since 1.8
 */
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = {
    
     "com.client.feign.inter" })
public class Application {
    
    

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

    @Bean
    public IRule feignRule() {
    
    
        return new ZoneAvoidanceRule();
    }
}

5. 常见问题与解决

  • 遇到404,检查@GetMapping中url
  • 超时,application.yml配置ribbon
    在这里插入图片描述
  • 还报超时,访问不到,有可能跨域问题

在controller层增加 @CrossOrigin;

扫描二维码关注公众号,回复: 12655483 查看本文章
  • 没报错,就是返回结果不是期望的值,有可能程序中间有异常报错
    在这里插入图片描述日志如上,被调用的服务日志打印完全一致,结果不是想要的,最后发现是程序后续处理过程有bug…

6. 小技巧,可调试用

在url处配置服务的ip与端口,调试程序问题;

@FeignClient(name="hello-service-provider",url="http://xxx.xxx.xxx.xxx/")
public interface HelloServiceFeign {
    
    

    @RequestMapping(value = "/hello/demo/getHost", method = RequestMethod.GET)
    public String getHost(String name);

    @RequestMapping(value = "/hello/demo/postPerson", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public Person postPerson(String name);
}

参考

猜你喜欢

转载自blog.csdn.net/qq_40985985/article/details/112562756