SpringCloud中项目因为是一个个小的项目,所以项目(模块)之间怎样访问就成了问题?
有两种方式:
第一种:使用RestTemplate方式来调用服务端的接口
步骤1:在子模块的启动类同目录下加一个类,名字随便起(我的是RestConfig),代码附下
@Configuration
public class RestConfig {
@Bean
//@LoadBalanced //Ribbon负载均衡(手动开启)
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
步骤2:.在controller中注入RestTemplate
@Autowired
private RestTemplate restTemplate;
步骤3:在调用的方法内写下面的两行代码
String url="http://localhost:7201/provider/hello";---需要调用的模块路径
String message = restTemplate.getForObject(url, String.class);
然后调用即可
注:在启动类EurekaConsumer0