package com.mazong.serverblogarticle.controller;
import com.mazong.serverblogarticle.entity.TbArticle;
import com.mazong.serverblogarticle.mapper.IDbArticleMapper;
import com.mazong.serverbloguser.entity.TbUser;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/")
public class ArticleController {
@Autowired
private IDbArticleMapper iDbArticleMapper;
@Autowired
HttpServletRequest request;
@Autowired
RestTemplate restTemplate;
@Qualifier("eurekaClient")
@Autowired
EurekaClient eurekaClient;
@RequestMapping(value = "/getArticle")
@ResponseBody
public Object getArticle(@RequestParam("id") Integer id) {
// http://localhost:9000/getArticle?id=2
Map<String, Object> resultMap = new HashMap<>();
TbArticle article = iDbArticleMapper.getArticle(id);
resultMap.put("article", article);
if(article != null) {
InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("server-blog-user", false);
Map urlVariables = new HashMap();
urlVariables.put("hostName", instanceInfo.getHostName());
urlVariables.put("port", instanceInfo.getPort());
TbUser user = restTemplate.getForObject("http://{hostName}:{port}/getUser?id=" + article.getUid(), TbUser.class, urlVariables);
resultMap.put("user", user);
}
return resultMap;
}
}
package com.mazong.serverbloguser.entity;
public class TbUser {
private int id;
private String UserName;
private int Age;
private String CreateTime;
private String ServerInfo;
// get
// set
// ...
}
package com.mazong.serverbloguser.controller;
import com.mazong.serverbloguser.entity.TbUser;
import com.mazong.serverbloguser.mapper.IDbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/")
public class UserController {
@Autowired
private IDbUserMapper iUserDbMapper;
@Autowired
HttpServletRequest request;
@Value("${spring.application.name}")
private String serverName;
@Value("${server.port}")
private int serverPort;
@RequestMapping(value = "/getUser")
@ResponseBody
public Object getUser(@RequestParam("id") Integer id) {
// http://localhost:8000/getUser?id=2
TbUser user = iUserDbMapper.getUserById(id);
user.setServerInfo(serverName+":"+serverPort);
return user;
}
@RequestMapping(value = "/addUser")
@ResponseBody
public String addUser(@RequestParam("userName") String userName, @RequestParam("age") int age) {
// http://localhost:8000/addUser
TbUser user = new TbUser();
user.setUserName(userName);
user.setAge(age);
int id = iUserDbMapper.addUser(user);
System.out.println("addUser="+user.getId());
return user.toString();
}
@RequestMapping(value = "/delUser")
@ResponseBody
public String getUserById(@RequestParam("id") Integer id) {
// http://localhost:8000/getUserById?userId=2
iUserDbMapper.delUser(id);
return String.format("delete id=%d", id);
}
@RequestMapping(value = "/updateUser")
@ResponseBody
public String updateUser(@RequestParam("id") int id, @RequestParam("userName") String userName, @RequestParam("age") int age) {
// http://localhost:8000/updateUser
TbUser user = new TbUser();
user.setId(id);
user.setUserName(userName);
user.setAge(age);
iUserDbMapper.updateUser(user);
System.out.println("updateUser="+user.getId());
return user.toString();
}
}
同一个业务,部署在多个服务器上。
一个业务分拆多个子业务,部署在不同的服务器上。
微服务的设计是为了不因为某个模块的升级和BUG影响现有的系统业务。微服务与分布式的细微差别是,微服务的应用不一定是分散在多个服务器上,它也可以是同一个服务器。
分布式是否属于微服务? 答案是肯定的。微服务的意思也就是将模块拆分成一个独立的服务单元通过接口来实现数据的交互。分布式和微服的架构很相似,只是部署的方式不一样而已。
修改服务端口分别为8000、8001、8002、8003并运行。