美文网首页
OpenFegin远程调用入门案例

OpenFegin远程调用入门案例

作者: CodeYang | 来源:发表于2021-08-28 17:48 被阅读0次

一、Pom 引入 OpenFegin 依赖

 <!-- 使用 OpenFegin 进行远程调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

二、创建接口 OpenFeginService 接口

/**
 * 使用 OpenFegin 进行远程调用
 */
@Component
@FeignClient(value = "CLOUD-PROVIDER") //服务名
public interface OpenFeginService {
   @GetMapping(value = "/provider/hello/{name}")
    public String hello(@PathVariable("name") String name);
}

三、创建 OpenFeginController 编写调用接口

@RestController
public class OpenFeginController {

@Resource
private OpenFeginService service;

@GetMapping("/consumer/openfegin/{name}")
public String openFegin(@PathVariable("name") String name){
    return service.hello(name)+"| OpenFegin";
}

}

四、启动类开启 OpenFegin

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients //开启使用 哦OpenFegin
public class ConsumerApp81 {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApp81.class);
    }
}

五、调用测试,OpenFegin 默认集成了 Ribbon 所以采用默认负载均衡机制:轮询

第一次调用.png 第二次调用.png

相关文章

网友评论

      本文标题:OpenFegin远程调用入门案例

      本文链接:https://www.haomeiwen.com/subject/ophhiltx.html