Netflix(奈飞或网飞,这家公司的企业文化值得学习),推出的SpringCloud版本,可以说是早期比较成熟的微服务解决方案。
后来Netflix放弃了SpringCloud相关服务组件的维护更新,Alibaba版SpringCloud就顺势而为。
本篇简单记录下Netflix版SpringCloud的工程结构,权当编程思想借鉴,不作技术参考(可以说已是过时了的技术)。
1. 单机版eureka
eureka,意为找到了,发现了,作为服务注册中心,就类似zookeeper的角色,或者AlibabaCloud中的nacos。
1.1 pom
<?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.demo</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.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>
<!--cloud与SpringBoot LTS版兼容,如:1.5.12.RELEASE-->
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
1.2 yml
server:
port: 8888
spring:
application:
name: eureka-server
## 默认情况下eureka server也是一个eureka client ,必须要指定一个 server
## 通过eureka.client.registerWithEureka:false和fetchRegistry:false
## 来表明自己是一个eureka server
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
1.3 application
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 启动服务注册中心
* 访问 http://localhost:8888可以查看server服务注册情况
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
1.4 monitor
2. 集群版eureka
作为服务注册中心,如果是单机版,就会存在单点故障。因此,实际生产环境中,eureka也是集群部署的。
eureka集群,其实也就是eureka服务部署多个节点,相互注册发现,关联起来即可。下面以两个eureka服务节点集群为例。
2.1 pom (同1.1)
2.2 yml
准备工作(本地环境域名解析,实际生产环境为真实域名DNS解析):
## 修改hosts,
## [windows] c:/windows/System32/drivers/etc/hosts,
## [linux] vim /etc/hosts,加上:
## 127.0.0.1 peer1
## 127.0.0.1 peer2
application.yml
spring:
application:
name: eureka-server
## 启动不同的服务节点时,对应激活节点配置文件即可
## java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer2
profiles:
active: peer1
application-peer1.yml
server:
port: 8888
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2:8889/eureka/
application-peer2.yml
server:
port: 8889
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:8888/eureka/
2.3 application
# 进入pom文件所在目录位置执行cmd打包(或直接使用IDEA工具打包)
mvn package
java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer1
java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer2
/**
* 启动服务注册中心
* http://localhost:8888/ 可以查看第一个server服务注册情况
* http://localhost:8889/ 可以查看第二个server服务注册情况
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
先后启动peer1, peer2两个节点服务,发现peer1刚启动时会报错,这是因为peer1会到peer2上去注册服务,发现peer2服务尚未启动,所以报错了,可以忽略不计,待peer2服务节点启动后,peer1就会发现peer2并相互注册服务,关联到一起成为集群的注册中心服务。
2.4 monitor
访问peer1 :
http://peer1:8888
访问peer2 :
http://peer2:8889
3. eureka client (micro-service)
这里所说的eureka client就是所谓的具有专属业务功能模块划分的微服务了
3.1 pom (同1.1)
3.2 yml
server:
port: 8887
## 需要指明spring.application.name,这个很重要,
## 这在以后的服务与服务之间相互调用一般都是根据这个name
spring:
application:
name: eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8888/eureka/,http://peer2:8889/eureka/
3.3 application
启动eureka-client 微服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
3.4 monitor
任意访问peer1 或 peer2注册中心服务节点,都可以看到刚启动的eureka-client微服务已经注册上来了。