首先我的博客记理论知识很少,大家对spring boot、spring cloud 、分布式 、微服务什么的一点概念都没有的还请先去百度看看理论,知道了是做什么用的,然后再写下demo ,这样学起来才没有那么迷糊!
我一般写下的demo都是我踩了很多坑,或者说很多人在其他地方写下的demo搬过来根本运行不起来,然后我经过处理和查询,整理了一下!
方便我以后再回来查找,也方便有些小伙伴入门的demo!
我就简略的介绍一个这个demo;就是spring cloud 中的一个注册服务器 、注册管理中心 会自动监控!
这个demo是我从官网上面找下来的,然后注释了一些用不上的代码和配置!
我这里采用的idea编辑器!
第一步:我们建立一个spring boot项目
如下图:一直next.......最后Finish
第二步:修改maven的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><!--描述这个POM文件是遵从哪个版本的项目描述符。--><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>demo project for Spring Boot</description><parent><!--父级依赖,继承--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><!--引入依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><!--声明依赖,如果dependencies中没有声明版本就会来这里面找--><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.BUILD-SNAPSHOT</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><properties><!--定义的标签属性可以在其他地方读取--><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><start-class>com.example.demo.DemoApplication</start-class><java.version>1.8</java.version><!-- <docker.image.prefix>springcloud</docker.image.prefix>--></properties><build><plugins><!-- <plugin><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId><version>0.2.3</version><configuration><baseImage>openjdk:8-jre-alpine</baseImage><imageName>${docker.image.prefix}/${project.artifactId}</imageName><exposes>8761</exposes><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></configuration></plugin>--><!-- <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><!– defined in spring-cloud-starter-parent pom (as documentation hint),but needs to be repeated here –><configuration><requiresUnpack><dependency><groupId>com.netflix.eureka</groupId><artifactId>eureka-core</artifactId></dependency><dependency><groupId>com.netflix.eureka</groupId><artifactId>eureka-client</artifactId></dependency></requiresUnpack></configuration></plugin>--><!-- <plugin><groupId>pl.project13.maven</groupId><artifactId>git-commit-id-plugin</artifactId><configuration><failOnNoGitDirectory>false</failOnNoGitDirectory></configuration></plugin><plugin><!–skip deploy (this is just a test module) –><artifactId>maven-deploy-plugin</artifactId><configuration><skip>true</skip></configuration></plugin>--></plugins></build><!-- <!–相当于配置远程仓库–><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/libs-snapshot</url><!–是否自动更新–><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>spring-releases</id><name>Spring Releases</name><url>https://repo.spring.io/libs-release</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><!–插件仓库–><pluginRepositories><pluginRepository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/libs-snapshot-local</url><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone-local</url><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories>--></project>
以上的pom文件你复制进去了会发现很多注释掉了。先别管
第三步:修改 application.properties,文件内容如下:
server.port=3333eureka.instance.hostname=localhost #不要向注册中心注册自己 eureka.client.register-with-eureka=false #禁止检索服务 eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
第四步:在启动类上加上这个注解@EnableEurekaServer
package com.example.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer @SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }
到了这一步所有的配置和代码已经写完了!
注意:如果你的项目现在启动不起来,那就把pom文件的注释全部放开,会自动从配置的仓库里面下载jar包
一切正常后,我们通过main方法启动,通过上面的配置,我们打开浏览器访问 http://localhost:3333
会出现如下界面