栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用Springboot+RestTemplate进行远程调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Springboot+RestTemplate进行远程调用

RestTempalte是Spring Boot封装好的Http客户端工具。

  1. 添加服务提供者,提供Rest HTTP接口
  2. 添加服务调用者,通过RestTemplate访问服务提供者的接口
  3. 服务提供者和调用者都放到一个父工程中
搭建父工程

为了方便项目管理,这里把服务提供者和服务调用者放到同一个工程中
父工程不需要写代码,可以将src目录删除
添加Spring Boot依赖


    org.springframework.boot
    spring-boot-starter-parent
    2.2.8.RELEASE
     


    1.8


    
        org.springframework.boot
        spring-boot-starter
    
    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
        
            
                org.junit.vintage
                junit-vintage-engine
            
        
    
	

搭建服务提供者

1.添加提供者模块(ProviderMainApp)
添加依赖

> 
    
        org.springframework.boot
        spring-boot-starter-web
    

编写配置文件

server:
  port: 8001
spring:
  application:
    # 当前应用的服务名称(也叫服务ID)
    name: user-service
  jackson:
    time-zone: GMT+8`

启动类

package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}

添加实体

package com.itheima.user.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    private Date updateTime;
}

编写controller

package com.itheima.user.controller;

import com.itheima.user.entity.User;
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 java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {

    
    @GetMapping("/{id}")
    public User getById(@PathVariable("id") Long id){
        User user = new User(id,"tom",18,new Date());
        return user;
    }
}

启动项目,打开http://localhost:8001/user/1

消费服务者搭建

添加模块


    
        org.springframework.boot
        spring-boot-starter-web
    

添加配置文件

server:
  port: 9001
spring:
  application:
    # 当前应用的服务名称(也叫服务ID)
    name: service-consumer
  jackson:
    time-zone: GMT+8

添加启动类

package com.itheima;

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, args);
    }

}

添加实体

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    private Date updateTime;
}

编写controller

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

    @Autowired  // 注入RestTemplate模板工具
    private RestTemplate restTemplate;

    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id") Long id) {
        // 通过远程调用user-service提供的HTTP接口
        String url = "http://localhost:8001/user/" + id;
        // 第一个参数是接口调用地址  第二个参数是返回的类型
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }
}

访问http://localhost:9001/consumer/user/1

概括一下就是分布式服务必然要面临的问题:

  • 服务管理
    • 如何自动注册和发现服务
    • 如何实现服务状态监管
    • 如何实现服务动态路由
  • 服务如何实现负载均衡
  • 服务如何解决容灾问题
  • 服务如何实现统一配置

以上的问题,我们都将在Spring Cloud中得到解决。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号