服务提供者的端口为:8081
服务调用者的端口为:8080
在服务调用者中的controller如下:
package cn.itcast.springcloud.controller;
import cn.itcast.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@Controller
@RequestMapping("consumer/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping
@ResponseBody
public User queryById(@RequestParam("id") Long id){
User user=this.restTemplate.getForObject("http://localhost:8080/user/"+id,User.class);
return user;
}
}
在url中的端口应该改为8081