SpringCloud——接口互相调用(Feign)
这次我们使用一个简单点的接口
user中的test接口
//UserController
@RequestMapping(value="/test",method=RequestMethod.GET)
public String test() {
return "Hello World";
}
启动类BootMain.java添加注解
@EnableDiscoveryClient :启用服务注册与发现
@EnableFeignClients:启用feign进行远程调用
在Student中添加访问服务接口
@FeignClient(name=”user”) 中的name为你在注册中心注册的名字
@RequestMapping(value=”/test”,method=RequestMethod.GET) value 为你访问的目标接口
package com.springcloud.demo.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="user")
public interface UserTestFeignService {
@RequestMapping(value="/test",method=RequestMethod.GET)
public String userTestFeign();
}
接口中直接调用
//StudentApi.java
@Autowired
public UserTestFeignService userTestFeignService;
@RequestMapping(value="userTestFeign",method=RequestMethod.GET)
public String userTestFeign() {
return userTestFeignService.userTestFeign();
}
启动类BootMain.java添加注解
@EnableDiscoveryClient :启用服务注册与发现
@EnableFeignClients:启用feign进行远程调用
测试
还没有评论,来说两句吧...