springclound快速入门
一、开始使用springcloud实战微服务1.工具及软件版本选择jdk 1.8springboot 1.5.9springcloudedgwarespringtool suite / ideamaven 3.5.02.服务提供者与服务消费者3.编写服务提供者1.创建maven项目2.pom.xml 文件内容如下<?xml version="1.0" encod.
·
一、开始使用springcloud实战微服务
1.工具及软件版本选择
jdk 1.8
springboot 1.5.9
springcloud edgware
springtool suite / idea
maven 3.5.0
2.服务提供者与服务消费者
3.编写服务提供者
1.创建maven项目
2.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.qy</groupId>
<artifactId>cloud01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</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>
3.创建实体类
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Integer 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 Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
4.创建dao
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
5.创建controller
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;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/{id}")
public User findById(@PathVariable Long id) {
User findOne = this.userRepository.findOne(id);
System.out.println(id+"***********");
return findOne;
}
}
6.编写启动类
@SpringBootApplication
public class Cloud01Application {
public static void main(String[] args) {
SpringApplication.run(Cloud01Application.class, args);
}
}
7.编写配置文件application.yml
server:
port: 8000
spring:
jpa:
generate-ddl: false
show-sql: true
datasource: # 指定数据源
#配置数据库的路径
url: jdbc:mysql://localhost:3306/java1807?useUnicode=true&characterEncoding=UTF-8&useSSL=true
#数据库登录名
username: root
#登陆密码
password: hnqy
driver-class-name: com.mysql.jdbc.Driver
8.测试
http://localhost:8000/1
备注:对应的sql代码如下:
drop table user if exists user;
create table user
(
id int AUTO_INCREMENT PRIMARY KEY ,
username varchar(40),
name varchar(20),
age int(3),
balance decimal(10,2)
);
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);
4.编写服务消费者
1.创建maven项目
2.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.qy</groupId>
<artifactId>cloud01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</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>
3.创建实体类
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
//省略getter setter,写的时候自己加上
}
4.无需创建dao
5.创建controller
@RestController
public class MovieController {
private static final Logger logger = LoggerFactory.getLogger(MovieController.class);
@Autowired
private RestTemplate restTemplate;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
//return this.restTemplate.getForObject("http://cloud/" + id, User.class);
return this.restTemplate.getForObject("http://localhost:8000/" + id, User.class);
}
}
6.编写启动类
@SpringBootApplication
public class SpringCustomerApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringCustomerApplication.class, args);
}
}
7.编写配置文件application.yml
server:
port: 8010
spring:
application:
name: spring-customer
user:
userServiceUrl: http://localhost:8000/
8.测试
http://localhost:8010/user/1
更多推荐
已为社区贡献2条内容
所有评论(0)