Feign负载均衡调用微服务

发布于 2021-04-17  1186 次阅读


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

1.@FeignClient("service1") 开启调用接口,service1为nacos中的服务名

@FeignClient("service1")
public interface FeginService {
    @GetMapping("/service1")
    String service1();

    @GetMapping("/service1/param")
    String serviceParam(@RequestParam("name") String name);
}

2.在spring中开启feign支持

@EnableFeignClients

3.使用feign调用

    @Autowired
    FeginService feginService;

    @GetMapping("/consumer")
    public String service1() {
        final String s = feginService.service1();
        System.out.println(s);
        System.out.println("consumer");
        return "consumer";
    }

4.feign传参注意

必须要写参数注解

    String serviceParam(@RequestParam("name") String name);

@RequestParam,@Requestbody,否则会出现传参失败或者get转为post的现象

2021年11月23日更新高级用法的参考链接:

https://www.huangchaoyu.com/2019/12/13/openFeign-%E7%94%A8%E6%B3%95/

https://segmentfault.com/a/1190000021508815


欢迎欢迎~热烈欢迎~