创建Eureka项目——初学者

1.创建Springboot项目,父类EurekaDemo

创建方式 maven

父类依赖pom如下(注意里面的包modules为成品并不是一开始版本)

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">

4.0.0

com.web

eureka_demo

pom

1.0-SNAPSHOT

eureka_server

eureka_server02

service_provider

eureka_consumer

config_server

config_server02

config_client1

spring-boot-starter-parent

org.springframework.boot

2.2.4.RELEASE

Hoxton.SR1

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

org.apache.maven.plugins

maven-surefire-plugin

2.17

true

*这是父类springcloud的基础搭建

2.创建服务端子类(Eureka的服务端以及客户端)

Eureka注册中心是需要服务端的这边我们直接以集群的形式来写demo

我们这边创建两个 Eureka_server Eureka_server2

创建方式如上创建父类的MAVEN格式一样,这边不过多赘述

这边server需要进行修改操作部署3个地方,启动类,依赖yml(properties),pom.xml文件,下面依次进行修改

1.pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.web

eureka_server

1.0-SNAPSHOT

jar

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-surefire-plugin

2.17

true

src/main/java

**/*.yml

**/*.properties

**/*.xml

false

src/main/resources

**/*.yml

**/*.properties

**/*.xml

false

com.web

eureka_demo

1.0-SNAPSHOT

org.apache.skywalking

apm-toolkit-logback-1.x

8.1.0

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

以上依赖中对于本文章的核心依赖为:

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

2.配置文件:

server:

port: 8761

spring:

application:

name: eureka-server # 应用名称

# 配置 Eureka Server 注册中心

eureka:

instance:

hostname: eureka01 # 主机名,不配置的时候将根据操作系统的主机名来获取

prefer-ip-address: true # 是否使用 ip 地址注册

instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port

client:

# 设置服务注册中心地址,指向另一个注册中心

service-url: # 注册中心对外暴露的注册地址

defaultZone: http://localhost:8762/eureka/

这里注意集群server互相指向

3.启动类

package com.web;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer

@SpringBootApplication

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class,args);

}

}

主要是@EnableEurekaServer这个注解

Eureka_server02的设置配置跟以上大同小异唯一区别在于互相指向的IP:端口

server:

port: 8762 # 端口

spring:

application:

name: eureka-server # 应用名称

# 配置 Eureka Server 注册中心

eureka:

instance:

hostname: eureka02 # 主机名,不配置的时候将根据操作系统的主机名来获取

prefer-ip-address: true # 是否使用 ip 地址注册

instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port

client:

# 设置服务注册中心地址,指向另一个注册中心

service-url: # 注册中心对外暴露的注册地址

defaultZone: http://localhost:8761/eureka/

3.创建客户端子类(业务逻辑端)

创建方式如上创建父类的MAVEN格式一样,这边不过多赘述

这边server需要进行修改操作部署3个地方,启动类,依赖yml(properties),pom.xml文件,下面依次进行修改

1.pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.web

service_provider

1.0-SNAPSHOT

jar

com.web

eureka_demo

1.0-SNAPSHOT

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-surefire-plugin

2.17

true

src/main/java

**/*.yml

**/*.properties

**/*.xml

false

src/main/resources

**/*.yml

**/*.properties

**/*.xml

false

org.springframework.boot

spring-boot-starter-data-elasticsearch

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-config

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

provided

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.3

mysql

mysql-connector-java

8.0.11

runtime

核心依赖为

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

2.配置文件

server:

port: 9999 # 端口

spring:

application:

name: service-provider # 应用名称(集群下相同)

datasource:

username: root

password: root

#url中database为对应的数据库名称

url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC

driver-class-name: com.mysql.jdbc.Driver

# 配置 Eureka Server 注册中心

eureka:

instance:

prefer-ip-address: true # 是否使用 ip 地址注册

instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port

client:

service-url: # 设置服务注册中心地址

defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

#自定义

name: order-service-default

mybatis:

mapper-locations: classpath:mapping/*.xml

type-aliases-package: com.web.entity

#showSql

logging:

level:

com.example.demo.mapper: debug

以上依赖主要是# 配置 Eureka Server 注册中心 这里面的为核心其他为其他依赖 可以根据需要进行删减

3.启动类

启动类springboot会自动进行加不用我们继续写可以省略

4.总结

以上就是整个Eureka的整合过程 整合中可能会出现各种各样的错误,如有问题解决不了还请搜csdn进行查看其他博主的进行解决

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: