搭建eureka遇到的问题

1、情境

      我是准备搭建提供者、消费者、注册中心,但是搭建好提供者、消费者并且启动不报错的情况下,eureka启动的时候就是各种错误(要么jar包少,要么方法少,反正就是起不来),纠结了好几天的我,准备放弃的情况下,问了一下我的男朋友,他就把他当时搭建eureka的pom依赖以及配置文件发给我,我就死马当活马医试了试。结果,全部正确可以显示。

2、提供者、消费者、注册中心搭建过程

2、1 父项目

pom依赖

# parent的底层也是继承了spring-boot-dependencies。或者可以不引入parent,单独继承jar依赖

org.springframework.boot

spring-boot-starter-parent

2.2.5.RELEASE

# springcloud的依赖

org.springframework.cloud

spring-cloud-dependencies

Greenwich.SR3

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.3

mysql

mysql-connector-java

8.0.21

com.alibaba

druid

1.0.18

org.projectlombok

lombok

1.18.12

junit

junit

4.12

test

log4j

log4j

1.2.17

org.springframework.boot

spring-boot-starter-web

2.3.5.RELEASE

# 有的配置默认xml文件不会被编译,需要加入下面这个配置,xml文件会被编译

src/main/java

com/sxw/dao/*.xml

src/main/resources

true

2、2 提供者

pom依赖

#eureka客户端的jar

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

2.2.5.RELEASE

#引入pojo单独的项目

com.sxw

SpringCloudApi

1.0.0

org.mybatis.spring.boot

mybatis-spring-boot-starter

mysql

mysql-connector-java

com.alibaba

druid

org.projectlombok

lombok

junit

junit

test

#springboot启动器

org.springframework.boot

spring-boot-starter-web

配置文件

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调用提供者的服务)

com.sxw

SpringCloudApi

1.0.0

org.springframework.boot

spring-boot-starter-web

2、4 Eureka

pom依赖

#eureka的服务端jar

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

2.2.5.RELEASE

配置文件

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);

   }

}

你可能感兴趣的:(搭建eureka遇到的问题)