最新学习了一些使用SpringCloud搭建微服务的简单样例,虽然简单,却包含了SpringCloud一些基本的入门知识,怕日久生遗,随记于此!
单体应用架构存在的问题和向微服务演进的北京这里就不做介绍了,大家感兴趣的可以自己 去了解一下。
Spring Cloud 版本
Component
| Angel.SR6 | Brixton.SR5 | Camden.M1 | Camden.BUILD-SNAPSHOT |
---|---|---|---|---|
spring-cloud-aws | 1.0.4.RELEASE | 1.1.1.RELEASE | 1.1.1.RELEASE | 1.1.2.BUILD-SNAPSHOT |
spring-cloud-bus | 1.0.3.RELEASE | 1.1.1.RELEASE | 1.2.0.M1 | 1.2.0.BUILD-SNAPSHOT |
spring-cloud-cli | 1.0.6.RELEASE | 1.1.5.RELEASE | 1.2.0.M1 | 1.2.0.BUILD-SNAPSHOT |
spring-cloud-commons | 1.0.5.RELEASE | 1.1.1.RELEASE | 1.1.1.RELEASE | 1.1.2.BUILD-SNAPSHOT |
spring-cloud-contract | 1.0.0.M2 | 1.0.0.BUILD-SNAPSHOT | ||
spring-cloud-config | 1.0.4.RELEASE | 1.1.3.RELEASE | 1.2.0.M1 | 1.2.0.BUILD-SNAPSHOT |
spring-cloud-netflix | 1.0.7.RELEASE | 1.1.5.RELEASE | 1.2.0.M1 | 1.2.0.BUILD-SNAPSHOT |
spring-cloud-security | 1.0.3.RELEASE | 1.1.2.RELEASE | 1.1.2.RELEASE | 1.1.3.BUILD-SNAPSHOT |
spring-cloud-starters | 1.0.6.RELEASE | |||
spring-cloud-cloudfoundry | 1.0.0.RELEASE | 1.0.0.RELEASE | 1.0.1.BUILD-SNAPSHOT | |
spring-cloud-cluster | 1.0.1.RELEASE | |||
spring-cloud-consul | 1.0.2.RELEASE | 1.1.0.M1 | 1.1.0.BUILD-SNAPSHOT | |
spring-cloud-sleuth | 1.0.6.RELEASE | 1.0.6.RELEASE | 1.0.7.BUILD-SNAPSHOT | |
spring-cloud-stream | 1.0.2.RELEASE | Brooklyn.M1 | Brooklyn.BUILD-SNAPSHOT | |
spring-cloud-zookeeper | 1.0.2.RELEASE | 1.0.2.RELEASE | 1.0.3.BUILD-SNAPSHOT | |
spring-boot | 1.2.8.RELEASE | 1.3.7.RELEASE | 1.4.0.RELEASE | 1.4.0.RELEASE |
spring-cloud-task | 1.0.2.RELEASE | 1.0.2.RELEASE | 1.0.3.BUILD-SNAPSHOT |
实战篇
编写服务提供者(用户微服务)
创建一个maven项目
<?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.mountain.springboot</groupId>
<artifactId>microservice-provider-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 引入spring boot的依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEbcoding>
UTF-8
</project.build.sourceEbcoding>
<java-version>1.8</java-version>
</properties>
<dependencies>
<dependency>
<!--提供springMVC的支持-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!--提供spring data JPA的支持-->
<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-thymeleaf</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>
准备好建表语句
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)
);
准备几条数据
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);
创建用户实体类
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
@Column
private String username;
@Column
private Integer age;
@Column
private BigDecimal balance;
此处省略set/get
}
创建DAO
/**
* @author mountain
* @date 2017/10/23 9:56
* @description userDao
*/
public interface UserRepository extends JpaRepository<User,Long> {
}
建controller
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/{id}")
public User findById(@PathVariable Long id){
return this.userRepository.findOne(id);
}
}
编写启动类
@SpringBootApplication
public class ProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderUserApplication.class,args);
}
}
在类上使用@SpringBootApplication声明这个是一个SpringBoot项目,@SpringBootApplication是一个组合注解。
之前用户使用的是3个注解注解他们的main类。分别是@Configuration,@EnableAutoConfiguration,@ComponentScan。由于这些注解一般都是一起使用,spring boot提供了一个统一的注解@SpringBootApplication。
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
编写配置文件
配置文件命名为application.yml
server:
port: 8000
spring:
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
info:
app:
author: mountian
tel: 1347*******
name: microservice-simple-provider-user
java:
source: 1.8
target: 1.8
info: app: author: mountian tel: 1347******* name: microservice-simple-provider-user java: source: 1.8 target: 1.8
Spring Cloud支持使用properties或者yml格式的文件作为配置文件 但是(以下这段网摘自@认真的学生)
将yml与properties对比之后发现,yml最大的好处在于其拥有天然的树状结构,所以着手尝试将properties文件更改为yml文件,发现了几个要注意的地方:
1、在properties文件中是以”.”进行分割的, 在yml中是用”:”进行分割;
2、yml的数据格式和json的格式很像,都是K-V格式,并且通过”:”进行赋值;
3、在yml中缩进一定不能使用TAB,否则会报很奇怪的错误;(缩进特么只能用空格!!!!)
4、每个k的冒号后面一定都要加一个空格;
5、使用spring cloud的maven进行构造的项目,在把properties换成yml后,一定要进行mvn clean insatll
测试
{"id":1,"name":"张三","username":"account1","age":20,"balance":100.00}
创建服务的消费者http://blog.csdn.net/u010331823/article/details/78336883
如果以上内容对你有帮助,麻烦客官给个赞