• Eureka多服务调用


     所以我们需要写入公共模块

    自己随便写入然后在pom中添加

    并且在订单中调用用户服务需要使用restTemlate这个

    UserController

    package cn.jiedada.web.controller;
    
    import cn.jiedada.domain.User;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/provider")
    public class UuserController {
        @RequestMapping("/")
        public String home() {
            return "Hello world";
        }
    
        @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
        public User getUserById(@PathVariable("id") Long id){
            System.out.println("这是user中的getUser方法被调用");
            return new User(id,"user");
        }
    }

    OrderController

    package cn.jiedada.web.controller;
    
    import cn.jiedada.domain.User;
    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;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RequestMapping("/customer")
    public class OrderController {
        @Autowired
        private RestTemplate restTemplate;
    
        @RequestMapping("/")
        public String home() {
            return "Hello world";
        }
        /*这个方法是我们去掉user_service_2000中的数据
        所以需要使用restTemplate的类
        * */
        @RequestMapping("/order/{id}")
        public User getUserById(@PathVariable("id")Long id){
            //拼接字符串
            String url="http://localhost:2000/provider/user/"+id;
            //通过这个方法调用我们的
            return restTemplate.getForObject(url,User.class);
        }
    }

    RestTemplate

    package cn.jiedada.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    @Configuration
    public class WebConfig {
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }

    出现这个页面

  • 相关阅读:
    PHP延迟静态绑定
    PHP SPL神器实现堆排序
    魔术方法__sleep 和 __wakeup
    SPL 笔记
    PHP中对象的深拷贝与浅拷贝
    PHP的垃圾回收机制详解
    深入理解PHP中赋值与引用
    xdebug安装及使用小结
    工作中碰到的一个问题(cookie相关)
    分享一个解析XML成为php数组的方法
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11925876.html
Copyright © 2020-2023  润新知