使用 Netflix Feign 客户端调用服务
1. 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>2. 使用 @EnableFeignClients 注解标注引导类
package com.study.cloudlearning;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@RefreshScope
@EnableFeignClients
@SpringBootApplication
public class CloudLearningApplication {
public static void main(String[] args) {
SpringApplication.run(CloudLearningApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}3. 定义接口
Last updated