SpringCloud 中Feign的简单使用

  1. 导入依赖

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
  2. 启动类添加注解@EnableFeignClients

    @SpringBootApplication
    @EnableFeignClients
    public class ClientdemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ClientdemoApplication.class, args);
        }
    
    }
    
  3. 创建API接口

    //方式一:配置url
    //@FeignClient(name = "eurekaserver",url = "http://eureka1.com:8081")
    //方式二:配置name,此name要与要与服务名的纯小写相同
    @FeignClient(name = "eurekaserver")
    public interface FeignTest {
    
        @GetMapping(path = "/shello")
        String shello();
    
    }
    
  4. 注入调用

    @Autowired
    private FeignTest feignTest;
    
    public Object service4() {
        String hello = feignTest.shello();
        System.out.println(hello);
        return "service3: " + hello;
    }
    
posted @ 2022-03-07 18:04  叕叕666  阅读(37)  评论(0)    收藏  举报