zoukankan      html  css  js  c++  java
  • 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();
        }
    }

    出现这个页面

  • 相关阅读:
    LeetCode#160-Intersection of Two Linked Lists-相交链表
    LeetCode#2-Add Two Numbers-两数相加
    LeetCode#141-Linked List Cycle-环形链表
    LeetCode#66-Plus One-加一
    LeetCode#35-Search Insert Position-搜索插入位置
    LeetCode#203-Remove Linked List Elements-移除链表元素
    基姆拉尔森公式
    [leetcode] 树(Ⅲ)
    常用算法合集(一)
    离散数学 II(最全面的知识点汇总)
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11925876.html
Copyright © 2011-2022 走看看