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、访问测试,成功

本文介绍了如何创建一个Spring Cloud微服务消费者模块,包括添加Feign依赖、配置Eureka、定义服务接口和测试类,展示了如何通过Feign调用其他服务提供者的方法并进行测试。
830

被折叠的 条评论
为什么被折叠?



