Spring cloud微服务,Eureka,Feign完成post、get访问Demo
声明:本人自学整理材料,如有错误,勿喷,大家交流一下技术而已。
1.idea下maven项目添加Feign依赖。
<!--feign配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.配置一个微服务,spring cloud user feign服务客户端。
3.在主类上添加注解
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringBootCloudUserFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootCloudUserFeignApplication.class, args);
}
}
4.去调用另一个微服务客户端的服务。完成FeignClient的接口。
注意Feign的坑,就是在接口里面配置访问方法的是get时,不能使用 @GetMapping("/simple/{id}"),而必须使用 @RequestMapping(value = "/simple/{id}",method = RequestMethod.GET )
还有就是传参时@PathVariable必须填写value属性,例如:@PathVariable("id") String id
4.1新建一个微服务客户端完成,post个get的访问接口。
4.2配置文件配置的微服务访问名称
spring:
application:
name: spring-boot-cloud-user #注册的服务名称
4.3完成Feign接口里面访问的俩个方法,post和get方法。
5.完成一个Controller,注入配置好的Feign客户端。
6.去调用你要访问的另一个微服务
7.启动注册中心,启动Feign服务客户端,和启动User客户端。
8.访问http://localhost:8761/查看注册中心已经注册的服务。
9.然后通过Feign客户端访问,User客户端里面配置的俩个访问方法post,get。
post: http://localhost:8091/getUser?id=1&name=shenyu
get:http://localhost:8091/simple/2
本次示例结束,希望对大家有助。