1. 程式人生 > >Spring cloud實戰微服——服務提供者

Spring cloud實戰微服——服務提供者

turn nbsp private config amd .class res 請求 sim

目錄講解:

  一、服務提供者與服務消費者的概念

  二、編寫一個服務提供者的測試類(code)

  1.1、 使用微服務構建的是分布式系統,微服務之間通過網絡進行通信。我們使用微服務提供者與服務消費者來描述微服務之間的調用關系。

     如下:

技術分享圖片

    在生活中就有這樣的例子,在我們看電影的時候,用戶發起一個購票的請求。在進行購票的業務操作前,電影微服務需要調用用戶微服務的接口,

    查詢當前用戶的余額是多少,是不是符合購票標準等等。在這種場景下,用戶微服務就是一個服務提供者,電影微服務則是一個服務消費者

     技術分享圖片

2.2 編寫一個服務提供者

    框架結構如下:

    技術分享圖片

    

    步驟:

    2.1 創建一個maven項目,架構如上,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"
> <modelVersion>4.0.0</modelVersion> <groupId>com.itmuch.cloud</groupId> <artifactId>microservice-simple-provider-user</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!-- 引入spring boot的依賴 --> <parent
> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2.2 準備好建表語句

drop table user if exists;
create table user (id bigint generated by default as identity, username varchar(40), name varchar(20), age int(3), balance decimal(10,2), primary key (id));

2.2 準備好幾條語句

insert into user (id, username, name, age, balance) values (1, ‘account1‘, ‘張三‘, 20, 100.00);
insert into user (id, username, name, age, balance) values (2, ‘account2‘, ‘李四‘, 28, 180.00);
insert into user (id, username, name, age, balance) values (3, ‘account3‘, ‘王五‘, 32, 280.00);

  

2.3 準備實體類

package com.itmuch.cloud.study.entity;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  @Column
  private String username;
  @Column
  private String name;
  @Column
  private Integer age;
  @Column
  private BigDecimal balance;

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return this.age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public BigDecimal getBalance() {
    return this.balance;
  }

  public void setBalance(BigDecimal balance) {
    this.balance = balance;
  }

}

  

2.3 編寫Dao類

2.3 編寫controller類

package com.itmuch.cloud.study.controller;

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 com.itmuch.cloud.study.entity.User;
import com.itmuch.cloud.study.repository.UserRepository;

@RestController
public class UserController {
  @Autowired
  private UserRepository userRepository;

  @GetMapping("/{id}")
  public User findById(@PathVariable Long id) {
    User findOne = this.userRepository.findOne(id);
    return findOne;
  }
}

  2.4 編寫啟動類

package com.itmuch.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderUserApplication {
  public static void main(String[] args) {
    SpringApplication.run(ProviderUserApplication.class, args);
  }
}

  2.5 編寫配置文件

server:
  port: 8000
spring:
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:                           # 指定數據源
    platform: h2                        # 指定數據源類型
    schema: classpath:schema.sql        # 指定h2數據庫的建表腳本
    data: classpath:data.sql            # 指定h2數據庫的數據腳本
logging:                                # 配置日誌級別,讓hibernate打印出執行的SQL
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    
## INFO
info: 
  app: 
    name: @project.artifactId@
    encoding: @project.build.sourceEncoding@
    java:
      source: @java.version@
      target: @java.version@

   最後在瀏覽器裏面輸入:localhost:8000/1 看見了用戶信息,則成功了

技術分享圖片

後續再為大家總結,有寫的不周到的地方,歡迎大家提出來

Spring cloud實戰微服——服務提供者