这一篇主要使用feign实现内部调用服务,所有连接统一经网关转发到相应的服务,连接经过网关转发到oauth服务然后用feign调用user服务并返回。
zuul----->oauth-------->user
目的:
访问127.0.0.1:9999/oauth/getFeignHello 接口,经过getFeignHello接口远程调用user服务的hello接口,返回user/hello字符串
因为是从oauth调用user服务,所以要在oauth的pom中添加feign jar包
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
在启动类上添加@EnableFeignClients
再oauth服务中添加service包,然后新建接口helloService接口
@FeignClient(value = "vcloud-user-server")
public interface HelloService {
@GetMapping("/hello")
public String getHello();
}
@FeignClient中的value是表示要远程调用的服务名,这里是vcloud-user-server,因为我要调用user服务
@GetMapping("/hello") 调用的user接口。
然后在controller中调用刚刚上面写的接口
就可以实现 127.0.0.1:9999/oauth/getFeignHello,调用远程服务了。启动全部服务
测试
调用远程服务成功