从应用网关调用微服务
微服务端
在Eureka中注册服务名为Server-IFS
获取根据用户id获取用户的职员信息
/ifs/user/getStaffByCurrentUser
内部调试用的带请求头的curl请求:
curl -H 'Content-Type: application/json' -H 'Db-Suffix: syt230001' http://xmsaas-sys:9598/ifs/user/getStaffByUserName
java调用端
pom.xml,添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
添加服务接口
在package sy.xmsaas.gbs.service; 该服务可以带请求头参数
//在生产环境根据微服务名称方式
@FeignClient("server-ifs")
//在本地环境根据服务的url方式
//@FeignClient(value = "IfsService", url="http://192.168.0.186:88/xmsaas-ifs/")
public interface IfsService {
@GetMapping("/ifs/user/getStaffByCurrentUser")
public String getStaffByCurrentUser(
@RequestHeader("Content-Type") String h1,
@RequestHeader("Db-Suffix") String h2,
@RequestHeader("Author") String h3
);
}
应用模块入口添加注释
ApiGbsApplication中,添加@EnableFeignClients(basePackages = "sy.xmsaas.gbs.service")
@SpringBootApplication
@EnableFeignClients(basePackages = "sy.xmsaas.gbs.service")
@EnableZuulProxy
@EnableEurekaClient
public class ApiGbsApplication {
调用服务
在controller的LoginGbs中
//引用微服务接口的Bean
@Resource
IfsService ifsService;
//用于vue前端检查用户是否登录
@GetMapping("/user/")
public LoginUserOutput getUserInfo(HttpServletResponse response) throws IOException, JSONException {
。。。。
//设置请求头参数变量
String h1="application/json;charset=UTF-8";
String h2=currUser.getTenant().getTntCode();
String h3= URLEncoder.encode(json,"UTF-8");
//调用微服务请求职员信息
Object ifs1=ifsService.getStaffByCurrentUser(h1,h2,h3);
System.out.println(ifs1.toString());
。。。。
}