当前位置 > it书童 > java > 正文

微服务场景模拟

java it书童 2020-09-11 09:42:57 0赞 0踩 54阅读 0评论

首先,我们需要模拟一个服务调用的场景,搭建两个工程:service-provider(服务提供方)和 service-customer(服务调用方)

服务提供方:使用mybatis操作数据库,实现对数据的增删改查;并对外提供rest接口服务。

服务消费方:使用restTemplate远程调用服务提供方的rest接口服务,获取数据。

服务提供者

我们新建一个项目:service-provider,对外提供根据id查询用户的服务。

借助于Spring提供的快速搭建工具:用脚手架与 maven 的区别,省去了手动加依赖的步骤

next-->填写项目信息:

next --> 添加web依赖:

根据需求,选择要添加的依赖

image-20200910202848781

生成的项目结构,已经包含了 springboot 引导类,pom.xml 也自动引入了相关依赖

当然,因为要使用通用mapper,所以我们需要手动加一条依赖:

<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.0.4</version>
</dependency>

用脚手架搭建项目比用 maven 要便捷得多

编写代码

属性文件,这里我们采用了yaml语法,而不是properties:

application.yml

server:
    port: 8082
spring:
    datasource:
        url: jdbc:mysql:///test
        username: root
        password: Mac_2020Pwd_Root
mybatis:
    type-aliases-package: com.itshutong.service.pojo

实体类

package com.itshutong.service.pojo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String username;

    private String sex;

    private Date birthday;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", sex='" + sex + '\'' +
            ", birthday=" + birthday +
            '}';
    }
}

UserMapper

package com.itshutong.service.mapper;

import com.itshutong.service.pojo.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {
}

UserService

package com.itshutong.service.service;

import com.itshutong.service.mapper.UserMapper;
import com.itshutong.service.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public User queryUserById(Long id) {
        return this.userMapper.selectByPrimaryKey(id);
    }
}

UserController

添加一个对外查询的接口:

package com.itshutong.service.controller;

import com.itshutong.service.pojo.User;
import com.itshutong.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("{id}")
    public User queryUserById(@PathVariable("id")Long id) {
        return this.userService.queryUserById(id);
    }
}

启动并测试

启动项目,访问接口:http://localhost:8082/user/1

image-20200911093001264

服务调用者

搭建 service-customer 服务消费方工程。

创建工程步骤与上面类似,这里不再赘述,需要注意的是,我们调用 service-provider 的解耦获取数据,因此不需要mybatis相关依赖了,只需要 web 依赖即可

编写代码

首先在引导类中注册RestTemplate

package com.itshutong.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ItshutongServiceCustomerApplication {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(ItshutongServiceCustomerApplication.class, args);
    }

}

编写配置(application.yml):

server:
    port: 85

编写UserController:

package com.itshutong.service.controller;

import com.itshutong.service.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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;

@Controller
@RequestMapping("customer/user")
public class UserController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping
    @ResponseBody
    public User queryUserById(@RequestParam("id")Long id) {
        return this.restTemplate.getForObject("http://localhost:8082/user/" + id, User.class);
    }
}

pojo对象(User):

package com.itshutong.service.pojo;

import java.util.Date;

public class User {
    private Long id;

    private String username;

    private String sex;

    private Date birthday;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", sex='" + sex + '\'' +
            ", birthday=" + birthday +
            '}';
    }
}

启动测试

因为我们没有配置端口,那么默认就是8080,我们访问:http://localhost:85/customer/user?id=1

image-20200911093305045

一个简单的远程服务调用案例就实现了

但这种粗笨的方式有很大问题,下一篇将进行说明

转载须注明出处:https://www.itshutong.com/articles/704/microservice-scenario-simulation
关于我
一个文科出身的程序员,追求做个有趣的人,传播有价值的知识,微信公众号主要分享读书思考心得,不会有代码类文章,非程序员的同学请放心订阅
发表评论
我有句话,不知当讲不当讲?
要讲之前请先 登录