一. Consumer服务:
1.添加必要依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.3</version>
</dependency>
2. 向启动类中添加@EnableFeignClients注解
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class MyConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(MyConsumerApplication.class, args);
}
}
3. 添加使用Feign进行远程调用的Service接口
@FeignClient(name = "my-producer" )
public interface HelloFeignService {
@GetMapping("/hello")
public String hello();
}
4. 还可以像普通服务一样添加一个Controller层
@RestController
public class HelloController {
@Autowired
private HelloFeignService helloFeignService ;
@GetMapping("/feign/call")
public String call() {
return helloFeignService .hello();
}
}