当前位置: 首页 > 编程日记 > 正文

快速构建Spring Cloud工程

spring cloud简介

spring cloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解

在之前的所有Spring Boot相关博文中,都会涉及Spring Boot工程的创建。而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也可以通过SPRING INITIALIZR页面工具来创建,相信每位读者都有自己最喜欢和最为熟练的创建方式。

本文我们将介绍嵌入的Intellij中的Spring Initializr工具,它同Web提供的创建功能一样,可以帮助我们快速的构建出一个基础的Spring Cloud工程。

创建工程

第一步:菜单栏中选择File=New=Project..,我们可以看到如下图所示的创建功能窗口。其中Initial Service Url指向的地址就是Spring官方提供的Spring Initializr工具地址,所以这里创建的工程实际上也是基于它的Web工具来实现的。

项目结构:

我测试了连接数据库查询数据,不多说先配置依赖:

 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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!-- 这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--S支持全栈式Web开发,包括Tomcat和spring-webmvc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--数据库连接 --><!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client --><dependency><groupId>org.mariadb.jdbc</groupId><artifactId>mariadb-java-client</artifactId><version>2.4.1</version></dependency><!--支持JPA(Java Persistence API. ,包括spring-data-jpa、spring-orm、Hibernate。--><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><version>2.1.3.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

application.properties:配置

server.port=3333# 数据源配置
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/stu
spring.datasource.username=root
spring.datasource.password=666666spring.jpa.hibernate.ddl-auto=update# 如果是mariadb,需要配置这个
spring.database-platform=org.hibernate.dialect.MariaDB10Dialect

controller层:

package com.example.demo.controller;import com.example.demo.entity.News;
import com.example.demo.service.Impl.NewsServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class NewsController{@Autowiredprivate NewsServiceImp newsServiceImp;//查询@RequestMapping(value = "/listNews", method = RequestMethod.GET)public ResponseEntity getNews() {List<News> news = newsServiceImp.listAll();return ResponseEntity.ok(news);}}

dao层:

package com.example.demo.dao;import com.example.demo.entity.News;
import org.springframework.data.jpa.repository.JpaRepository;public interface NewsMapper extends JpaRepository<News,Integer> {
}

entity层:

package com.example.demo.entity;import javax.persistence.*;@Entity
@Table(name = "news") //表名
public class News {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)  //标明该字段是自动增长private int id;private String title;private String body;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}
}

service层 里面包含的一个Impl 的包,下面放的是实现接口类:

package com.example.demo.service;import com.example.demo.entity.News;import java.util.List;public interface NewsService {List<News> listAll();void add (News news);void del (int id);void update(News news);
}

Impl下:

package com.example.demo.service.Impl;import com.example.demo.dao.NewsMapper;
import com.example.demo.entity.News;
import com.example.demo.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class NewsServiceImp implements NewsService {@Autowiredprivate NewsMapper newsMapper;@Overridepublic List<News> listAll() {return newsMapper.findAll();}@Overridepublic void add(News news) {newsMapper.save(news);}@Overridepublic void del(int id) {newsMapper.deleteById(id);}@Overridepublic void update(News news) {newsMapper.save(news);}
}

下面启动看效果:

数据库已经查询出来

源码地址: https://github.com/nongzihong/spring_cloud

转载于:https://www.cnblogs.com/nongzihong/p/10576132.html

相关文章:

win10红色警戒黑屏解决

相信很多80&#xff0c;90后的同学们都喜欢在代码之余打打红色警戒 但是苦于win10差劲兼容性&#xff0c;每次下个红色警戒or尤里复仇不是弹框就是黑屏 今天笔者给出一个完美究极解决方案 请注意&#xff1a; 1&#xff01;红色警戒中把Ra2.exe和Game.exe右键兼容性调到Win XP …

html超链接button

1.如果让本页转向新的页面则用&#xff1a; <input typebutton οnclick"window.location.href(连接)"> 2.如果需要打开一个新的页面进行转向&#xff0c;则用&#xff1a; <input typebutton οnclick"window.open(连接)">转载于:https://www…

低版本jdbc连接高版本oracle,转:oracle11g的JDBC连接 URL和之前的版本有一定的区别...

今天安装了oracle11g后&#xff0c;写了JDBC测试程序&#xff0c;一直都连接不上&#xff01;一直找不到原因后来读了一下安装文件中的Readme.txt文档&#xff0c;汗啊&#xff01;这个版本居然把url的访问方式改变了&#xff1a;Some Useful Hints In Using the JDBC Drivers-…

Android studio 获取每次编译apk时的日期

项目中需要获取apk的编译日期&#xff0c;首先肯定是用手动的方式获取&#xff0c;但这样容易遗忘&#xff0c;怎么样通过代码的方式获取呢&#xff1f; 其实android 为我们提供了一个BuildConfig的类&#xff0c;android 每次编译的时候都会自动生成 一次BuildConfig 类&#…

明文存密码成惯例?Facebook 6 亿用户密码可被 2 万员工直接看

近日&#xff0c;外媒发布了一份互联网安全的调研报告&#xff0c;报告中称Facebook曾将6亿用户的账号密码使用明文存储&#xff0c;且可以被Facebook内部员工随意搜索查看。据Facebook方面的消息人士称&#xff0c;纯文本存档的用户密码可追溯到2012年&#xff0c;在这期间有超…

pthreads 的学习

多线程学习参考的网站&#xff1a; http://www.ibm.com/developerworks/cn/linux/l-pthred/ 初探线程——pthread_create http://www.cnblogs.com/huangwei/archive/2010/05/19/1739593.html 转载于:https://www.cnblogs.com/nemo2011/archive/2012/05/02/2479163.html

Oracle不加IP无法登录,Oracle 无法通过IP连接问题

1.安装目录:D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN2.listener.ora(里面的localhost或127.0.0.1改成机器名)# listener.ora Network Configuration File: D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\listener.ora# Generated by Oracle…

巧用gh-pages分支发布自己的静态项目

大家都知道可以通过github pages 发布自己的静态博客&#xff0c;然后通过 username.github.io 可以访问。例如我的博客可以通过 nqmysb.github.io 访问&#xff0c;不过我的已经绑定域名 https://liaocan.top &#xff0c;所以会直接跳转到域名显示。但是我们通常有很多其他的…

【读书笔记】iOS-网络-解析响应负载

Web Service可以通过多种格式返回结构化数据&#xff0c; 不过大多数时候使用的是XML与JSON。也可以让应用只接收HTML结构的数据。实现了这些Web Service或是接收HTML文档的应用必须能解释这种结构化数据并将其转换为对于应用上下文有意义的对象。 一&#xff0c;XML 使用原生解…

What Are Words(一诺千金)

曲名&#xff1a;What Are Words&#xff08;一诺千金&#xff09;Anywhere you are, I am near Anywhere you go, Ill be there Anytime you whisper my name, youll see How every single promise I keep Cause what kind of guy would I be If I was to leave when you need…

oracle 插入 基准测试,oracle proc 插入操作性能优化实践

场景&#xff1a;student 表中 10万条数据。从 student 表中取出所有数据&#xff0c;插入到 student_his 表中优化方法&#xff1a;1.批量插入(效果明显)2.批量查询(效果不明显)3.批量提交(效果不明显)4.预编译 sql 语句(效果不明显)效果&#xff1a;10万条数据&#xff0c;普…

240个jquery插件

240个jquery插件 http://www.kollermedia.at/archive/2007/11/21/the-ultimate-jquery-plugin-list/File upload Ajax File Upload.jQUploader.Multiple File Upload plugin.jQuery File Style.Styling an input type file.Progress Bar Plugin. Form Validation jQuery Valida…

sql 优化 tips

索引就是排序 outer join笛卡儿积, inner join看情况。 可以用临时表加update的方式把outer join 替换成inner join提高性能。用union代替where中的or 和join(不同表时)join的列有索引&#xff0c;select 中的列能被索引覆盖到&#xff0c;消除执行计划中的lookup(lookup有时会…

第24课 《前端之路,以不变应万变》

今天的内容有些借鉴于业内大佬的内容&#xff0c;由于本人技术实在太渣&#xff0c;几乎没有可以用来演讲的素材。抱歉 大家好&#xff0c;我是来自存勖科技的Rocken。我今天演讲的内容是&#xff1a;前端的未来。大家都知道&#xff0c;前端所依托的基础直到上世纪九十年代才出…

php hasattribute,PHP DOMElement hasAttribute()用法及代码示例

DOMElement::hasAttribute()函数是PHP中的内置函数&#xff0c;用于了解具有特定名称的属性是否作为元素的成员存在。用法:bool DOMElement::hasAttribute( string $name )参数&#xff1a;该函数接受单个参数$name&#xff0c;该参数保存属性的名称。返回值&#xff1a;如果成…

搭建turnserver

参考文件&#xff1a; http://blog.csdn.net/kl222/article/details/20145423 为什么要搭建TURN服务器&#xff1f; 因为我们编写的sip客户端再和南瑞的sip服务器进行通信的时候&#xff0c;中间经过一个安全平台&#xff0c;这个安全平台具有NAT和防火墙功能。RTP和RTCP包传递…

【Android开发】:在任意目录执行NDK编译

2019独角兽企业重金招聘Python工程师标准>>> 文以简单的例子讲述如何在任意目录把自己写的C代码使用NDK提供的交叉编译该工具来编译成Android可使用的静态库/动态库。 1. 准备环境 首先&#xff0c;你得安装了Android的NDK编译工具&#xff0c;假设你的NDK的根目录在…

SurfaceView 间取得焦点

在SurfaceView中我们的onKeyDown虽然重写了view的函数&#xff0c; 但一定需要我们在初始化的时候去声明焦点 //添加这个来取得按健事件this.setFocusable(true);this.setFocusableInTouchMode(true);this.requestFocus();如果这些方法&#xff0c;会造成按键无效&#xff0c;提…

Oracle字符串转BooIean,利用Java的多线程技术实现数据库的访问.pdf

利用Java的多线程技术实现数据库的访问.pdf第 卷第 期 计算机应用22 12 Voi .22 , No . 12年 月2002 12 Computer Appiications Dec . , 2002文章编号&#xff1a; ( )1001 - 9081 2002 12 - 0121 - 03利用Java 的多线程技术实现数据库的访问刘 巍&#xff0c;唐学兵(武汉大学 …

Linux音频设备驱动

在Linux中&#xff0c;先后出现了音频设备的两种框架OSS和ALSA&#xff0c;本节将在介绍数字音频设备及音频设备硬件接口的基础上&#xff0c;展现OSS和ALSA驱动的结构。17.1&#xff5e;17.2节讲解了音频设备及PCM、IIS和AC97硬件接口。17.3节阐述了Linux OSS音频设备驱动的组…

japid-controller自动绑定的数据类型

参考文献&#xff1a;http://www.playframework.org/documentation/1.2.3/controllers 当参数名和HTTP请求中的参数名&#xff08;即界面中的name&#xff09;相同时&#xff0c;后台Controller可以直接获取该变量的值。变量分两大类&#xff1a; 1. Simple types 所有的基本数…

NAT,Easy IP

R3配置 [V200R003C00] #snmp-agent local-engineid 800007DB03000000000000snmp-agent #clock timezone Indian Standard Time minus 05:13:20clock daylight-saving-time Day Light Saving Time repeating 12:32 9-1 12:32 11-23 00:00 2005 2005 #drop illegal-mac alarm #…

linux用户在哪个文件夹,LINUX中用命令成功建立一个用户后信息会记录在哪个文件中...

LINUX中用命令成功建立一个用户后信息会记录在哪个文件中发布时间:2007-07-28 10:14:57来源:红联作者:MPiops增加用户帐号后新建用户的命令十分简单&#xff0c;在命令行下使用 useradd 命令&#xff1a;useradd david该命令做了下面几件事&#xff1a;1)在 /etc/passwd 文件中…

iOS开发—block介绍

- (void)viewDidLoad {[super viewDidLoad];NSLog("我在玩手机");NSLog("手机没电了");[self chargeMyIphone:^{NSLog("出门逛街");}];NSLog("我在看电视"); }-(void)chargeMyIphone:(void(^)(void))finishBlock {double delayInSecon…

Sap Byd Soap使用 SSL 客户端证书

1.修改通讯安排为使用SSL客户端证书2.设置客户端证书上传证书,或者上传并下载证书3.用SoapUI 测试系统选择下载来的证书,或者自己创建的通讯证书,并输入密码配置好后就可以测试系统了4.关于验证失败.byd 系统 有时候,系统生效会比较慢,如果不行,请等待3分钟,还是不行,从头在试下…

IOS开源项目汇总

扫描wifi信息&#xff1a;http://code.google.com/p/uwecaugmentedrealityproject/http://code.google.com/p/iphone-wireless/ 条形码扫描&#xff1a;http://zbar.sourceforge.net/iphone/sdkdoc/install.html tcp/ip的通讯协议&#xff1a;http://code.google.com/p/cocoaas…

linux命令face,linux下配置face_recognition

1、如linux下已有python2.7,但需要更新一下python 2.7至python2.xsudo add-apt-repository ppa:fkrull/deadsnakes-python2.7sudo apt-getupdatesudo apt-get upgrade2、部署步骤安装Boost, Boost.Pythonsudo apt-get install build-essential cmakesudo apt-get install libgt…

微服务系列(五):事件驱动的数据管理

编者的话&#xff5c;本文来自 Nginx 官方博客&#xff0c;是「Chris Richardson 微服务」系列的第五篇文章。第一篇文章介绍了微服务架构模式&#xff0c;并且讨论了使用微服务的优缺点&#xff1b;第二和第三篇描述了微服务架构模块间通讯的不同方面&#xff1b;第四篇研究了…

js基础知识温习:Javascript中如何模拟私有方法

本文涉及的主题虽然很基础&#xff0c;在很多人眼里属于小伎俩&#xff0c;但在JavaScript基础知识中属于一个综合性的话题。这里会涉及到对象属性的封装、原型、构造函数、闭包以及立即执行表达式等知识。 公有方法 公有方法就是能被外部访问并调用的方法。 // 在对象中 var R…

格式化测试数据,组装用于插入表中的sql语句

最近闲的蛋疼&#xff0c;每每在写测试例子的时候&#xff0c;万恶的测试数据需要手工书写insert语句的向表中插入&#xff0c;很费事&#xff0c;于是就像写个脚本来实现自动生成插入语句 测试数据&#xff1a; 100 北京 20120203123 100 天津20120203123 101 湖南20120203nul…