您的位置:首页 > 运维架构 > Docker

基于docker部署的微服务架构(一):服务注册中心

2016-12-21 17:10 741 查看
摘要: eureka服务注册中心

前言

微服务架构解决方案使用 spring cloud ,由于spring cloud目前版本迭代非常快,bug也有不少,这里以目前最新的版本 Camden.SR2 为例。

spring cloud netflix套件

spring cloud netflix 是在netflix开源的一套微服务构建工具之上进行了封装。依靠注解和自动配置即可完成常用的配置,从spring boot开始,spring对配置做了大量的简化,并实现了自动化配置,可以更快速的创建项目。

服务注册和发现的必要性

在微服务架构中,会把一个大的单体服务拆分成若干个功能单一的微服务。微服务的数量根据业务而定,可能会很大,这些微服务的运行状态,以及服务之间的通信需要一个统一的注册中心进行管理。

实现一个简单的服务注册中心

创建一个maven工程,在 pom.xml 文件中加入spring cloud 依赖:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在 resources 目录中创建 application.yml 配置文件,在配置文件内容:

spring:
application:
name: @project.artifactId@

server:
port: 8000

eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/ registerWithEureka: false
fetchRegistry: false
server:
enableSelfPreservation: false

在 java 目录中创建一个包 demo ,在包中创建启动入口 ServiceRegistryApplication.java

@EnableEurekaServer
@SpringBootApplication
public class ServiceRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}

运行 main 方法,启动注册中心。启动完成后,访问http://localhost:8000, 可以打开eureka的管理页面。



使用docker-maven-plugin打包并生成docker镜像

复制 application.yml,重命名为 application-docker.yml,内容不需要做修改。

修改 application.yml 中的 spring 节点为:

spring
7fe0
:
application:
name: @project.artifactId@
profiles:
active: @activatedProperties@

这里增加了 profiles 的配置,在maven打包时选择不同的profile,加载不同的配置文件

在pom.xml文件中增加:

<properties>
<java.version>1.8</java.version> <!-- 指定java版本 -->
<!-- 镜像前缀,推送镜像到远程库时需要,这里配置了一个阿里云的私有库 -->
<docker.image.prefix>
registry.cn-hangzhou.aliyuncs.com/ztecs
</docker.image.prefix>
<!-- docker镜像的tag -->
<docker.tag>demo</docker.tag>

<!-- 激活的profile -->
<activatedProperties></activatedProperties>
</properties>

<profiles>
<!-- docker环境 -->
<profile>
<id>docker</id>

<properties>
<activatedProperties>docker</activatedProperties>
<docker.tag>docker-demo-${project.version}</docker.tag>
</properties>
</profile>
</profiles>

<build>
<defaultGoal>install</defaultGoal>
<finalName>${project.artifactId}</finalName>

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<!-- 配置spring boot maven插件,把项目打包成可运行的jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>

<!-- 打包时跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

<!-- 配置docker maven插件,绑定install生命周期,在运行maven install时生成docker镜像 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>tag</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 修改这里的docker节点ip,需要打开docker节点的远程管理端口2375,
具体如何配置可以参照之前的docker安装和配置的文章 -->
<dockerHost>http://docker节点ip:2375</dockerHost>
<imageName>${docker.image.prefix}/${project.build.finalName}</imageName>
<baseImage>java</baseImage>
<!-- 这里的entryPoint定义了容器启动时的运行命令,容器启动时运行
java -jar 包名 , -Djava.security.egd这个配置解决tomcat8启动时,
因为需要收集环境噪声来生成安全随机数导致启动过慢的问题-->
<entryPoint>
["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/${project.build.finalName}.jar"]
</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<image>${docker.image.prefix}/${project.build.finalName}</image>
<newName>${docker.image.prefix}/${project.build.finalName}:${docker.tag}</newName>
<forceTags>true</forceTags>
<!-- 如果需要在生成镜像时推送到远程库,pushImage设为true -->
<pushImage>false</pushImage>
</configuration>
</plugin>
</plugins>
</build>

选择
docker
profile,运行
mvn install -P docker
,打包项目并生成docker镜像,注意docker-maven-plugin中的
<entryPoint>
标签里的内容不能换行,否则在生成docker镜像的时候会报错


运行成功后,登录docker节点,运行
docker images
应该可以看到刚才打包生成的镜像了。

运行
docker run -it IMAGE_ID
就可以运行镜像了。

demo源码 spring-cloud-1.0/service-registry-demo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息