版本:CamdenSR1
SpringCloud特点:约定优于配置、开箱即用,快速启动、适用于各种环境、轻量级组件、组件的支持很丰富,功能齐全(配置中心,注册中心,智能路由)、选型中立(EuraKa、Zookeeper、consul)。
把maven项目转换成gradle项目 在文件夹立打开命令行窗口,输入: fradle init --type pom 即可
会生成一个build.gradle文件.
服务提供者于服务消费者:
服务提供者:服务的被调用方(即,为其他服务提供服务的服务);
服务消费者:服务的调用方(即,依赖其他服务的服务);
比如电影院购票系统:用户就是服务提供者,提供一个购票的消费接口,
购票的微服务,就是服务消费者,调用用户提供的接口,完成消费;
接下来,让我们来写一个小demo:
进入这个网址:
显示如下界面
选择maven和springboot的版本号
创建我们的项目及我们所需要的组件
需要组件 web、jpa、mysql
然后点击下面的绿色按钮,下载
之后解压
使用开发工具 IDEA,导入maven项目
选择我们要导入项目的pom文件,点击ok
在我们的resource目录下,编写我们的建表语句和一些测试数据
schema.sql :建表语句
drop table if EXISTS user;CREATE TABLE user ( id int NOT NULL AUTO_INCREMENT , username VARCHAR (20), name VARCHAR (20), age int (5), balance DECIMAL (20,2), PRIMARY KEY (id));
data.sql:测试数据
insert into user (id,username,name,age,balance) VALUES (1,'user1','张三',20,'100.00');insert into user (id,username,name,age,balance) VALUES (2,'user2','李四',20,'100.00');insert into user (id,username,name,age,balance) VALUES (3,'user3','王五',20,'100.00');insert into user (id,username,name,age,balance) VALUES (4,'user4','赵六',20,'100.00');insert into user (id,username,name,age,balance) VALUES (5,'user5','王麻子',20,'100.00');
创建包结构
由于SimpleProviderUserApplication 是启动类,必须放在最外层
配置我们的配置文件更名为:application.yml
server: port: 7900 #配置端口号spring: jpa: generate-ddl: false #启动的时候要不要生成ddl语句 show-sql: true #为了打印sql语句 #因为jpa依赖hibernate, 这个也是ddl语句的配置,设置为none hibernate: ddl-auto: none #配置数据源 # MySQL database datasource: platform: mysql url: jdbc:mysql://localhost:3306/macroservice?useUnicode=true&characterEncoding=utf-8 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver schema: classpath:schema.sql data: classpath:data.sql #显示sql语句,并把参数什么的打印出来logging: level: root: INFO org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE com.itmuch: DEBUG
在entity包下,编写我们的实体类,并生产setget
package com.example.simpleprovideruser.entity;import javax.persistence.*;import java.io.Serializable;import java.math.BigDecimal;/** * Created by dell on 2017/9/5. */@Entitypublic class User implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String name; @Column private short age; @Column private BigDecimal balance; public Long getId() { return id; } public String getUsername() { return username; } public String getName() { return name; } public short getAge() { return age; } public BigDecimal getBalance() { return balance; } public void setId(Long id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setName(String name) { this.name = name; } public void setAge(short age) { this.age = age; } public void setBalance(BigDecimal balance) { this.balance = balance; }}
在dao包下,编写我们的接口,并继承 JpaRepository
package com.example.simpleprovideruser.dao;import com.example.simpleprovideruser.entity.User;import org.springframework.data.jpa.repository.JpaRepository;/** * Created by dell on 2017/9/5. */public interface UserDao extends JpaRepository{}
接着编写我们的controller
package com.example.simpleprovideruser.Controller;import com.example.simpleprovideruser.dao.UserDao;import com.example.simpleprovideruser.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;/** * Created by dell on 2017/9/5. */@RestControllerpublic class UserController { @Autowired private UserDao userDao; @GetMapping("/user/{id}") public User findById(@PathVariable Long id){ return userDao.findOne(id); }}
启动项目效果如下:
没有报错,输入
及完成了服务提供者的搭建!
接下来是消费者的搭建:
导入创建好的消费者项目
包结构如下:
编写User实体类
package com.example.simpleconsumermovie.entity;import java.math.BigDecimal;/** * Created by dell on 2017/9/5. */public class User { private Long id; private String username; private String name; private short age; private BigDecimal balance; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getName() { return name; } public void setName(String name) { this.name = name; } public short getAge() { return age; } public void setAge(short age) { this.age = age; } public BigDecimal getBalance() { return balance; } public void setBalance(BigDecimal balance) { this.balance = balance; }}
编写controller
package com.example.simpleconsumermovie.controller;import com.example.simpleconsumermovie.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;/** * Created by dell on 2017/9/5. */@RestControllerpublic class MovieController { @Autowired private RestTemplate restTemplate; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id){ return this.restTemplate.getForObject("http://localhost:7900/user/" + id , User.class); }}
在调用的方法中实例化提供的RestTemplate方法
package com.example.simpleconsumermovie;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplicationpublic class SimpleConsumerMovieApplication { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SimpleConsumerMovieApplication.class, args); }}
配置yml文件的端口号
server: port: 7901 #配置端口号
接下来,首先启动服务提供者,在启动消费者‘
页面输入
这样就实现了,消费者调用服务者的这样一个服务。