1. 首先搭建父工程,在父工程下面建立三个子模块,分别是Eurekaserver7001,comsumer_order_80和provice_payment_8001
(1)建立父工程


父工程的pom添加如下:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source><!-- 根据你的jdk来改 -->
<maven.compiler.target>1.8</maven.compiler.target><!-- 根据你的jdk来改 -->
<junit.version>4.12</junit.version>
<lombok.version>1.18.10</lombok.version>
<log4j.version>1.2.17</log4j.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<dependencyManagement><!--定义规范,但不导入-->
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</dependency>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud 阿里巴巴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
(2)建立Eureka服务治理中心



添加pom依赖:

<dependencies>
<!-- Eureka服务治理得到服务端(server) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 发送web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 服务治理的健康监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
新建主启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
新建application.yml文件

eureka:
instance:
hostname: localhost
#客户端配置
client:
#注册进入服务治理中心
register-with-eureka: false
#使用集群
fetch-registry: false
#服务治理的路径
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
(3)建立comsumer_order_80模块




pom文件依赖如下:
<dependencies>
<!-- 发送web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 不用写set和get -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 服务治理的健康监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 格式化json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- 说明这是服务治理的客户端(client) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
新建主启动类和application.yml文件

主启动类
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
yml文件:

eureka:
#这是客户端
client:
#是否注册进入服务治理中心
register-with-eureka: true
#是否使用集群负载均衡
fetch-registry: true
#入驻服务治理的地址,入驻哪个服务治理
service-url:
defaultZone: http://localhost:7001/eureka
(4)建立provice_payment_8001模块


pom依赖如下:
<dependencies>
<!-- 发送web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 数据库连接驱动 -->
<!--<dependency>-->
<!--<groupId>mysql</groupId>-->
<!--<artifactId>mysql-connector-java</artifactId>-->
<!--<scope>runtime</scope>-->
<!--</dependency>-->
<!-- 持久层用mybatis -->
<!--<dependency>-->
<!--<groupId>org.mybatis.spring.boot</groupId>-->
<!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--</dependency>-->
<!-- 服务治理的健康监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 连接池 -->
<!--<dependency>-->
<!--<groupId>com.alibaba</groupId>-->
<!--<artifactId>druid-spring-boot-starter</artifactId>-->
<!--</dependency>-->
<!-- 使用JDBC连接 -->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-jdbc</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 不用写set和get-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 用来格式化json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- 说明这是服务治理的客户端(client) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
新建主启动类

@SpringBootApplication
@EnableEurekaClient
public class paymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(paymentMain8001.class,args);
}
}
新建application.yml

server:
port: 8001
spring:
application:
name: cloud-payment-service
#这是eureka服务治理
eureka:
#这是客户端
client:
#是否注册进入服务治理中心
register-with-eureka: true
#是否使用集群负载均衡
fetch-registry: true
#入驻服务治理的地址,入驻哪个服务治理
service-url:
defaultZone: http://localhost:7001/eureka
(5) 最后启动微服务,启动顺序,先启动Eureka服务治理中心,然后启动其他的微服务,在浏览器输入 http://localhost:7001
当出现Instances currently registered with Eureka不为空时,环境搭建成功,如图所示:
提示:因为服务器启动,还有微服务入驻需要时间,因此,在启动完成后,等待1分钟在刷新,才能看到下面图片一样的内容










