1、情境
我是准备搭建提供者、消费者、注册中心,但是搭建好提供者、消费者并且启动不报错的情况下,eureka启动的时候就是各种错误(要么jar包少,要么方法少,反正就是起不来),纠结了好几天的我,准备放弃的情况下,问了一下我的男朋友,他就把他当时搭建eureka的pom依赖以及配置文件发给我,我就死马当活马医试了试。结果,全部正确可以显示。
2、提供者、消费者、注册中心搭建过程
2、1 父项目
pom依赖
# parent的底层也是继承了spring-boot-dependencies。或者可以不引入parent,单独继承jar依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency># springcloud的依赖
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
# 有的配置默认xml文件不会被编译,需要加入下面这个配置,xml文件会被编译
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>com/sxw/dao/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
2、2 提供者
pom依赖
<dependencies>
<dependency>#eureka客户端的jar
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>#引入pojo单独的项目
<groupId>com.sxw</groupId>
<artifactId>SpringCloudApi</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>#springboot启动器
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
配置文件
server: #端口
port: 8001
mybatis: #mybatis的配置 主要是mapper文件的位置 pojo的别名
type-aliases-package: com.sxw.pojo
mapper-locations: classpath:com/sxw/dao/*.xml
spring: #主要是数据源的配置
datasource:
url: jdbc:mysql://localhost:3306/springcloud01?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: sunxw518
application:
name: springcloud_provider_8001
eureka: #将提供者注册到eureka服务端的配置
client:
service-url:
defaultZone: http://localhost:6001/eureka/
instance: #这个不重要,配不配都行
instance-id: SpringCloudProvider8001
2、3 消费者(通过restTemplate调用提供者的服务)
<dependencies>
<dependency>
<groupId>com.sxw</groupId>
<artifactId>SpringCloudApi</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2、4 Eureka
pom依赖
<dependencies>#eureka的服务端jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
配置文件
server:
port: 6001
eureka:
instance:
hostname: localhost#IP地址
client:
register-with-eureka: false#是否向eureka中心注册自己
fetch-registry: false#如果是false,说明自己就是服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/#这个地址是提供者注册的地址
#eureka启动后,想要访问eureka 地址为http://localhost:6001/ 这个地址是可以查看eureka中几个注册者
2、5 spring-cloud-starter-netflix-eureka-server和spring-cloud-starter-eureka-server的区别
我第一次使用的jar包是spring-cloud-starter-eureka-server,但是一启动就是各种各样的问题,刚开始以为是自己前面提供者消费者的jar包太多有冲突,直至今天才知道,这个包已经被废弃了。springcloud的新版本需要使用spring-cloud-starter-netflix-eureka-server。springcloud旧版本1.5X之前是使用spring-cloud-starter-eureka-server。
2、6 启动类
注册中心启动类
@SpringBootApplication
@EnableEurekaServer
publicclassApplication{
publicstaticvoidmain(String[]args) {
SpringApplication.run(Application.class,args);
}
}
提供者启动类
@SpringBootApplication
@EnableEurekaClient
publicclassSpringApplicationProvider{
publicstaticvoidmain(String[]args) {
SpringApplication.run(SpringApplicationProvider.class,args);
}
}