1.Spring Cloud 架构
Spring Cloud 是一个基于spring boot 实现的微服务架构工具
SpringCloud提供了一整套为分布式服务解决方案,主要但不限于:
1、Eureka:服务注册中心、服务治理中心(分布式服务核心组件)
2、zuul:加在整个微服务最前沿的防火墙和代理器,隐藏微服务结点IP端口信息,加强安全保护
3、Ribbon:客户端负载均衡器,用于调用集群
4、Feign客户端:声明示远程服务调用
5、Config:分布式配置中心
2.Spring Cloud构建
那么构建的一个spring cloud的项目 起码需要三个内容。分别是 服务注册中心、服务提供方、服务消费方。
1.搭建服务注册中心 EurekaServer
Spring Cloud Eureka 是Spring Cloud Netflix 微服务套件中的一部分,是基于Netflix Eureka 做的二次封装,主要为我们微服务提供服务治理的功能,由于Spring Cloud 是一个基于spring boot 实现的微服务架构工具,所以我们通过简单的引入依赖和注解就能让spring boot 构建的微服务和Eureka进行整合。
Eureka主要有服务端和客户端组成,提供服务的注册和发现功能。使用RESTful api 进行通信
构建Eureka服务端即服务注册中心
首先创建一个spring boot 工程,命名为eurekaserver pom添加如下依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--服务监控管理 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Spring boot 核心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.4.0.RELEASE</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>
<!--打包跳过测试插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>在application.yml中配置 注册中心的相关参数
server:
port: 8761 #端口号
eureka:
instance:
hostname: localhost #注册中心实例名
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #服务注册地址
fetch-registry: false
register-with-eureka: false #是否将本服务注册到服务中心
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: 5000 #驱逐下线的服务,间隔,5秒,默认是60在默认情况下 注册中心会将自己作为服务也给注册起来,但在单节点中我们并不需要,我们通过
eureka.client.register-with-eureka: false:禁止注册中心将自己注册起来。
eureka.client.fetch-registry: false :由于注册中心重要是维护服务的实例,并不需要去发现服务,所以我们将它置位false。
但在高可用服务注册中心集群中。需要将自己作为服务向其他服务注册中心注册自己,所以这两个可以设置为true
最后 我们在启动类里添加 @EnableEurekaServer注解,启动一个服务注册中心提供给其他应用进行对话
@EnableEurekaServer
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@ComponentScan(basePackages={"com.eureka.server"})
public class App {
public static void main( String[] args )
{
SpringApplication springApplication = new SpringApplication(App.class);
springApplication.run(args);
}
}启动:
此时我们看得到 还没有可用的服务
接下来我们需要构建一个服务提供方和服务消费方
2.搭建服务提供方postmanager
为了模拟真实环境中的微服务,我们重新建立springboot项目,服务命名为postmanager
在pom 文件中增加Eureka客户端的依赖
<!-- Spring boot 父引用-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- Spring boot 核心web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.4.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>在配置文件application.properties中添加Eureka参数配置
server.port=8088
#服务名
spring.application.name=postmanager
#指定注册中心地址
eureka.client.serviceUrl.defalultZone=http://localhost:8761/eureka/
#客户端配置 ,eureka客户端需要向eureka服务器发送心跳的频率 (Spring Cloud默认该配置是 30s)
eureka.instance.lease-renewal-interval-in-seconds=10
# 客户端配置,eureka服务器在接收到最后一个心跳之后等待的时间,然后才能从列表中删除此实例 (Spring Cloud默认该配置是 90s)
eureka.instance.lease-expiration-duration-in-seconds=30
#指定实例名 命名方式
eureka.instance.instance-id=${spring.application.name}:${random.int}主要配置该客户端服务名称、注册中心的位置、以及服务注册中心客户端的心跳频率,心跳是客户端向服务端表明存活的方式 后面我会详情介绍。由于一个服务可能会有多个实例,我们需要区分这些实例,所在在服务实例名后面添加的随机数
我们需要在启动函数上添加注解@EnableDiscoveryClient 后续我们会介绍具体的源码实现
@EnableFeignClients
@EnableDiscoveryClient
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@ComponentScan(basePackages={"com.postmanager"})
public class App {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(App.class);
springApplication.run(args);
}
}
最后我们向外提供一个restful服务
@Controller
public class FacadeTest {
@Value("${spring.application.name}")
private String servername;
@RequestMapping("/getprovider")
@ResponseBody
public String getprovider(){
return "hello i am "+servername;
}
}启动服务:
我们可以看到注册中心已经有我们的服务postmanager了
3.搭建服务消费方postpublish
同样我们构建一个springboot工程,使用Feign 来做服务调用,Feign整合了Ribbon和Hystrix,提供了服务消费和负载均衡。后续我们会介绍。引入pom
<!-- Spring boot 父引用-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- Spring boot 核心web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.4.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>application.properties配置:
server.port=8089
#服务名
spring.application.name=postpublish
#指定注册中心地址
eureka.client.serviceUrl.defalultZone=http://localhost:8761/eureka/
#客户端配置 ,eureka客户端需要向eureka服务器发送心跳的频率 (Spring Cloud默认该配置是 30s)
eureka.instance.lease-renewal-interval-in-seconds=10
# 客户端配置,eureka服务器在接收到最后一个心跳之后等待的时间,然后才能从列表中删除此实例 (Spring Cloud默认该配置是 90s)
eureka.instance.lease-expiration-duration-in-seconds=30
#指定实例名 命名方式
eureka.instance.instanceId=${spring.application.name}:${random.int}
eureka.client.fetch-registry=true和服务提供方一样指定服务注册中心的基本信息
启动函数添加:@EnableFeignClients注解,开启Spring Cloud Feign 支持功能
@EnableFeignClients
@EnableDiscoveryClient
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@ComponentScan(basePackages={"com.postpublish"})
public class App {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(App.class);
springApplication.run(args);
}
}
消费服务,在服务消费端我们需要定义接口来绑定服务提供方的服务
@FeignClient(value="postmanager")
@RequestMapping("/facade")
public interface RemoteApiTest {
@RequestMapping("/getProvider")
public String getProvider();
}使用@FeignClient来绑定服务提供方注册的服务名称postmanager,@RequestMapping路由服务提供方服务的具体位置。
定义测试:
@Controller
@RequestMapping("/Hello")
public class Hello {
@Autowired
private RemoteApiTest remoteApiTest;
@RequestMapping("/hello")
@ResponseBody
public String getProvider(){
return remoteApiTest.getProvider();
}
}启动postpublish
我们发现 注册中心有两个服务,分别是postmanager和postpublish
访问postpublsh的测试:http://localhost:8089/Hello/hello
postpublsh调用了postmanager提供的服务了
通过以上Spring Cloud的的构建我们大致了解springcloud的服务注册和服务消费的过程
后续将介绍服务注册的详情和源码分析