(3)微服务之间项目调用

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
简介: (3)微服务之间项目调用

采用RestTemplate方式调用:
(1)添加RestTemplate到Spring容器中:


import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * Created by lj on 2020/4/26.
 */
@Configuration
public class RestConfiguration {

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

(2)使用:


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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Random;

/**
 * Created by lj on 2020/4/25.
 */

@RestController
@RequestMapping(value = "/User",produces = "application/json;charest=UTF-8")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/getUserInfo/{id}")
    public String getUserInfo(@PathVariable("id") Integer id){
        return  id+":用户信息返回成功";
    }


    @RequestMapping(value = "/getOrderByID/{id}")
    public String getOrderByID(@PathVariable("id") Integer id){
        Random random = new Random();
        //com-eureka-client001为spring的服务名
        String result = restTemplate.getForObject("http://com-eureka-client001/Order/getOrderInfo/"+random.nextInt(100),String.class);
        //String result = restTemplate.getForObject("http://127.0.0.1:8088/Order/getOrderInfo/"+random.nextInt(100),String.class);
        return  result;
    }
}
相关文章
|
5月前
|
Dubbo Java 应用服务中间件
微服务框架(十)Maven Archetype制作Dubbo项目原型
  此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。   本文为Maven Archetype的制作及使用,使用archetype插件制作Dubbo项目原型
|
16天前
|
消息中间件 监控 开发工具
微服务(三)-实现自动刷新配置(不重启项目情况下)
微服务(三)-实现自动刷新配置(不重启项目情况下)
|
5月前
|
微服务
jeecg微服务项目调用接口报错Token验证失效的解决方法
jeecg微服务项目调用接口报错Token验证失效的解决方法
|
5月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
380 0
|
3月前
|
负载均衡 Java 开发者
如何在Spring Boot项目中实现微服务架构?
如何在Spring Boot项目中实现微服务架构?
|
3月前
|
消息中间件 负载均衡 Java
最容易学会的springboot gralde spring cloud 多模块微服务项目
最容易学会的springboot gralde spring cloud 多模块微服务项目
|
5月前
|
前端开发 JavaScript 搜索推荐
微服务项目|基于SpringCloud微服务技术的旅游信息平台的设计与实现
微服务项目|基于SpringCloud微服务技术的旅游信息平台的设计与实现
272 1
|
5月前
|
监控 数据可视化 安全
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
环境实时数据、动态监测报警,实时监控施工环境状态,有针对性地预防施工过程中的环境污染问题,打造文明生态施工,创造绿色的生态环境。
57 0
智慧工地SaaS可视化平台源码,PC端+APP端,支持二开,项目使用,微服务+Java++vue+mysql
|
5月前
|
Java Maven 微服务
微服务项目-将普通文件夹设为模块与添加services窗口
微服务项目-将普通文件夹设为模块与添加services窗口
29 0
|
5月前
|
Cloud Native Dubbo Java
如何确定微服务项目中Spring Boot、Spring Cloud、Spring Cloud Alibaba三者之间的版本
如何确定微服务项目中Spring Boot、Spring Cloud、Spring Cloud Alibaba三者之间的版本
120 0