资讯专栏INFORMATION COLUMN

SpringCloud(第 001 篇)简单用户微服务

ivyzhang / 356人阅读

摘要:第篇简单用户微服务一大致介绍通过接口来简单获取数据库中的用户信息,并且数据库中的字段与实体类的字段相互映射。添加简单用户微服务启动类简单用户微服务类。

SpringCloud(第 001 篇)简单用户微服务

-

一、大致介绍
通过 RestAPI 接口 /simple/{id} 来简单获取 H2 数据库中的用户信息,并且数据库中的字段与实体 User 类的字段相互映射 。
二、实现步骤 2.1 添加 maven 引用包


    4.0.0

    springms-simple-provider-user
    1.0-SNAPSHOT
    jar
    
    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    
    
    
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.h2database
            h2
            runtime
        

        
        
            org.springframework.boot
            spring-boot-starter-data-solr
        
    

2.2 添加应用配置文件(springms-simple-provider-usersrcmainresourcesapplication.yml)
server:
  port: 8000
spring:
  application:
    name: springms-simple-provider-user  #全部小写
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:
    platform: h2
    schema: classpath:schema.sql
    data: classpath:data.sql
logging:
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.springms: DEBUG
2.3 添加 H2 数据库脚本(springms-simple-provider-usersrcmainresourcesschema.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)
);
2.4 插入 H2 数据库一些初始化数据(springms-simple-provider-usersrcmainresourcesdata.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", "李四", 22, 100.00);
INSERT into user (id, username, name, age, balance) values (3, "user3", "王五", 24, 100.00);
INSERT into user (id, username, name, age, balance) values (4, "user4", "赵六", 26, 100.00);
INSERT into user (id, username, name, age, balance) values (5, "user5", "李逵", 27, 100.00);
INSERT into user (id, username, name, age, balance) values (6, "user6", "张远", 10, 100.00);
INSERT into user (id, username, name, age, balance) values (7, "user7", "迪拜", 60, 100.00);
INSERT into user (id, username, name, age, balance) values (8, "user8", "哈士奇", 40, 100.00);
INSERT into user (id, username, name, age, balance) values (9, "user9", "关羽", 30, 100.00);
2.5 添加访问底层数据模型的DAO接口(springms-simple-provider-usersrcmainjavacomspringmscloudrepositoryUserRepository.java)
package com.springms.cloud.repository;

import com.springms.cloud.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository {

}
2.6 添加实体用户类User(springms-simple-provider-usersrcmainjavacomspringmscloudentityUser.java)
package com.springms.cloud.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 Short 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 Short getAge() {
    return this.age;
  }

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

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

  public void setBalance(BigDecimal balance) {
    this.balance = balance;
  }
}
2.7 添加用户Web访问层Controller(springms-simple-provider-usersrcmainjavacomspringmscloudcontrollerMsSimpleProviderUserController.java)
package com.springms.cloud.controller;

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

/**
 * 用户微服务Controller。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/17
 *
 */
@RestController
public class MsSimpleProviderUserController {

    @Autowired
    private UserRepository userRepository;

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

2.8 添加简单用户微服务启动类(springms-simple-provider-usersrcmainjavacomspringmscloudMsSimpleProviderUserApplication.java)
package com.springms.cloud;

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

/**
 * 简单用户微服务类。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/17
 *
 */
@SpringBootApplication
public class MsSimpleProviderUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsSimpleProviderUserApplication.class, args);
        System.out.println("【【【【【【 简单用户微服务 】】】】】】已启动.");
    }
}
三、测试
/****************************************************************************************
 一、简单用户微服务接口测试:

 1、启动 springms-simple-provider-user 模块服务,启动1个端口;
 2、在浏览器输入地址 http://localhost:8000/simple/1 可以看到信息成功的被打印出来。;
 ****************************************************************************************/
四、下载地址
https://git.oschina.net/ylimhhmily/SpringCloudTutorial.git

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70470.html

相关文章

  • SpringCloud 001 简单用户服务

    摘要:第篇简单用户微服务一大致介绍通过接口来简单获取数据库中的用户信息,并且数据库中的字段与实体类的字段相互映射。添加简单用户微服务启动类简单用户微服务类。 SpringCloud(第 001 篇)简单用户微服务 - 一、大致介绍 通过 RestAPI 接口 /simple/{id} 来简单获取 H2 数据库中的用户信息,并且数据库中的字段与实体 User 类的字段相互映射 。 二、实现步骤...

    zombieda 评论0 收藏0
  • SpringCloud 002 简单电影服务类(消费方,而提供方为用户服务

    摘要:添加简单电影微服务启动类简单电影微服务类消费方,而提供方为用户微服务。 SpringCloud(第 002 篇)简单电影微服务类(消费方,而提供方为用户微服务) - 一、大致介绍 微服务与微服务之间通过 Http 协议进行通信; 用户微服务作为提供方,电影微服务作为消费方,电影微服务消费用户微服务 ; 二、实现步骤 2.1 添加 maven 引用包 4.0.0 s...

    高璐 评论0 收藏0
  • SpringCloud 046 )注解式Schedule配置定时任务,不支持任务调度

    摘要:当前时间打印当前时间定时任务触发,操作多个添加数据,事务中任一异常,都可以正常导致数据回滚。当前时间当前时间添加微服务启动类注解式配置定时任务,不支持任务调度。 SpringCloud(第 046 篇)注解式Schedule配置定时任务,不支持任务调度 - 一、大致介绍 1、很多时候我们需要隔一定的时间去执行某个任务,为了实现这样的需求通常最普通的方式就是利用多线程来实现; 2、但是有...

    masturbator 评论0 收藏0
  • SpringCloud 051 )EurekaServer集群高可用注册中心以及简单的安全认证

    SpringCloud(第 051 篇)EurekaServer集群高可用注册中心以及简单的安全认证 - 一、大致介绍 1、前面章节分析了一下 Eureka 的源码,我们是不是在里面注意到了 Peer 节点的复制,为什么要复制节点同步信息呢,其实就是为了同一个集群之间的EurekaServer一致性方案的一个实现; 2、于是我们在本章节就真正的来通过代码来实现一下EurekaServer之间的高...

    coordinate35 评论0 收藏0
  • SpringCloud 027 )集成异构服务系统到 SpringCloud 生态圈中(比如

    摘要:注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。包含了客户端注解,同时也包含了断路器模块注解,还包含了网关模块。 SpringCloud(第 027 篇)集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务) - 一、大致介绍 1、在一些稍微复杂点系统中,往往都不是单一代码写的服务,而恰恰相反集成了各种语言写的系统,并且我们还...

    caozhijian 评论0 收藏0

发表评论

0条评论

ivyzhang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<