第六节 RestTemplate+EurekaClient微服务调用

亮子 2021-06-08 23:41:02 17629 0 0 0

1、使用EurekaClient

1)、修改ArticleController类

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;
    }
}

2)、修改对比

图片alt

2、server-blog-user改造

1)、TbUser类增加新字段

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
    // ...
}

2)、修改UserController类

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();
    }
}

3)、编译运行

图片alt

3、集群部署server-blog-user服务

1)、集群架构

同一个业务,部署在多个服务器上。

图片alt

2)、分布式架构

一个业务分拆多个子业务,部署在不同的服务器上。

图片alt

3)、微服务架构

微服务的设计是为了不因为某个模块的升级和BUG影响现有的系统业务。微服务与分布式的细微差别是,微服务的应用不一定是分散在多个服务器上,它也可以是同一个服务器。

图片alt

分布式是否属于微服务? 答案是肯定的。微服务的意思也就是将模块拆分成一个独立的服务单元通过接口来实现数据的交互。分布式和微服的架构很相似,只是部署的方式不一样而已。

4)、运行多个server-blog-user实例

(1)修改IDEA运行配置

图片alt

(2)选中项目,右侧去掉Single Instance only选项

图片alt

(3)修改服务端口

修改服务端口分别为8000、8001、8002、8003并运行。

图片alt

(4)运行artice服务,注意观察article服务调用的是哪个微服务。

图片alt