spingCloud项目之-eureka注册中心搭建及服务注册
核心:除了注册中心 其他所有的服务既是服务端也是客户端

一、注册中心端
1.搭建Eureka-注册中心





2.主要pom.xml依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3.application.yml文件
server:
port: 9091
eureka:
instance:
## 定义 Eureka 实例所在的主机名称
hostname: demo-hxx-service-eureka
## 访问的路径变为 IP 地址
prefer-ip-address: true
##ip-address: 192.168.192.179
server:
## 设置为false表示关闭保护模式 (即已经kill掉的服务,eureka自动清理)
enable-self-preservation: false
client:
## 当前微服务不注册到eureka之中 (禁止自身注册自身)
register-with-eureka: false
## 不通过eureka获取注册信息
fetch-registry: false
## 指定服务注册中心地址
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4.启动类注解
@SpringBootApplication
@EnableEurekaServer //表明这个服务就是注册中心
public class DemoHxxSeviceEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(DemoHxxSeviceEurekaApplication.class, args);
}
}
5.启动即可

二、客户端-服务端
将传统服务项目注册到注册中心
1.pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- cloud相关-注册中心连接端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.application.yml文件
## 注册中心
eureka:
instance:
hostname: demo-hxx-service-eureka
prefer-ip-address: true
#ip-address: xxx # 当前机器的地址 -真实外围域名
lease-renewal-interval-in-seconds: 5 # 默认的监控检查调整
lease-expiration-duration-in-seconds: 12
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:9091/eureka/
3.启动类
@SpringBootApplication
@EnableEurekaClient// 链接注册中心
@MapperScan("com.hxx.demo_admin.dao")
public class DemoAdminApplication {
public static void main(String[] args) {
SpringApplication.run(DemoAdminApplication.class, args);
}
}
4.遇到到坑
此时,项目启动一直报
Error creating bean with name 'bootstrapImportSelectorConfiguration'
但是呢,项目并没有设计bootstrap资源,因此便随原因,发现是spring-boot-starter-parent版本号不对导致。修改为
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
5.启动

生产环境不建议关闭自我保护机制:https://blog.csdn.net/cookily_liangzai/article/details/109996052

浙公网安备 33010602011771号