spring cloud服务提供者与服务消费者

一 Spring Tool Suit官网下载
https://spring.io/tools/sts/legacy
下载3.8.3版本
二 服务提供者和服务消费者
服务提供者:服务的被调用方,即为其他服务提供服务的服务。
服务消费者:服务的调用方,即依赖其他服务的服务。
spring cloud服务提供者与服务消费者_第1张图片
三 服务提供者编写过程
1 进入 http://start.spring.io/
2 创建用户微服务
spring cloud服务提供者与服务消费者_第2张图片
3 点击生成项目
生成 microservice-simple-provider-user.zip
4 创建电影微服务
spring cloud服务提供者与服务消费者_第3张图片
microservice-simple-consumer-movie.zip
四 导入用户微服务
spring cloud服务提供者与服务消费者_第4张图片
spring cloud服务提供者与服务消费者_第5张图片
spring cloud服务提供者与服务消费者_第6张图片
五 编写服务提供者
1 pom.xml


  4.0.0
  com.itmuch.cloud
  microservice-simple-provider-user
  0.0.1-SNAPSHOT
  jar
  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.3.RELEASE
  
  
    UTF-8
    1.8
  
  
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.boot
      spring-boot-starter-data-jpa
    
    
      com.h2database
      h2
    
    
      org.springframework.boot
      spring-boot-starter-actuator
    
  
  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Camden.SR4
        pom
        import
      
    
  
  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  
2 准备好建表语句,在项目的classpath下建立schema.sql.
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));
3 准备好几条数据,在classpath下建立文件data.sql
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);
4 创建实体类
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;
  }

}
5 创建DAO
package com.itmuch.cloud.study.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.itmuch.cloud.study.entity.User;

@Repository
public interface UserRepository extends JpaRepository {
}
6 创建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;
  }
}
6 编写启动类
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);
  }
}
7 编写配置文件
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@
8 测试
spring cloud服务提供者与服务消费者_第7张图片
六 编写服务消费者
1 pom.xml


  4.0.0
  com.itmuch.cloud
  microservice-simple-consumer-movie
  0.0.1-SNAPSHOT
  jar
  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.3.RELEASE
  
  
    UTF-8
    1.8
  
  
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.boot
      spring-boot-starter-actuator
    
  
  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Camden.SR4
        pom
        import
      
    
  
  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  
2 实体类
package com.itmuch.cloud.study.user.entity;
import java.math.BigDecimal;
public class User {
  private Long id;
  private String username;
  private String name;
  private Integer age;
  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;
  }
}
3 启动类
package com.itmuch.cloud.study;

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 ConsumerMovieApplication {
  @Bean
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }

  public static void main(String[] args) {
    SpringApplication.run(ConsumerMovieApplication.class, args);
  }
}
4 Controller
package com.itmuch.cloud.study.user.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 org.springframework.web.client.RestTemplate;

import com.itmuch.cloud.study.user.entity.User;

@RestController
public class MovieController {
  @Autowired
  private RestTemplate restTemplate;

  @GetMapping("/user/{id}")
  public User findById(@PathVariable Long id) {
    return this.restTemplate.getForObject("http://localhost:8000/" + id, User.class);
  }
}
5 application.yml
server:
  port: 8010
6 测试
spring cloud服务提供者与服务消费者_第8张图片

你可能感兴趣的:(微服务)