目前支持的Spring Boot版本号为2.0.3.RELEASE;Spring Cloud版本号为Finchley.RELEASE
一、Spring Cloud简介
Spring Cloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑,另外说明Spring Cloud是基于Spring Boot的。
二、创建服务注册中心
在这里,我们采用Eureka作为服务注册与发现的组件,至于Consul之后会出文章详细介绍。
2.1 创建一个maven工程SpringCloud_Common
Spring Boot版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE。这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom文件。
工程结构图如下:
pom.xml内容如下:
<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</groupId>
<artifactId>SpringCloud_Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>SpringCloud_Common</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
2.2 创建maven工程SpringCloud_Server作为服务注册中心,即Eureka Server
工程结构图如下:
pom.xml文件内容如下,由于继承了父pom依赖,所以只需要引入spring-cloud-starter-netflix-eureka-server依赖即可
<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>
<artifactId>SpringCloud_Server</artifactId>
<packaging>jar</packaging>
<name>SpringCloud_Server</name>
<parent>
<groupId>com</groupId>
<artifactId>SpringCloud_Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
启动一个服务注册中心,只需要一个@EnableEurekaServer注解,加在application类上即可。
package com.zit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/*启动一个服务注册中心*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存之中完成),在默认情况下eureka server也是一个eureka client,必须指定一个server, 故eureka server的配置文件appication.properties内容如下:
server.port=8761
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
spring.application.name=eureka-server
eureka.client.registerWithEureka=false和eureka.client.fetchRegistry=false表明自己是个eureka server。
2.3 eureka server是有界面的,启动工程,打开浏览器访问:http://localhost:8761 ,界面如下:
No application available 没有服务被发现 ……
因为没有注册服务当然不可能有服务被发现了。
三、创建一个服务提供者(eureka client)
当client向server注册时,它会提供一些元数据,例如主机、端口、URL、主页等。Eureka server从每个client实例接收心跳消息。如果心跳超时,则通常将该实例从注册server中删除
3.1 创建maven工程SpringCloud_Client作为服务提供者,即Eureka Client
工程结构图如下:
pom.xml文件如下:
<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>
<artifactId>SpringCloud_Client</artifactId>
<packaging>jar</packaging>
<name>SpringCloud_Client</name>
<parent>
<groupId>com</groupId>
<artifactId>SpringCloud_Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
通过注解@EnableEurekaClient表明自己是个eureka client
package com.zit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/*@EnableEurekaClient表明自己是个eureka客户端*/
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplicationOne {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplicationOne.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String hi(@RequestParam String name) {
return "hi"+name+",i am from port:"+port;
}
}
仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.properties配置文件如下:
server.port=8762
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=service-hi
需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
启动工程,打开http://localhost:8761 ,即eureka server 的网址:
你会发现一个服务已经注册在服务中心了,服务名为SERVICE-HI ,端口为8762。
这时打开 http://localhost:8762/hi?name=forezp ,你会在浏览器上看到: