上一篇【Spring-Cloud学习之路-版本说明】中提到了spring-cloud的版本依赖。
这里开始创建一个简单的Demo,我使用的IDE是IntelliJ IDEA 2016.2.5
首先:File –> New –> Project
此处因为我电脑上安装的JDK是7.0的 所以我选择的java版本是1.7
spring-boot版本我选择的1.5.6 红框内值得我们关注,它不仅包含了Spring Boot Starter POMs中的各个依赖,还包含了Spring Cloud的各种依赖。
点击Next,进入最后关于工程物理存储的一些细节。最后,点击Finish就能完成工程的构建了。
创建完成后的项目结构如图:
图中.mvn文件夹/mvnw/mvnw.cmd 是我们不需要的,我直接是删除掉的。
然后创建Eureka-Service模块,方法同上,就不再累述;
创建完成后的整体项目结构:
引入想依赖的jar,完成后的pom.xml:
<?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>
<groupId>com.jedi</groupId>
<artifactId>eureka-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-service</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>
<dependencies>
<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>
<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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在主应用类中添加 @EnableEurekaServer 注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
在application.properties中添加:
spring.application.name=eureka-server
# eureka-service 端口
server.port=8010
eureka.instance.hostname=localhost
#是否将本应用作为服务提供者注册到eureka
eureka.client.register-with-eureka=false
#是否从eureka获取注册信息
eureka.client.fetch-registry=false
# eureka服务器的地址(注意:地址最后面的 /eureka/ 这个是固定值)
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
logging.file=${spring.application.name}.log
至此一个简单的Eureka-service就完成了,然后我们访问 http://localhost:8010/ 可以看到如下界面:
创建Service Provider(服务提供者)
同上创建两个个Module,分别取名:service-provider-1,service-provider-2
首先需要做的就是在pom中引入Spring Cloud Eureka的依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
启用Service Provider非常简单,只需要加上@EnableDiscoveryClient即可。
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProvider1Application {
public static void main(String[] args) {
SpringApplication.run(ServiceProvider1Application.class, args);
}
}
application.properties:(eureka-provider-2的配置同)
spring.application.name=eureka-provider-1
# 端口
server.port=8020
# eureka服务器的地址(注意:地址最后面的 /eureka/ 这个是固定值)
eureka.client.serviceUrl.defaultZone=http://localhost:8010/eureka/
logging.file=${spring.application.name}.log