创建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">
*这是父类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">
以上依赖中对于本文章的核心依赖为:
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">
核心依赖为
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进行查看其他博主的进行解决
相关阅读
发表评论