详解spring cloud eureka注册中心

2025-05-29 0 87

注册中心呢 就是springcloud的一个核心组件 所有微服务的基石 微服务的核心思想就是分布式 所有的服务分开管理 但这些服务分开后该如何协同呢 就需要注册中心的介入

怎么使用注册中心

首先在gradle引入它的依赖

?

1
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'

这里再讲一下 springcloud会分布很多模块 很难管理 所以在整个项目的build.gradle中可以对所有模块的build.gradle进行管理

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74
//插件

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

}

}

}

看下配置文件

?

1

2

3

4

5

6

7

8

9
server:

port: 8888 #端口号

spring:

application:

name: register-center #项目名

eureka:

client:

register-with-eureka: false #启动时不向注册中心注册自己 意思也就是证明自己是一个注册中心

fetch-registry: false #启动时是否检索服务 不检索 含义同上

还有一个

?

1
eureka.server.enable-self-preservation:

是否开启自我保护模式,默认为true 会在下一篇博客详细说明[/code]

需要在启动类上加上两个注解

?

1

2

3

4

5

6

7
@springbootapplication //启动项目

@enableeurekaserver // 定义自己是一个注册中心

public class registercenterprovider {

public static void main(string[] args) {

springapplication.run(registercenterprovider.class,args);

}

}

这时候就可以尝试将一个服务加入注册中心

先建一个新模块

一、加入依赖

?

1
compile 'org.springframework.cloud:spring-cloud-starter-eureka-server'

二、配置文件 几乎一样 唯一不同是加入如下两点

?

1

2

3

4

5

6
eureka:

client:

service-url:

defaultzone: http://localhost:8888/eureka/ #注册进那个注册中心 注册中心的地址

instance:

prefer-ip-address: true #是否用ip地址注册进注册中心

三、在启动类上加入注解

?

1
@enablediscoveryclient

之后启动注册中心的启动类 再启动服务端的启动类 看看什么效果

一定要先启动注册中心 再启动服务端 否则服务端找不到可以注册注册中心就会报错

详解spring cloud eureka注册中心

这样就是已经注册成功

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/wangkee/p/9304161.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解spring cloud eureka注册中心 https://www.kuaiidc.com/111387.html

相关文章

发表评论
暂无评论