SpringCloud-day02-服务消费者项目建立
4.4microservice-ticket-consumer-80服务消费者项目建立
我们新建一个服务器提供者module子模块,类似前面建的common公共模块,名称是 microservice-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>microservice-station-consumer-80</artifactId>
<dependencies>
<dependency>
<groupId>com.wfd360.station</groupId>
<artifactId>microservice-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>
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; /**
* Created by 姿势帝-博客园 on 2019/3/26.
* 欢迎添加笔者wx(851298348)共同探讨、学习!
*
* springCloud相关配置
*/
@Configuration
public class SpringCloudConfig {
/**
* 调用服务模版
* @return
*/
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
主要是定义一个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; /**
* Created by 姿势帝-博客园 on 2019/3/26.
* 欢迎添加笔者wx(851298348)共同探讨、学习!
*/ /**
* 知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
*/
@Controller
@RequestMapping("/ticket")
public class TicketConsumerController {
@Autowired
private RestTemplate restTemplate; /**
* 添加或者修改车票信息
*
* @param ticket
* @return
*/
@PostMapping(value = "/save")
@ResponseBody
public boolean save(Ticket ticket) {
System.out.println("======su=====save=========");
return restTemplate.postForObject("http://localhost:1001/ticket/save", ticket, Boolean.class);
} /**
* 查询车票信息
*
* @return
*/
@SuppressWarnings("unchecked")
@GetMapping(value = "/list")
@ResponseBody
public List<Ticket> list() {
return restTemplate.getForObject("http://localhost:1001/ticket/list", List.class);
} /**
* 根据id查询车票信息
*
* @return
*/
@GetMapping(value = "/get/{id}")
@ResponseBody
public Ticket get(@PathVariable("id") Integer id) {
return restTemplate.getForObject("http://localhost:1001/ticket/get/" + id, Ticket.class);
} /**
* 根据id删除车票信息
*
* @return
*/
@GetMapping(value = "/delete/{id}")
@ResponseBody
public boolean delete(@PathVariable("id") Integer id) {
try {
restTemplate.getForObject("http://localhost:1001/ticket/delete/" + id, Boolean.class);
return true;
} catch (Exception e) {
return false;
}
}
}
启动类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; /**
* Created by 姿势帝-博客园 on 2019/3/26.
* 欢迎添加笔者wx(851298348)共同探讨、学习!
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class TicketConsumerApplication_80 {
public static void main(String[] args) {
SpringApplication.run(TicketConsumerApplication_80.class, args);
}
}
这里的话 加了特殊配置 排除了 数据源注入,不加的话 可能会因为找不到数据库配置会报错;
我们测试下:(先启动microservice-ticket-provider-1001,在启动microservice-ticket-consumer-80)
到这里一个最基本的微服务架构完成,后面我们围绕着这个基本架构慢慢演变成生产上的架构
源码下载地址:https://github.com/bangbangw/wfd360-station (选择V2版本)
SpringCloud-day02-服务消费者项目建立的更多相关文章
- Rest构建分布式 SpringCloud微服务架构项目
一.开发环境:jdk 1.8.Maven 3.x.IDEA 2019.1.4.SpringBoot 2.0.7.spring Cloud 最新的稳定版 Finchley SR2 搭配 ...
- SpringCloud的服务消费者 (二):(rest+feign/ribbon)声明式访问注册的微服务
采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...
- springCloud学习-服务消费者(rest+ribbon)
1.ribbon简介 spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign. Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器 它可以 ...
- SpringCloud的服务消费者 (一):(rest+ribbon)访问注册的微服务
采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...
- springCloud学习-服务消费者(Feign)
1.简介 Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解.Fei ...
- springcloud干活之服务消费者(feign)
springcloud系列文章的第三篇 本章将继续讲述springcloud的消费者(feign) Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端. ...
- 玩转SpringCloud(F版本) 二.服务消费者(1)ribbon+restTemplate
上一篇博客有人问我,Springcloud系列会不会连载 ,大家可以看到我的标签分类里已经开设了SpringCloud专题,所以当然会连载啦,本人最近也是买了本书在学习SpringCloud微服务框架 ...
- SpringCloud微服务学习笔记
SpringCloud微服务学习笔记 项目地址: https://github.com/taoweidong/Micro-service-learning 单体架构(Monolithic架构) Mon ...
- springcloud-Netflix创建服务消费者
目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...
随机推荐
- java基础(6)常用API
1 Object类 `java.lang.Object`类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类就是Object. 如果一个类没有特 ...
- Codeforces 1080C- Masha and two friends
AC代码 #include <bits/stdc++.h> #define ll long long const int maxn=1e6+10; using namespace std; ...
- SQL-记录删除篇-007
删除记录: delete * from table_name 解释:删除表中的所有数据 delete * from table_name where id<10 解释:删除表中id小于10的数据 ...
- linux ---> taskkill pid 8080 /f
★ :--linux->-端口占用 : netstat -anonetstat -ano|findstr 8003taskkill /pid 4816 /f
- Linux设备驱动模型之platform(平台)总线详解
/********************************************************/ 内核版本:2.6.35.7 运行平台:三星s5pv210 /*********** ...
- PythonStudy——列表类型 List type
# 1.定义 ls = [3, 1, 2] # 语法糖 | 笑笑语法 print(ls) ls = list([3, 1, 2]) # 本质 print(ls) # 嵌套 ls = [3, 1, [3 ...
- find查找文件的时间问题
很多细节方面的东西没有到真正用的时候,是觉察不出来的,因为这个时间的问题出了问题,现在好好理一下,这个find的时间很容易就搞混了,一段时间不用,也忘了,也反映出来了自己的基础知识不是很牢固啊 f ...
- Spring Boot - 配置介绍
Spring Boot 针对常用的开发场景提供了一系列自动化配置来减少原本复杂而又几乎很少改动的模板配置内容,但是,我们还是需要了解如何在Spring Boot中修改这些自动化的配置,以应对一些特殊场 ...
- Java8-dateTimeFormatter
时间格式化LocalDate,DateTimeFormatter--->parse,ofParttern 伴随lambda表达式.streams以及一系列小优化,Java 8 推出了全新的日期时 ...
- node升级的正确方法
本文主要是针对安装了node的用户如何对node进行升级或者安装指定版本:没有安装node的可以参考连接node安装方法 . 安装方法: 1.产看node版本,没安装的请先安装: $ node -v ...