SpringCloud是基于SpringBoot的一整套实现微服务的框架。它提供了微服务开发所需的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等组件。最重要的是, 跟spring boot框架一起使用的话,会让你开发微服务架构的云服务非常好的方便。
一、开发工具说明
-
开发工具:Spring Tool Suite 3.9.4.RELEASE
-
JDK版本:1.8.0_162
-
MAVEN版本:3.5.3
二、创建Eureka注册中心
1、New Spring Starter Project
选择 Eureka Server ,点击finish项目创建完成
此时pom.xml内容为:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.carry.springcloud</groupId> 7 <artifactId>eureka-server</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>eureka-server</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.4.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.SR1</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <dependencyManagement> 42 <dependencies> 43 <dependency> 44 <groupId>org.springframework.cloud</groupId> 45 <artifactId>spring-cloud-dependencies</artifactId> 46 <version>${spring-cloud.version}</version> 47 <type>pom</type> 48 <scope>import</scope> 49 </dependency> 50 </dependencies> 51 </dependencyManagement> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 63 </project>
2、属性文件配置
将resources目录下的application.properties重命名为application.yml(建议使用格式化yaml语言,好处的话用过就知道),添加如下内容
server: port: 8761 #eureka默认端口号 eureka: instance: hostname: localhost prefer-ip-address: true client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
解释:
- eureka.client.registerWithEureka:表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设置为false。
- eureka.client.fetchRegistry:表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false。
- eureka.client.serviceUrl.defalseZone:设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址,多个地址可用逗号(英文的)分割。
3、启动类配置
添加@EnableEurekaServer注解即可
1 package com.carry.springcloud; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @EnableEurekaServer 8 @SpringBootApplication 9 public class EurekaServerApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(EurekaServerApplication.class, args); 13 } 14 }
使用SpringBootApp启动,访问地址:localhost:8761
正常打开以上网页,说明Eureka注册中心配置成功。
三、创建客户端项目
1、创建项目步骤同上,这里我们选择Eureka Discovery、Web
pom.xml内容:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.carry.springcloud</groupId> 7 <artifactId>service-eureka-client</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>service-eureka-client</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.4.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.SR1</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-test</artifactId> 41 <scope>test</scope> 42 </dependency> 43 </dependencies> 44 45 <dependencyManagement> 46 <dependencies> 47 <dependency> 48 <groupId>org.springframework.cloud</groupId> 49 <artifactId>spring-cloud-dependencies</artifactId> 50 <version>${spring-cloud.version}</version> 51 <type>pom</type> 52 <scope>import</scope> 53 </dependency> 54 </dependencies> 55 </dependencyManagement> 56 57 <build> 58 <plugins> 59 <plugin> 60 <groupId>org.springframework.boot</groupId> 61 <artifactId>spring-boot-maven-plugin</artifactId> 62 </plugin> 63 </plugins> 64 </build> 65 66 67 </project>
2、属性文件配置
application.yml内容如下:
server: port: 8080 spring: application: name: SERVICE-EUREKA-CLIENT eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3、启动类配置
添加@EnableEurekaClient注解即可
1 package com.carry.springcloud; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 7 @EnableEurekaClient 8 @SpringBootApplication 9 public class ServiceEurekaClientApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(ServiceEurekaClientApplication.class, args); 13 } 14 }
启动项目,如下图,客户端注册成功