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

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

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

 pom.xml修改:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>wfd360-station</artifactId>
 7         <groupId>com.wfd360.station</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>microservice-station-consumer-80</artifactId>
13     <dependencies>
14         <dependency>
15             <groupId>com.wfd360.station</groupId>
16             <artifactId>microservice-common</artifactId>
17             <version>${project.version}</version>
18         </dependency>
19         <dependency>
20             <groupId>org.springframework.boot</groupId>
21             <artifactId>spring-boot-starter-web</artifactId>
22         </dependency>
23         <!-- 修改后立即生效,热部署 -->
24         <dependency>
25             <groupId>org.springframework</groupId>
26             <artifactId>springloaded</artifactId>
27         </dependency>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-devtools</artifactId>
31         </dependency>
32     </dependencies>
33 
34 </project>
View Code

application.yml配置文件:

server:
  port: 80
  context-path: /

SpringCloudConfig配置类:

 1 package com.wfd360.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.client.RestTemplate;
 6 
 7 /**
 8  * Created by 姿势帝-博客园 on 2019/3/26.
 9  * 欢迎添加笔者wx(851298348)共同探讨、学习!
10  *
11  * springCloud相关配置
12  */
13 @Configuration
14 public class SpringCloudConfig {
15     /**
16      * 调用服务模版
17      * @return
18      */
19     @Bean
20     public RestTemplate getRestTemplate(){
21         return new RestTemplate();
22     }
23 }
View Code

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

TicketConsumerController类:

 1 package com.wfd360.controller;
 2 
 3 import com.wfd360.model.Ticket;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.*;
 7 import org.springframework.web.client.RestTemplate;
 8 
 9 import java.util.List;
10 
11 /**
12  * Created by 姿势帝-博客园 on 2019/3/26.
13  * 欢迎添加笔者wx(851298348)共同探讨、学习!
14  */
15 
16 /**
17  * 知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
18  */
19 @Controller
20 @RequestMapping("/ticket")
21 public class TicketConsumerController {
22     @Autowired
23     private RestTemplate restTemplate;
24 
25     /**
26      * 添加或者修改车票信息
27      *
28      * @param ticket
29      * @return
30      */
31     @PostMapping(value = "/save")
32     @ResponseBody
33     public boolean save(Ticket ticket) {
34         System.out.println("======su=====save=========");
35         return restTemplate.postForObject("http://localhost:1001/ticket/save", ticket, Boolean.class);
36     }
37 
38     /**
39      * 查询车票信息
40      *
41      * @return
42      */
43     @SuppressWarnings("unchecked")
44     @GetMapping(value = "/list")
45     @ResponseBody
46     public List<Ticket> list() {
47         return restTemplate.getForObject("http://localhost:1001/ticket/list", List.class);
48     }
49 
50     /**
51      * 根据id查询车票信息
52      *
53      * @return
54      */
55     @GetMapping(value = "/get/{id}")
56     @ResponseBody
57     public Ticket get(@PathVariable("id") Integer id) {
58         return restTemplate.getForObject("http://localhost:1001/ticket/get/" + id, Ticket.class);
59     }
60 
61     /**
62      * 根据id删除车票信息
63      *
64      * @return
65      */
66     @GetMapping(value = "/delete/{id}")
67     @ResponseBody
68     public boolean delete(@PathVariable("id") Integer id) {
69         try {
70             restTemplate.getForObject("http://localhost:1001/ticket/delete/" + id, Boolean.class);
71             return true;
72         } catch (Exception e) {
73             return false;
74         }
75     }
76 }
View Code

启动类TicketConsumerApplication_80:

 1 package com.wfd360;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 6 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 7 
 8 /**
 9  * Created by 姿势帝-博客园 on 2019/3/26.
10  * 欢迎添加笔者wx(851298348)共同探讨、学习!
11  */
12 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
13 public class TicketConsumerApplication_80 {
14     public static void main(String[] args) {
15         SpringApplication.run(TicketConsumerApplication_80.class, args);
16     }
17 }
View Code

这里的话 加了特殊配置 排除了 数据源注入,不加的话 可能会因为找不到数据库配置会报错;

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

http://localhost/ticket/list

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

 源码下载地址:https://github.com/bangbangw/wfd360-station (选择V2版本)

猜你喜欢

转载自www.cnblogs.com/newAndHui/p/10615525.html
今日推荐