zoukankan      html  css  js  c++  java
  • 作为消费者访问提供者提供的功能(eureka的铺垫案例)

    1. 实体类、提供者的创建如本随笔者的Euraka适合初学者的简单小demo中有所展示

    2. 创建子工程作为消费者

    (1) 添加依赖:切记引入实体类的依赖

    <dependencies>
            <dependency>
                <groupId>com.offcn</groupId>
                <artifactId>microservice_cloud_02_api</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    

     (2) application.yml文件中设置端口号为80

      (3) 创建配置类

    @Configuration
    public class ConfigBean {
    
        @Bean
        public RestTemplate getConfigBean(){
            return new RestTemplate();
        }
    }
    

     (4) 创建controller类,实现消费者访问提供者功能的功能

    @RestController
    public class ProductController_Customer {
    
        @Autowired
        private RestTemplate restTemplate;
    
        private static final String REST_URL_PREFIX = "http://localhost:8001";
    
        @RequestMapping(value = "/customer/product/add")
        public boolean add(Product product){
            return restTemplate.postForObject(REST_URL_PREFIX+"/product/add",product,Boolean.class);
        }
    
        @RequestMapping(value = "/customer/product/get/{id}")
        public Product get(@PathVariable("id") Long id){
            return restTemplate.getForObject(URI.create(REST_URL_PREFIX+"/product/get/"+id),Product.class);
        }
    
        @RequestMapping(value = "/customer/product/get/list")
        public List<Product> list(){
            return restTemplate.getForObject(URI.create(REST_URL_PREFIX+"/product/get/list"),List.class);
        }
    }
    

     (5) 创建启动类

    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class CustomerApplication {
        public static void main(String[] args) {
            SpringApplication.run(CustomerApplication.class,args);
        }
    }
    

     因为这个实现在功能对且复杂的情况下,而且男用户量多的时候会配置很多的配置类,太过繁琐,以至于用了eureka来替代了它

  • 相关阅读:
    题解+补题
    信息安全导论期末复习
    Codeforces Round #104 (Div.2)
    中国计量大学现代科技学院第四届“中竞杯”程序设计校赛(同步赛)
    第一章练习-1
    【练习】购物车程序
    【转】Python中设置输出文字的颜色
    字符串,列表,元组,字典间的互相转换
    【转】Python Enhancement Proposal #8【PEP8】
    【转】pycharm的一些快捷键
  • 原文地址:https://www.cnblogs.com/xiaoyuer0506/p/11817425.html
Copyright © 2011-2022 走看看