前言:最近在学习关于微服务方面的相关内容,所以写点自己的实际项目搭建经验记录一下,顺带希望能帮助到喜欢这个方面的朋友吧!
准备工作:
IDE:推荐使用STS,如使用eclipse或者IDEA出现maven引入报错,请参照报错,补全缺失包即可!
项目源码地址:https://github.com/ksuth/MicroService.git
如果没有github账号请先注册~~具体使用方式请参考:http://www.cnblogs.com/shuyuq/p/9243915.html
实在不会就留个邮箱吧~看到了我会发的~~
接下来我们进入正题:
(一)简单的服务消费项目:
1.构建一个简单的spring-boot项目:
方式1:使用spring官网构建:https://start.spring.io
方式1:使用spring官网构建:https://start.spring.io
保存到本地后,解压缩导入。
方式二:使用STS构建:直接new个spring-start项目即可。
2.修改pom.xml文件:
<groupId>com.study.springcloud</groupId>
<artifactId>springcloud-ordinary-order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-ordinary-order-service</name>
<description>传统的订单服务实例</description>
<!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</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</artifactId>
</dependency>
<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>
</dependencies>
<!-- 引入spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
@Entity
public class OrderInfo {
//订单ID
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//订单号
@Column
private String orderNo;
//订单备注
@Column
private String orderRemark;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getOrderRemark() {
return orderRemark;
}
public void setOrderRemark(String orderRemark) {
this.orderRemark = orderRemark;
}
}
@Repository
public interface OrderService extends JpaRepository<OrderInfo, Long> {
}
5.添加web层入口:
@RestController
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("findOrderById/{id}")
public OrderInfo findById(@PathVariable Long id) {
return orderService.findOne(id);
}
}
- application.yml
server:
port: 8000
spring:
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource: # 指定数据源
platform: h2 # 指定数据源类型
schema: classpath:order_schema.sql # 指定h2数据库的建表脚本
data: classpath:order_data.sql # 指定h2数据库的数据脚本
logging: # 配置日志级别,让hibernate打印出执行的SQL
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
- order_data.sql
insert into order_Info (id, order_No, order_Remark) values (1, '20171111111111', '张三的订单', );
insert into order_Info (id, order_No, order_Remark) values (2, '20172222222222', '李四的订单', );
insert into order_Info (id, order_No, order_Remark) values (3, '20173333333333', '王五的订单', );
- order_schema.sql
drop table order_Info if exists;
create table order_Info (id bigint generated by default as identity, order_No varchar(50), order_Remark varchar(100), primary key (id));
7.启动项目:执行项目中main方法
8.检查结果:
看到这个结果表明服务建立成功。
接下来我们要建立一个简单的消费服务来消费刚才所建立的项目!
1.构建简单的spring-boot项目(方法不再赘述)
2.修改pom.xml文件:
<groupId>com.study.springcloud</groupId>
<artifactId>springcloud-ordinary-user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-ordinary-user-service</name>
<description>传统的用户服务实例,做为消费者使用</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 引入内存数据库 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<!-- 引入Maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
@Entity
public class OrderInfo {
//订单ID
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//订单号
@Column
private String orderNo;
//订单备注
@Column
private String orderRemark;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getOrderRemark() {
return orderRemark;
}
public void setOrderRemark(String orderRemark) {
this.orderRemark = orderRemark;
}
}
@RestController
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/user/findOrderById/{id}")
public OrderInfo findOrderById(@PathVariable Long id){
return (OrderInfo) restTemplate.getForEntity("http://localhost:8000/findOrderById/"+id, OrderInfo.class).getBody();
}
}
application.yml:
server:
port: 8010
此时启动项目访问如下路径:http://localhost:8010//user/findOrderById/2
如实现上图结果表示消费服务建立成功!
总结:此时我们可以看到,我们成功的在springcloud-ordinary-user-service项目中去使用了springcloud-ordinary-order-service的接口,得到了我们想要的结果!这个就是微服务项目的起点~当然这是最简单的一个,只是让我们理解服务于消费是怎么样的关系,不能运用于实际工作!接下来我们继续!
在之前两项目的基础之上,我们现在需要加上微服务的核心:注册于发现!
让我们先来写一个注册服务:
项目名称:springcloud-eureka-server
1.新建spring-boot项目
2.修改pom.xml文件:
<groupId>com.study.springcloud</groupId>
<artifactId>springcloud-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-eureka-server</name>
<description>服务注册与发现</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</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</artifactId>
</dependency>
<!-- 引入Spring Cloud Eureka依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 引入Maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaServerApplication.class, args);
}
}
server:
port: 8761 # 指定该Eureka实例的端口,默认为8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
6.启动项目,访问如下地址:http://localhost:8761/
可以看到我们这个简单的注册服务功能就实现了~
既然已经有了注册中心,下一步我们肯定就是对刚才已经写好的两个项目进行改造,将服务注册到注册中心上去!
(二)基于注册中心的服务消费项目:
项目名称:springcloud-order-service
构建项目参照上述内容,需修改内容如下:
1.pom.xml
<groupId>com.study.springcloud</groupId>
<artifactId>springcloud-order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-order-service</name>
<description>订单服务实例</description>
<!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</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</artifactId>
</dependency>
<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>
<!-- 引入Spring Cloud Eureka 客服端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</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>
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudOrdinaryOrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudOrdinaryOrderServiceApplication.class, args);
}
}
server:
port: 8020
spring:
application:
name: springcloud-order-service
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource: # 指定数据源
platform: h2 # 指定数据源类型
schema: classpath:order_schema.sql # 指定h2数据库的建表脚本
data: classpath:order_data.sql # 指定h2数据库的数据脚本
logging: # 配置日志级别,让hibernate打印出执行的SQL
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
启动服务前,请保证我们之前写的注册中心服务为启动状态!此时我们可以在注册中心看到:
这表示我们新的服务注册成功了!
同样的,我们可以将之前的消费服务注册到注册中心!项目改造如下:
项目名称:springcloud-user-service
项目修改:
1.pom.xml
<groupId>com.study.springcloud</groupId>
<artifactId>springcloud-user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-user-service</name>
<description>传统的用户服务实例,做为消费者使用</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 引入内存数据库 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- 引入Spring Cloud eureka 客服端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<!-- 引入SpringCloud 依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 引入Maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
@RestController
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private EurekaClient discoveryClient;
public String serviceUrl() {
InstanceInfo instance = discoveryClient.getNextServerFromEureka("SPRINGCLOUD-ORDER-SERVICE", false);
return instance.getHomePageUrl();
}
@GetMapping("/user/findOrderById/{id}")
public OrderInfo findOrderById(@PathVariable Long id){
return (OrderInfo) restTemplate.getForEntity(this.serviceUrl()+"/findOrderById/"+id, OrderInfo.class).getBody();
}
}
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudOrdinaryUserServiceApplication {
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudOrdinaryUserServiceApplication.class, args);
}
}
server:
port: 8030 #设置端口号
spring:
application:
name: springcloud-user-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
完成上述改造后我们可以启动服务,检查注册中心如下:
此时我们可以看到之前的两个服务都已经纳入到注册中心!
我们访问消费服务:http://localhost:8030//user/findOrderById/3
尾声:
关于一个简单的微服务项目搭建就给大家介绍到这里。GitHub中给出了基于权限验证的项目代码,如果对这一块感兴趣的朋友不妨可以参考下~最后对于微服务项目的高可用做个简单的介绍,我们一般的实现方式为通过启动两个注册中心,相互注册,实现服务的相互同步来达到高可用的目的~如果有更好的方式欢迎提出来一起探讨~