在网上找了一篇文章,然后跟着编写实例,本着学习的态度,没有去直接下载作者的源码,而是自建工程,复制文章中的代码块。
文章: https://www.jianshu.com/p/7293b148028f
完成编码后,系统无法启动,会出现一些错误,然后针对问题百度查询解决方案。 最终功夫不负有心人,完成了我的第一个spring cloud实例,并成功部署到linux服务器 上
1. 搭建典型微服务架构
创建一个Maven父项目spring-cloud-examples
,用于管理项目依赖包版本
pom.xml配置如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<spring.boot.version>2.1.4.RELEASE</spring.boot.version>
<spring.cloud.version>Greenwich.SR1</spring.cloud.version>
<lombok.version>1.18.8</lombok.version>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.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>
2. 搭建服务配置中心
- 在
spring-cloud-examples
项目下创建一个子项目spring-cloud-example-config
,添加Spring Cloud Config Server端的相关依赖包:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<finalName>spring-cloud-example-config</finalName>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
- 添加Spring Boot配置文件
application.yml
,配置如下:
spring:
application:
name: spring-cloud-example-config
profiles:
active: native #启用本地配置文件
cloud:
config:
server:
native:
search-locations: classpath:/configs/ #配置文件扫描目录
server:
port: 8000 #服务端口
- 启动类添加注解
@EnableConfigServer
通过启用Config Server服务。
package org.spring.cloud.example.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
3. 搭建服务注册中心
- 在
spring-cloud-examples
项目下创建一个子项目spring-cloud-example-registry
,在pom.xml
中添加Eureka Server相关依赖包:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<build>
<finalName>spring-cloud-example-registry</finalName>
<defaultGoal>compile</defaultGoal>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource&