1、新建module:test-consumer
2、加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
3、application.properties配置
server.port=2222
spring.application.name=feign-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
4、启动类加上注解
@EnableFeignClients
@EnableDiscoveryClient
5、新建TestService,通过feign调提供者接口
@FeignClient("test-service")
@Service
public interface TestService {
@GetMapping("/see")
String test(@RequestParam("name") String name);
}
6、新建测试类ConsumerController
@RestController
public class ConsumerController {
private Logger logger = LoggerFactory.getLogger(ConsumerController.class);
@Autowired
TestService testService;
@GetMapping("/hello")
public String test(@RequestParam String name){
logger.info("消费服务启动{}");
return testService.test(name);
}
}
7、访问测试,成功