RestTemplate体验远程调用

为SpringCloud学习作准备

RestTemplate使用的是http方式,缺点比RPC略慢,优点不限定语言,灵活。

Dubbo使用的是RPC(Remote Procedure Call远程过程调用),优点速度比http略快,缺点限定语言

idea创建maven父工程cloud-demo

不使用骨架

父工程pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.company</groupId>
<artifactId>cloud-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>user-service</module>
<module>consumer-demo</module>
</modules>
<packaging>pom</packaging>


<!--SpringBoot启动器作为父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<mapper.start.version>2.1.5</mapper.start.version>
<mysql.version>5.1.6</mysql.version>
</properties>
<!--在dependencyManagement标签中的内容,子工程需要引用才能用(无需指定版本)-->
<dependencyManagement>
<dependencies>
<!--springCloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--通用mapper启动器-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.start.version}</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--在dependencyManagement标签外的内容,子工程可以直接使用-->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

再创建两个子工程user-service和consumer-demo

在父工程上右键新建Module

不使用骨架,默认父工程就是cloud-demo

user-service

  pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud-demo</artifactId>
<groupId>com.company</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>user-service</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
    

    <!--通用Mapper,会依赖导入MyBatis启动器和jdbc启动器

    参考https://www.cnblogs.com/zou-rong/p/12566257.html
    -->
      <dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

  application.yaml配置文件

server:
servlet:
#拦截路径
context-path: /
#端口
port: 8088
logging:
level:
#日志级别,目录: 级别
com.company: debug

#配置com.zaxxer:HikariCP:3.4.1连接池
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
username: root
password: yourPassword

#整合mybatis
mybatis:
#启用别名
type-aliases-package: com.company.pojo

  UserApplication启动类

package com.company;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.company.mapper")
public class UserApplication {

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

  实体类

  

package com.company.pojo;

import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
@Data
@Table(name="tb_user")
public class User {
@Id//主键
@KeySql(useGeneratedKeys = true)//自动增长
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
//1男,2女
private Integer sex;
private Date birthday;
private String note;
private Date created;
// @Transient 忽略该字段
private Date updated;
}

  UserMapper

package com.company.mapper;

import com.company.pojo.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User>{
}

  UserServiceImpl(接口略)

package com.company.service.impl;

import com.company.mapper.UserMapper;
import com.company.pojo.User;
import com.company.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public User findOne(Long id) {
return userMapper.selectByPrimaryKey(id);
}

@Override
@Transactional
public void insert(User user) {
user.setUserName("test5");
user.setPassword("test5");
user.setName("ceshi5");
userMapper.insert(user);
// int i=10/0;
}
}

  UserController

package com.company.web;

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

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

@RequestMapping("/{id}")
public User getUser(@PathVariable("id") Long id){
return userService.findOne(id);
}

@RequestMapping("/add")
public String addUser(){
userService.insert(new User());
return "ok";
}
}

接下来是consumer-demo

  pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud-demo</artifactId>
<groupId>com.company</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>consumer-demo</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

  启动类ConsumerApplication

package com.company;

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 ConsumerApplication {

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

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

  ConsumerController

package com.company.controller;

import com.company.pojo.User;
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;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

@Autowired
private RestTemplate template;

@GetMapping("/{id}")
public User getUserById(@PathVariable("id")Long id){
return template.getForObject("http://localhost:8088/user/"+id,User.class);
}
}

  实体类

package com.company.pojo;

import lombok.Data;

import java.util.Date;

@Data
public class User {
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
//1男,2女
private Integer sex;
private Date birthday;
private String note;
private Date created;
private Date updated;
}

  接下来刷新maven

  

 

 

   右下角会提示

  

 

  点开

  

 

 

   很方便统一管理

  

 

 

  把两个子项目都启动,然后访问consumer-demo的http://localhost:8080/consumer/1

  id为1的用户就被user-service项目所查询出来

  

 

 

   与直接访问http://localhost:8088/user/1一致。

 

posted @ 2020-03-26 10:38  荒野猛兽  阅读(903)  评论(0编辑  收藏  举报