spring Cloud并不是我们说的云计算的解决方案,它只是在springBoot基础上进行构建的,用于快速构建分布式系统的通用模式的工具级,使用spring Cloud开发的应用程序适合在Dokcer和PaaS部署。
- spring Cloud 特点
- 约定优于配置
- 适合各种环境的开发和部署
- 提供声明式,无xml的配置方式
- 快速启动,开启就能使用
- 轻量级的组件。例如Eureka(服务集群),Zuul(JVM路由和服务端的负载均衡器)
工具选用
我选用的MAVEN,jdk官方建议使用1.8 ,工具开发可以使用Spring ToolSite(可以到官方下载),我使用是mar1 eclipce 版本,很多装的时候的插件很慢,
而且不断出现下载不下来或者是安装时候出现错误,我提供一个网盘地址下载,这个版本的插件是更新的springboot 2.0M7
链接:https://pan.baidu.com/s/1gfapnqF 密码:99s6服务提供者与服务消费者
服务提供者 服务的被调用方(为其他服务提供服务的服务)
服务消费者 服务的调用方(依赖其他服务的服务)以电影售票系统
了解一下SpringBoot
1.首先我们先建个项目 ,选择,springboot版本,我用的是1.5.9,jdk 1.8,主要很多1.7功能没有全,例如在编写spring batch 批处理,就必须要jdk1.8,一般jdk是maven 配置选中默认版本,如果本机没1.8自己可以到本地添加 Java build Path 进行本地导入配置。
2.@SpringBootApplication=@SpringBootConfiguration+
@EnableAutoConfiguration+@ComponentScan
注解ComponentScan里面有
excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
去除用户自定义的过滤器及自动配置过滤,所以无论你在自定义类中如何加上任何关自定的自动配置和自义定过滤都是不行。
3.我自己建的包如图下
所以建议Controller 建到自己的根包中,就不会出现找不到包的情况了首先建一个User类
package com.zjm.user;
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 int age;
@Column
private BigDecimal balance;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
使用的spring-data-jpa (有规则的查询里必须根据他定义的查询名和是体名称是相同的,hibernate相似,注解的写法实际是差不多,只是orm映射,都是和是体有关联,我记得用的hibernate3.0时候,直接是配置文件和是体一一关联,自从以后注解慢慢的覆盖了xml,简洁而方面。))
UserRepository 类
package com.zjm.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.zjm.user.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
UserController 类
package com.zjm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.zjm.dao.UserRepository;
import com.zjm.user.User;
@RestController
@SpringBootApplication
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/{id}")
public User findById(@PathVariable Long id){
User user=this.userRepository.findOne(id);
return user;
}
public static void main(String[] args) {
SpringApplication.run(UserController.class, args);
}
}
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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>microservice-simple-provider-user</name>
<description>zjmdemo</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8080
spring:
jpa:
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: update
database: mysql
datasource:
username: root
password: a123
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/user
springboot官方指南(相关配置都在指南里面)
https://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-nosql
上面为了普及SpringBoot 写了一个服务提供者
接下来写服务提供者
这是通过RestTemplate请求初始化的,
User 类
package com.zjm.user;
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 int age;
@Column
private BigDecimal balance;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
MovieContoller类
package com.zjm.contoller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.zjm.user.User;
@RestController
@SpringBootApplication
public class MovieController {
@Autowired
public RestTemplate restTemplate;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id){
return this.restTemplate.getForObject("http://localhost:8080/"+id, User.class);
}
}
启动类 ConsumerMovieApplication
package com.zjm;
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);
}
}
这个server端口是8081 application.yml 只要配置端口就可以了
访问http://127.0.0.1:8081/user/1
结果:
{“id”:1,”username”:”account1”,”name”:”zhangsan”,”age”:20,”balance”:100.00}