springCloud是涵盖服务注册、发现、负载均衡、断路、路由等能力并快速构建分布式系统的微服务框架。
基于Finchley.SR1版本,官方文档:http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html
maven多模块

目录
服务注册发现
- 注册中心: 提供服务注册、暴露相关服务信息
- 服务提供方: 注册到服务注册中心提供服务

项目创建:
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>org.example.cloud</groupId>
<artifactId>spring-cloud-example</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 继承SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<!-- 子模块 -->
<modules>
<module>eureka-server</module>
<module>eureka-client</module>
</modules>
<!-- 统一管理依赖 -->
<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.SR1</spring-cloud.version>
</properties>
<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>
注册中心:
- 创建子项目eureka-server
- pom.xml配置
- 注册中心启动类EurekaRegisterApplication.java
- 配置文件application.yml
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">
<parent>
<artifactId>spring-cloud-example</artifactId>
<groupId>org.example.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类:EurekaRegisterApplication.java
注意:
服务端使用@EnableEurekaServer
客户端使用@EnableEurekaClient
package org.example.register;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author Cheng.Wei
* @date 2018-09-15 02:21
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaRegisterApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaRegisterApplication.class).web(WebApplicationType.SERVLET).run(args);
}
}
配置文件:application.yml
server:
port: 7777 #启动端口
eureka:
#实例配置
instance:
hostname: localhost
appname: 注册中心
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
服务提供方
- 创建子项目eureka-client
- pom.xml配置
- 创建启动类:EurekaProviderApplication
- 配置文件application.yml
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">
<parent>
<artifactId>spring-cloud-example</artifactId>
<groupId>org.example.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-client</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
启动类:EurekaProviderApplication
package org.example.provider;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author Cheng.Wei
* @date 2018-09-15 02:42
*/
@EnableEurekaClient
@SpringBootApplication
public class EurekaProviderApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaProviderApplication.class).web(WebApplicationType.SERVLET).run(args);
}
}
- 配置文件:application.yml
spring:
application:
name: provider
server:
port: 7778
eureka:
instance:
appname: 服务A
#不使用主机名来定义注册中心的地址,而使用IP地址的形式
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name}
client:
serviceUrl:
defaultZone: http://localhost:7777/eureka/
启动顺序: 注册中心(EurekaRegisterApplication)==>>服务提供端(EurekaProviderApplication)


本文详细介绍如何使用SpringCloud Finchley.SR1版本构建微服务架构,包括服务注册、发现、负载均衡等核心功能实现。通过创建Maven多模块项目,搭建Eureka注册中心和服务提供者,展示微服务的启动流程和配置细节。

848

被折叠的 条评论
为什么被折叠?



