1.创建一个服务提供者(因为springcloud依赖springboot,所以所有创建的项目均为springboot项目),在application.properties中配置端口信息(同时启动多个项目,防止端口冲突)
server.port=8080
2.编写服务提供者controller
@RestController
public class HelloController {
@RequestMapping("/web/hello")
public String hello(){
return "hello,springcloud";
}
}启动程序,可以正常访问
3.创建一个服务消费者项目(springboot项目),在application.properties中配置端口信息(同时启动多个项目,防止端口冲突)
server.port=8081
4.调用通过resttemplate,编写一个配置类
@Configuration//配置类(相当于mvc中的xml)
public class Config {
@Bean//等价xml中<bean></bean>
public RestTemplate restTemplate(){
return new RestTemplate();
}
}5.编写一个controller
@RestController
public class consumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/consumer/hello")
public String hello(){
return restTemplate.getForEntity("http://localhost/web/hello",String.class).getBody();
}
}6.浏览器中访问http://localhost:8081/consumer/hello进行测试
7.页面返回hello,springcloud证明调用成功
版权声明:本文为weixin_42885159原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。