小白极速入手spring cloud 这把利器!学习spring cloud 要先有点spring boot基础,因为spring cloud是在spring boot上开发的。不过不要怕,简单实现以下spring boot创建项目。
开发准备 约定
jdk 1.8
maven 3.3.9
spring boot 1.4.1
spring cloud Camden SR1
4. 服务消费者与服务提供者
demo将使用spring boot完成一个查询user信息的接口。由服务者提供接口,消费者查询接口
1.下载demo
2.检查pom.xml
3.创建数据库数据
4.配置spring配置文件
5.创建实体类
6.创建DAO层jap操作(消费者需要)
7.创建Controller
8.测试——OK
4.1 创建项目
1.下载demo并导入
登录网站:http://start.spring.io/
注:spring boot 版本没有可以先在这,构建项目后修改pom文件
下载——解压——idea导入maven项目,进入开发。
2.开发——服务提供者
2.1pom.xml
主要引入三个:spring-boot-starter-parent(1.4.1.RELEASE)、spring-boot-starter-data-jpa、spring-boot-starter-web
<?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.xiaoming</groupId>
<artifactId>cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 数据层
resources 目录下新建SQL文件
这里使用H2数据库
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)
);
data.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', '李四', 20, 100.00);
insert into user(id,username, name, age, balance) values(3,'user3', '王五', 20, 100.00);
insert into user(id,username, name, age, balance) values(4,'user4', '马六', 20, 100.00);
修改application.yml文件
#服务发布端口号
server:
port: 7900
spring:
# 是否打印SQL语句等
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
# 数据库连接方式
datasource:
platform: h2
schema: classpath:sql/schema.sql
data: classpath:sql/data.sql
# 日志级别
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
com.itmuch: DEBUG
2.3 实体类
@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;
get/set 方法省略....
}
2.4 dao层
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
2.5 Controller
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/simple/{id}")
public User findById(@PathVariable Long id) {
return this.userRepository.findOne(id);
}
}
2.6 测试
CloudApplication.java 执行main方法;
网页访问:http://localhost:7900/simple/1
结果:
{"id":1,"username":"user1","name":"张三","age":20,"balance":100.00}
开发完毕
3.开发——服务消费者
3.1 pom.xml 同上
3.2 application.yml
server:
port: 7901
user:
userServicePath: http://localhost:7900/simple/
3.3 实体类
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
}
3.4 Controller层
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
// 读取application中配置
@Value("${user.userServicePath}")
private String userServicePath;
@GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject(this.userServicePath + id, User.class);
}
}
1.RestTemplate 此处包找不到bean不要担心,看下一个步骤解决。
3.5 引入restTemplat bean
CloudApplication.java
@SpringBootApplication
public class CloudApplication {
//引入bean restTemplate方法名 与controller中名称一致
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(CloudApplication.class, args);
}
}
3.6 测试
启动程序,注:(消费者要开启)
网页中输入:http://localhost:7901/movie/1
{"id":1,"username":"user1","name":"张三","age":20,"balance":100.00}
到此完成开发。
项目中存在的问题
- 真是环境中 提供端的ip和端口是变化的;
restTemplate 中访问的是配置文件中写死的ip,如果提供者ip发成变化,只能修改配置文件才能继续访问。 - 不能进行负载
下一期 解决上面的问题《服务发现》
如有不清楚,请留言。