SpringCloud-day02-服务消费者项目建立

4.4ticket-consumer-80服务消费者项目建立

 新建一个服务器提供者module子模块,类似前面建的common公共模块,名称是 ticket-consumer-80

 pom.xml修改:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>wfd360-station</artifactId>
        <groupId>com.wfd360.station</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ticket-consumer-80</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.wfd360.station</groupId>
            <artifactId>common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>
View Code

 

application.yml配置文件:

server:
  port: 80
  context-path: /

SpringCloudConfig配置类:

package com.wfd360.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 06/29 10:23
 * @description
 */

@Configuration
public class SpringCloudConfig {
    /**
     * 调用服务模版
     *
     * @return
     */
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
View Code

 

主要是定义一个bean RestTemplate对象; springcloud消费者,服务提供者之间的交互是http rest方式,比dubbo rpc方式更加灵活方便点;

TicketConsumerController类:

package com.wfd360.controller;

import com.wfd360.model.Ticket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 06/29 10:25
 * @description
 */
@Controller
@RequestMapping("/ticket")
public class TicketConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    private final String URL = "http://localhost:6001";

    /**
     * 添加或者修改车票信息
     *
     * @param ticket
     * @return
     */
    @PostMapping(value = "/save")
    @ResponseBody
    public boolean save(Ticket ticket) {
        return restTemplate.postForObject(URL + "/ticket/save", ticket, Boolean.class);
    }

    /**
     * 查询车票信息
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    @GetMapping(value = "/list")
    @ResponseBody
    public List<Ticket> list() {
        return restTemplate.getForObject(URL + "/ticket/list", List.class);
    }

    /**
     * 根据id查询车票信息
     *
     * @return
     */
    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public Ticket get(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(URL + "/ticket/get/" + id, Ticket.class);
    }

    /**
     * 根据id删除车票信息
     *
     * @return
     */
    @GetMapping(value = "/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable("id") Integer id) {
        try {
            restTemplate.getForObject(URL + "/ticket/delete/" + id, Boolean.class);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
View Code

 

启动类TicketConsumerApplication_80:

package com.wfd360;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 06/29 10:28
 * @description
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class TicketConsumerApplication_80 {
    public static void main(String[] args) {
        SpringApplication.run(TicketConsumerApplication_80.class, args);
    }
}
View Code

我们测试下:(先启动ticket-provider-6001,在启动ticket-consumer-80)

http://localhost/ticket/list

到这里一个最基本的微服务架构完成,后面围绕着这个基本架构慢慢演变成生产上的架构.

springCloud课程与代码下载:https://www.cnblogs.com/newAndHui/p/13210228.html

posted @ 2019-03-28 15:50  李东平|一线码农  阅读(504)  评论(0编辑  收藏  举报