学无止境,继续前进
今天开始学习SpringCloud的相关知识,作为初学者不想过多的深究原理和底层,把学会使用作为本节的目标。
简单地说,微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统 拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP 的RESTful API进行通信协作。被拆分成的每一个小型服务都围绕着系统中的某一项或一 些羁合度较高的业务功能进行构建,并且每个服务都维护着自身的数据存储、业务开发、 自动化测试案例以及独立部署机制。由于有了轻量级的通信协作基础,所以这些微服务可 以使用不同的语言来编写。
单体架构的优点:
单体架构的缺点:
SOA架构的优点:
SOA架构的缺点:
SpringCloud五大组件
Spring Boot | Spring Cloud |
---|---|
1.2.x | Angel版本 |
1.3.x | Brixton版本 |
1.4.x stripes | Camden版本 |
1.5.x | Dalston版本、Edgware版本 |
2.0.x | Finchley版本 |
2.1.x | Greenwich.SR2 |
SpringBoot和SpringCloud的版本必须对应。详细请查看官方对应数据:https://start.spring.io/actuator/info,也许你会看着很乱,不要着急,找一个JSON转换就OK啦。
创建Maven父工程,添加pom配置
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
创建Eureka Server子工程,配置pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在server启动类添加注解@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
创建application.yml文件
server:
port: 8001
eureka:
instance:
hostname: localhost
client:
#服务端server禁止自己注册自己
register-with-eureka: false
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: false
service-url: #指定交互和通信的url
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
#当eureka server启动的时候,不能从对等节点获取instance注册信息的情况,应等待多长时间。
wait-time-in-ms-when-sync-empty: 0
#服务端开启自我保护模式。无论什么情况,服务端都会保持一定数量的服务。避免client与server的网络问题,而出现大量的服务被清除。
enable-self-preservation: false
创建Eureka Client子工程,配置pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--添加web,不添加启动时自动停止-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
client启动类添加注解@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
创建application.yml文件
server:
port: 8101
#客户端必须起名字,否则注册为unknow
spring:
application:
name: client
eureka:
client:
service-url:
#填入server通信url
defaultZone: http://localhost:8001/eureka/
先后启动Server和Client,访问http://localhost:8001,可以看到如图的界面:
访问 http://127.0.0.1:8001/eureka/apps 能够看到所有注册到Eureka Server的Client信息。
访问 http://127.0.0.1:8001/eureka/apps/${applicationName} 能够看到对应Client注册到Eureka Server的信息。
注册中心到这里了,后面继续!!!