微服务架构需要考虑的问题为:
在服务发现的问题上,Spring cloud 提供了一个组件 Eureka,服务注册与发现中心,所有的服务都需要在服务与注册中心上进行注册,实现服务之间的可见性。
为了实现Eureka的高可用,解决单点故障的问题,因此,我们需要配置Eureka集群
由于要实现分布式的服务部署,因此,在服务数量居多的情况下,为了方便服务配置文件统一管理,实时更新,全部的配置文件要在分布式配置中心获取
因此采用的解决方案为:GitLab + nexus + 分布式Config+Eureka实现
本次使用Spring cloud的版本为 Finchley.RELEASE
<?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>
</parent>
<groupId>com.haolarn</groupId>
<artifactId>dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<!-- Environment Settings -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring Settings -->
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<spring-cloud-zipkin.version>2.10.1</spring-cloud-zipkin.version>
<spring-cloud-admin-server.version>2.0.1</spring-cloud-admin-server.version>
</properties>
<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>
<!--服务链路追踪 -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin</artifactId>
<version>${spring-cloud-zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>${spring-cloud-zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>${spring-cloud-zipkin.version}</version>
</dependency>
<!--监控中心 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-cloud-admin-server.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-cloud-admin-server.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- Compiler 插件, 设定 JDK 版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- 打包 jar 文件时,配置 manifest 文件,加入 lib 包的 jar 依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<!-- Add directory entries -->
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<!-- resource -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<!-- install -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<!-- clean -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<!-- ant -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>
<!-- dependency -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!-- Java Document Generate -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- YUI Compressor (CSS/JS压缩) -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<jswarn>false</jswarn>
<nosuffix>true</nosuffix>
<linebreakpos>30000</linebreakpos>
<force>true</force>
<includes>
<include>**/*.js
**/ *.css</include>
</includes>
<excludes>
<exclude>**/*.min.js
**/ *.min.css</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<!-- 资源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java
src/main/resources
nexus-releases
Nexus Release Repository
http://192.168.239.146:8081/repository/maven-releases/
nexus-snapshots
Nexus Snapshot Repository
http://192.168.239.146:8081/repository/maven-snapshots/
aliyun-repos
Aliyun Repository
http://maven.aliyun.com/nexus/content/groups/public
true
false
nexus
Nexus Repository
http://192.168.239.146:8081/repository/maven-public/
true
true
sonatype-repos
Sonatype Repository
https://oss.sonatype.org/content/groups/public
true
false
sonatype-repos-s
Sonatype Repository
https://oss.sonatype.org/content/repositories/snapshots
false
true
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
nexus
Nexus Plugin Repository
http://192.168.239.146:8081/repository/maven-public/
true
true
aliyun-repos
Aliyun Repository
http://maven.aliyun.com/nexus/content/groups/public
true
false
因为服务的文件都需要在分布式文件系统中获取,因此首先要设置 Spring-config
<?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>com.haolarn</groupId>
<artifactId>itoken-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../dependencles/pom.xml</relativePath>
</parent>
<artifactId>config</artifactId>
<packaging>jar</packaging>
<name>config</name>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!--服务链路追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!--服务的监控管理 -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.haolarn.config.ConfigApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<!--从私服中获取主要依赖 -->
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://192.168.239.146:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
模仿生产环境,使用这种方法,需要java -jar xxx.jar --spring.profile.active=prod解析jar
配置GitLab的仓库的文件所在地的git地址,使用Https的方式
spring:
application:
name: config
boot:
admin:
client:
url: http://192.168.239.151:8084
cloud:
config:
label: master
server:
git:
uri: http://192.168.239.144:8080/config.git
<!--文件所在目录 -->
search-paths: respo
username: haolarn
password: 12345678
zipkin:
base-url: http://192.168.239.151:9411
server:
port: 8888
eureka:
client:
serviceUrl:
defaultZone: http://192.168.239.151:8761/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
相关配置解释:
- spring.cloud.config.uri:配置服务中心的网址
- spring.cloud.config.name:配置文件名称的前缀
- spring.cloud.config.label:配置仓库的分支
- spring.cloud.config.profile:配置文件的环境标识
- dev:表示开发环境
- test:表示测试环境
- prod:表示生产环境
注意:
配置服务器的默认端口为 8888,如果修改了默认端口,则客户端项目就不能在 application.yml 或 application.properties 中配置 spring.cloud.config.uri,必须在 bootstrap.yml 或是 bootstrap.properties 中配置,原因是 bootstrap 开头的配置文件会被优先加载和配置,切记
<?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>com.haolarn</groupId>
<artifactId>dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../dependencles/pom.xml</relativePath>
</parent>
<artifactId>eureka</artifactId>
<packaging>jar</packaging>
<name>eureka</name>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!--服务链路追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!--分布式配置文件管理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 服务监控管理 -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.haolarn.eureka.EurekaApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
spring:
cloud:
config:
uri: http://192.168.239.151:8888
name: eureka
label: master
profile: prod
从config中获取
spring:
application:
name: itoken-eureka
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
# 表示是否将自己注册到Eureka,因为要构建集群环境,需要将自己注册到集群,所以应该开启
registerWithEureka: true
# 表示是否从Eureka获取注册信息,如果是当一节点,不需要同步其他Eureka节点,则可以设置为False
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.239.151:8761/eureka/,http://192.168.239.151:8861/eureka/,http://192.168.239.151:8961/eureka/
此处配置了三台eureka
将eureka跟config打成jar包上传到服务器上
#以什么镜像为基
FROM openjdk:8-jre
##创建app目录进行存放jar
RUN mkdir /app
##将当前上下文环境下的jar复制到app目录下
COPY itoken-config-1.0.0-SNAPSHOT.jar /app/
##解析jar,用pro文件进行运行
CMD java -jar /app/itoken-config-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod
##暴露8888端口给主机
EXPOSE 8888
docekr.compose.yml的配置
version: '3.1'
services:
itoken-config:
restart: always
image: 192.168.239.147:5000/itoken-config
container_name: itoken-config
ports:
- 8888:8888
~
FroM openjdk:8-jre
RUN mkdir /app
COPY itoken-eureka-1.0.0-SNAPSHOT.jar /app/
CMD java -jar /app/itoken-eureka-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod
EXPOSE 8761
version: '3.1'
services:
itoken-eureka-1:
restart: always
image: 192.168.239.147:5000/itoken-eureka
container_name: itoken-eureka-1
ports:
- 8761:8761
itoken-eureka-2:
restart: always
image: 192.168.239.147:5000/itoken-eureka
container_name: itoken-eureka-2
ports:
- 8861:8761
itoken-eureka-3:
restart: always
image: 192.168.239.147:5000/itoken-eureka
container_name: itoken-eureka-3
ports:
- 8961:8761
docker.compose up -d 运行两个服务
查看容器
访问
http://192.168.239.151:8761/
http://192.168.239.151:8861/
http://192.168.239.151:8961/
都能进行启动eureka