SpringCloud实战微服务

版本:CamdenSR1java

SpringCloud特色:约定优于配置、开箱即用,快速启动、适用于各类环境、轻量级组件、组件的支持很丰富,功能齐全(配置中心,注册中心,智能路由)、选型中立(EuraKa、Zookeeper、consul)。mysql

把maven项目转换成gradle项目  在文件夹立打开命令行窗口,输入: fradle init --type pom 便可web

会生成一个build.gradle文件.spring

服务提供者于服务消费者:sql

        服务提供者:服务的被调用方(即,为其余服务提供服务的服务);springboot

        服务消费者:服务的调用方(即,依赖其余服务的服务);app

好比电影院购票系统:用户就是服务提供者,提供一个购票的消费接口,maven

                                  购票的微服务,就是服务消费者,调用用户提供的接口,完成消费;ide

接下来,让咱们来写一个小demo:微服务

            进入这个网址:https://start.spring.io

显示以下界面

选择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.
 */
@Entity
public 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<User,Long> {


}

接着编写咱们的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.
 */

@RestController
public 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.
 */
@RestController
public 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;

@SpringBootApplication
public class SimpleConsumerMovieApplication {
   @Bean
   public RestTemplate restTemplate(){
      return  new RestTemplate();
   }

   public static void main(String[] args) {

      SpringApplication.run(SimpleConsumerMovieApplication.class, args);
   }
}

配置yml文件的端口号

server:
  port: 7901  #配置端口号

接下来,首先启动服务提供者,在启动消费者‘

页面输入

这样就实现了,消费者调用服务者的这样一个服务。

相关文章
相关标签/搜索