之前使用dubbo搭建了一个系统,不过搭的不是很好,最近开始学Spring Cloud微服务,我感觉Spring Cloud要比dubbo要好用多了,以前在搭dubbo的时候还没有写博客的习惯,很多东西都没有去总结久而久之就忘记了,所以这次学习Spring Cloud我会把自己学习的整个过程都记录下来,既能给自己增加记忆,方便查看,还能和大家分享。
好了,废话不多说,开始Spring Cloud的学习,希望和大家共同进步。
准备环境
- JDK 1.8
- IDEA 社区版
- spring boot 2.0.3
- maven 3.2.2
创建服务注册中心
使用IDEA快速创建一个Spring Boot工程,添加Eureka Server组件。【IDEA 社区版 创建spring cloud项目】
Eureka Server依赖
我选择spring boot的版本是2.0.3,spring cloud的版本是Finchley.SR3,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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>clarezhou.example</groupId>
<artifactId>eureka-server-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server-test</name>
<description>Eureka Server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- Eureka Server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- javaBean简化 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
</plugins>
</build>
</project>
这个依赖和spring boot 1.5版本有一些区别,1.5引入的依赖是spring-cloud-starter-eureka-server,2.0引入的依赖则改为了spring-cloud-starter-netflix-eureka-server
服务注册中心启动
服务注册中心启动非常的简单,只需要在Spring Boot的入口类添加一个@EnableEurekaServer注解,代码如下:
package clarezhou.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerTestApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerTestApplication.class, args);
}
}
服务注册中心配置信息
服务注册中心的配置还是很多的,我这里就列举几个简单的配置,更多的配置大家可以去官方文档里查找,配置文件application.properties
#端口号
server.port=8081
eureka.instance.hostname=localhost
#向注册中心注册服务
eureka.client.registerWithEureka=false
# 检索服务
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#在Eureka服务器获取不到集群里对等服务器上的实例时,需要等待的时间
server.waitTimeInMsWhenSyncEmpty=0
#自我保护模式
server.enableSelfPreservation=false
这里配置文件需要说明一下几点:
- server.port 是服务注册中心的端口号 自己设置就好
- eureka.client.registerWithEureka 是否向注册中心注册服务 默认是true
spring boot1.5 这个配置是这样的:eureka.client.register-with-eureka 略微有点区别 - eureka.client.fetchRegistry 与 serviceUrl 都是把1.5版本的下划线去掉了
- eureka.client.fetchRegistry=false 表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务
结果测试
运行EurekaServerTestApplication这个启动类,在浏览器输入http://localhost:8081/ 就能看到如下页面:
如上,Eureka Server服务注册中心就搭建完成了。
创建服务提供者
服务注册中心搭建好之后,我们就开始搭建服务提供者了。
搭建提供者项目
跟创建Eureka Server一样,使用IDEA快速创建一个spring boot的项目,选择Eureka Discovery Client组件,其他的根据自己需要选择组件就好。
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>clarezhou.example</groupId>
<artifactId>eureka-client-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-client-test</name>
<description>eureka-client</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
</plugins>
</build>
</project>
同样的spring boot 2.0版本和1.5版本在这个依赖包上也有一点区别,从spring-cloud-starter-eureka 改成了spring-cloud-starter-netflix-eureka-client
注册提供者服务
创建一个Controller,我创建一个测试的HelloController
package clarezhou.example.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
public class HelloController {
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index() {
List<ServiceInstance> instances = client.getInstances("client-demo");
for (int i = 0; i < instances.size(); i++) {
log.info("/hello,host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
}
return "Hello World";
}
}
在Spring Boot 入口类,添加@EnableDiscoveryClient注解来激活Eureka中的DiscoveryClient实现(因为我们在HelloController中注入了DiscoveryClient)。
package clarezhou.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientTestApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientTestApplication.class, args);
}
}
修改配置
#端口号
server.port=9081
# 服务名
spring.application.name=client-demo
#服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
启动服务 测试
服务启动之后,在浏览器输入http://localhost:8081/ 看到Eureka Server服务注册中心我们的client-demo服务已经注册上来了,
输入http://localhost:8081/eureka/apps 可以看到如下信息
然后在浏览器输入http://localhost:9081/hello/,浏览器上会打印Hello World,控制台会打印我们的日志信息
总结
如此,我们入门级的服务注册中心就搭建完成了,这个服务注册中心暂时是单节点的,如果一个服务中心瘫痪了整个网站就挂了,后面我会继续完善这个服务注册中心,避免一些系统问题。