Java B2B2C多用户商城 springboot架构-SpringCloud服务相互调用RestTemplate

Springcloud中的服务消费,就需要我们服务之前相互发请求了。之前我们都是想着用http请求相关的交互,用的比较多的是apache httpcomponents ,现在springboot提供了RestTemplate更高级别的方法来...

Springcloud中的服务消费,就需要我们服务之前相互发请求了。之前我们都是想着用http请求相关的交互,用的比较多的是apache httpcomponents ,现在springboot提供了RestTemplate更高级别的方法来满足我们的功能。

需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求 :二一四七七七五六三三

RestTemplate 的类路径
org.springframework.web.client.RestTemplate
其实我们之前就已经集成过了,在spring-boot-starter-web中已经有了它的依赖。

Maven

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

Biz服务

@RestController
@RequestMapping("index")
public class IndexController {
 
    @Resource
    private UserService userService;
 
    @RequestMapping("findUserMenuList")
    public Object findUserMenuList(){
        return userService.findUserMenuList("李文涛");
    }
}

Biz-2服务具体调用如下

@RestController
@RequestMapping("index")
public class IndexController {
 
    @Autowired
    private RestTemplate restTemplate;
 
    String host = "http://SERVICE-BIZ"; //biz服务的名称,大小写忽略
 
    @RequestMapping("index")
    public  Object index(){
        String url = host+"/index/findUserMenuList";
        Map<String,Object> uriVariables = new HashMap<>();
        return restTemplate.getForObject(url,Object.class);
    }
}

Biz-2调用的前提是,注册中心启动了,Biz服务也启动了,这样就OK了。

springboot微服务多用户商城系统java_代码开源_B2B电商系统_B2C电商系统


条评论

请先 登录 后评论
不写代码的码农
IT 小兵

355 篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除