微服务架构解决方案使用 spring cloud ,因为spring cloud目前版本迭代很是快,bug也有很多,这里以目前最新的版本 Camden.SR2 为例。java
spring cloud netflix 是在netflix开源的一套微服务构建工具之上进行了封装。依靠注解和自动配置便可完成经常使用的配置,从spring boot开始,spring对配置作了大量的简化,并实现了自动化配置,能够更快速的建立项目。git
在微服务架构中,会把一个大的单体服务拆分红若干个功能单一的微服务。微服务的数量根据业务而定,可能会很大,这些微服务的运行状态,以及服务之间的通讯须要一个统一的注册中心进行管理。spring
建立一个maven工程,在 pom.xml 文件中加入spring cloud 依赖:docker
<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 配置文件,在配置文件内容:apache
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.javatomcat
@EnableEurekaServer @SpringBootApplication public class ServiceRegistryApplication { public static void main(String[] args) { SpringApplication.run(ServiceRegistryApplication.class, args); } }
运行 main 方法,启动注册中心。启动完成后,访问http://localhost:8000, 能够打开eureka的管理页面。
安全
复制 application.yml,重命名为 application-docker.yml,内容不须要作修改。
修改 application.yml 中的 spring 节点为:架构
spring: application: name: @project.artifactId@ profiles: active: @activatedProperties@
这里增长了 profiles 的配置,在maven打包时选择不一样的profile,加载不一样的配置文件app
在pom.xml文件中增长:dom
<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