spring boot 要搭建一个 web 项目特别简单。只需要配置一下 pom.xml 然后几行代码就能搭建一个web项目,想让我们来搭建一个注册中心
一、注册中心 源码:https://github.com/cyyinfo/eureka-server.git
第一步新建一个 maven 项目 (这一步 具体细节省略不会的可以自行google)
第二步 配置 pom.xml
<!--基于Springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<!--设置字符编码及java版本-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--增加eureka-server的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--用于测试的,本例可省略-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
第二步 添加配置文件 application.properties
spring-boot 会自动在项目的classpath目录下寻找 application.properties 并且自动加载。
server.port=8888
spring.application.name=eureka-server
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8888/eureka
第三步 写代码
/**
* 注册中心 其他服务需要注册到该中心,统一调度
* @author cyy
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
代码是不是特别少?
第四步、 运行代码。
直接右键 run 就行,不需要配置各种web容器,因为 spring-boot 内置了 。是不是特别方便?
第五步 可以看运行效果了
在浏览器输入 http://localhost:8888 效果如下图
二、在注册中心中添加服务 源码:https://github.com/cyyinfo/eureka-client-01.git
第一步新建一个 maven 项目 (这一步 具体细节省略不会的可以自行google)
第二步、添加 pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!-- 这里和注册中心有所不同 其他都一样 -->
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<!-- 这里和注册中心有所不同 其他都一样 -->
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
第三步、写代码
/**
* Hello world!
*
* eureka 客户端,对外提供服务,需要注册到注册中心。
*
*/
@SpringBootApplication
@EnableEurekaClient // 注意这里和注册中心有所不同
public class EurekaClientApplication {
public static void main( String[] args ){
SpringApplication.run(EurekaClientApplication.class, args);
}
}
第四步、写配置文件
server.port=8887
eureka.instance.hostname=localhost
spring.application.name=eureka-client-01
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8888/eureka
第五步、 运行代码。
直接右键 run 就行,不需要配置各种web容器,因为 spring-boot 内置了 。是不是特别方便?
第liu步 可以看运行效果了
在浏览器输入 http://localhost:8888 效果如下图
总共用了不到 20行代码,一个 微服务的 注册中心 + 服务提供者完成
小程序查看更多java相关面试题