详解spring cloud eureka注册中心
作者:洛阳融科王珂 时间:2023-11-10 17:54:10
注册中心呢 就是springcloud的一个核心组件 所有微服务的基石 微服务的核心思想就是分布式 所有的服务分开管理 但这些服务分开后该如何协同呢 就需要注册中心的介入
怎么使用注册中心
首先在gradle引入它的依赖
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'
这里再讲一下 springcloud会分布很多模块 很难管理 所以在整个项目的build.gradle中可以对所有模块的build.gradle进行管理
//插件
apply plugin: 'java'
apply plugin: 'spring-boot'
//jdk版本
sourceCompatibility = 1.8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12' //项目版本
}
//在编译构建时的配置 自动维护版本号
buildscript {
ext{
springBootVersion='1.5.10.RELEASE' //ext中可以定义变量 里面写的是springboot插件的版本
}
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
jcenter()
mavenCentral()
maven{ url "http://repo.spring.io/snapshot" }
maven{ url "http://repo.spring.io/milestone" }
maven{ url "http://repo.spring.io/release" }
maven{ url 'http://repo.spring.io/plugins-snapshot' }
}
dependencies{
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
//统一所有项目的配置 就是对所有的模块进行统一配置 所有以后的模块都不用再配置
allprojects {
group 'com.indi.wk' //分组
version '1.0-SNAPSHOT' //版本号
ext{
springCloudVersion='Edgware.SR2'
}
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
jcenter()
mavenCentral()
maven{ url "http://repo.spring.io/snapshot" }
maven{ url "http://repo.spring.io/milestone" }
maven{ url "http://repo.spring.io/release" }
maven{ url 'http://repo.spring.io/plugins-snapshot' }
}
}
//统一所有子项目的配置
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
dependencies {
compile('org.springframework.boot:spring-boot-starter-web'){
//移除tomcat 因为springboot嫌tomcat运行慢 就使用undertow来代替
exclude module:"spring-boot-starter-tomcat"
}
//替代tomcat
compile 'org.springframework.boot:spring-boot-starter-undertow'
//健康检查
compile 'org.springframework.boot:spring-boot-starter-actuator'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}
//版本控制插件
dependencyManagement{
imports{
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
}
看下配置文件
server:
port: 8888 #端口号
spring:
application:
name: register-center #项目名
eureka:
client:
register-with-eureka: false #启动时不向注册中心注册自己 意思也就是证明自己是一个注册中心
fetch-registry: false #启动时是否检索服务 不检索 含义同上
还有一个
eureka.server.enable-self-preservation:
是否开启自我保护模式,默认为true 会在下一篇博客详细说明[/code]
需要在启动类上加上两个注解
@SpringBootApplication //启动项目
@EnableEurekaServer // 定义自己是一个注册中心
public class RegisterCenterProvider {
public static void main(String[] args) {
SpringApplication.run(RegisterCenterProvider.class,args);
}
}
这时候就可以尝试将一个服务加入注册中心
先建一个新模块
一、加入依赖
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'
二、配置文件 几乎一样 唯一不同是加入如下两点
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/ #注册进那个注册中心 注册中心的地址
instance:
prefer-ip-address: true #是否用ip地址注册进注册中心
三、在启动类上加入注解
@EnableDiscoveryClient
之后启动注册中心的启动类 再启动服务端的启动类 看看什么效果
一定要先启动注册中心 再启动服务端 否则服务端找不到可以注册的注册中心就会报错
这样就是已经注册成功
来源:http://www.cnblogs.com/wangkee/p/9304161.html
标签:spring,cloud,eureka,注册
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 自定义组件成JAR包的实现方法
2023-04-08 17:56:39
![](https://img.aspxhome.com/file/2023/0/102610_0s.png)
Java 动态代理的多种实现方式
2023-11-20 05:23:14
![](https://img.aspxhome.com/file/2023/6/91126_0s.png)
Java8 中的ParallelStream
2022-09-07 19:00:23
![](https://img.aspxhome.com/file/2023/4/87224_0s.png)
Android UI设计系列之自定义ListView仿QQ空间阻尼下拉刷新和渐变菜单栏效果(8)
2023-05-23 14:57:04
![](https://img.aspxhome.com/file/2023/8/138118_0s.gif)
Android SQLite数据库版本升级的管理实现
2023-11-09 23:58:44
使用C#编写15子游戏
2023-03-23 08:28:52
![](https://img.aspxhome.com/file/2023/3/129633_0s.png)
Spring Security 强制退出指定用户的方法
2022-10-04 18:13:04
Java聊天室之实现客户端一对一聊天功能
2022-12-28 09:23:30
![](https://img.aspxhome.com/file/2023/0/79340_0s.png)
dubbo服务链路跟踪方式
2023-08-24 09:54:21
![](https://img.aspxhome.com/file/2023/6/58296_0s.png)
Mybatis实现SQL存储流程详解
2022-09-11 05:17:05
C#调用海康工业相机SDK采集图像并在Halcon窗口中显示方式
2022-06-26 00:48:02
![](https://img.aspxhome.com/file/2023/3/113813_0s.png)
Unity实现VR中在黑板上写字效果
2021-08-04 20:11:11
![](https://img.aspxhome.com/file/2023/0/126070_0s.gif)
不知道面试会不会问Lambda怎么用(推荐)
2021-07-27 21:37:30
Java Swing JLabel标签的使用方法
2021-09-16 05:48:13
![](https://img.aspxhome.com/file/2023/3/68573_0s.jpg)
仿ios状态栏颜色和标题栏颜色一致的实例代码
2023-08-20 02:17:45
C++之异常处理详解
2023-04-10 10:22:00
![](https://img.aspxhome.com/file/2023/3/114923_0s.jpg)
基于Java8 Stream API实现数据抽取收集
2021-10-01 03:13:03
用C#编写ActiveX控件(三)
2023-03-26 12:35:10
![](https://img.aspxhome.com/file/2023/2/96332_0s.jpg)
Android开发自学笔记(二):工程文件剖析
2021-09-08 11:12:56
C#实现炫酷启动图-动态进度条效果
2022-01-04 22:22:10
![](https://img.aspxhome.com/file/2023/0/129690_0s.gif)