SpringCloud利用restTemplate实现服务间的远程调用

SpringCloud官网:https://spring.io/projects/spring-cloud

SpringCloud中文网: https://springcloud.cc/

SpringCloud的一系列产品很多都是接用别的公司的产品,其中借用netflix公司的最多,将许多别家的微服务产品全部整合到Spring中就成了SpringCloud

netflix官网:https://github.com/Netflix


restTemplate底层使用http调用方式,从一个服务中远程调用另一个服务的方法。

demo地址:https://github.com/hanhanhanxu/cloud-demo

结构:

V99rh8.png

一、pom

父工程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>cn.itcast.demo</groupId>
    <artifactId>cloud-demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <modules>
        <module>user-service</module>
        <module>consumer-demo</module>
    </modules>
    <packaging>pom</packaging> <!--父工程打包方式pom-->

    <!--引入springboot父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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.starter.version>2.0.3</mapper.starter.version>
        <mysql.version>5.1.32</mysql.version>
    </properties>

    <!--父工程的依赖管理,子工程引入一个是一个-->
    <dependencyManagement>
        <dependencies>
            <!--springcloud   spring-cloud-dependencie是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.starter.version}</version>
            </dependency>
            <!--mysql驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--版本管理 只要是子工程,不用引入都会有-->
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

user-service用户服务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">
    <parent>
        <artifactId>cloud-demo</artifactId>
        <groupId>cn.itcast.demo</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>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>

consemer-service顾客服务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">
    <parent>
        <artifactId>cloud-demo</artifactId>
        <groupId>cn.itcast.demo</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>

二、user-service

结构:

V9CMvQ.png

application.yml:

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
mybatis:
  type-aliases-package: cn.itcast.user.pojo

User:

package cn.itcast.user.pojo;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Data
@Table(name = "user")
public class User {

    @Id
    //@KeySql(useGeneratedKeys = true) 主键自增
    private String id;

    private String name;

    private String password;

    private String perms;

    @Transient //瞬时,不需要持久化到数据库
    private String other;
}

UserMapper:

package cn.itcast.user.mapper;

import cn.itcast.user.pojo.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {
}

UserService:

package cn.itcast.user.service;

import cn.itcast.user.mapper.UserMapper;
import cn.itcast.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User queryById(String id){return userMapper.selectByPrimaryKey(id);}
}

UserController:

package cn.itcast.user.web;

import cn.itcast.user.pojo.User;
import cn.itcast.user.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 queryById(@PathVariable String id){
        return userService.queryById(id);
    }
}

UserApplication:

package cn.itcast;

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

@SpringBootApplication
@MapperScan("cn.itcast.user.mapper")
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class);
    }
}

启动后 可在浏览器访问:http://localhost:8081/user/111

V9PmZR.md.png

三、consumer-service

结构:

V9P1zD.png

User:

package cn.itcast.consumer.pojo;

import lombok.Data;

@Data
public class User {

    private String id;

    private String name;

    private String password;

    private String perms;

    private String other;
}

ConsumerController:

package cn.itcast.consumer.web;

import cn.itcast.consumer.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 restTemplate;

    @GetMapping("/{id}")
    public User queryById(@PathVariable String id){
        String url = "http://localhost:8081/user/" + id;
        User user = restTemplate.getForObject(url,User.class);
        return user;
    }
}

ConsumerAplication:

package cn.itcast;

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 ConsumerAplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerAplication.class);
    }

    @Bean
    public RestTemplate restTemplate(){
        // Spring自带的RestTemplate ; 默认的RestTemplate,底层是走JDK的URLConnection方式。
        return new RestTemplate();
    }
}

将两个服务分别启动,浏览器访问http://localhost:8080/consumer/111

V9Pgwn.md.png

此服务访问了ConsumerController中的queryById,此方法中利用restTemplate远程调用UserController中的queryById方法。

三、搭建工程的简介

1、新建maven工程 cloud-demo:

V9kkz8.md.png

2、在cloud-demo上右键-New-Module,新建模块 user-service:

V9kVsg.md.png

V9kmZj.md.png

V9kEQS.md.png

V9kFRf.md.png

3、consumer-service同上

insist,on the road
-------------本文结束感谢您的阅读-------------