天天看点

详解SpringCloud中的Eureka

SpringCloud是Spring开发团队开发的为了解决分布式系统的一个全面的方案,也可以说SpringCloud是分布式系统的一个完整的解决方案。SpringCloud并不是一个具体的项目是多个项目的融合其中就包含Eureka。

为什么需要服务发现组件(Eureka):

没有发现服务的组件的编码是通过Rest的实现方式调用对应服务端口的方法,我们需要将服务提供者的网络地址和端口号写在代码中或者写在配置文件中。这种方式会有一些问题:

1、当ip地址发生变化时将会影响的服务消费者。

2、在生产环境中可能每个为服务会部署多个实例来实现容灾和负载均衡,需要系统由自动伸缩能力和扩展能力,通过这种方式实现不了。

为了解决这种问题服务的消费者需要一个服务发现机制,而Eureka就是其中的一种。

详解SpringCloud中的Eureka

在开发使用Ereuka时非常简单。在SpringBoot的基础上载maven中引入对应的依赖就可以了。下面创建3个模块分别是服务的发现Eureka,服务的消费者Consumer,服务的提供者Provider。

详解SpringCloud中的Eureka

项目的模块其中一个消费者,一个提供者,和3个服务注册中心(集群),为什么使用3个注册中这样可以保证项目的稳定,当一个注册服务挂掉,不影响消费者访问服务,我这项目不是生产项目,没有提供多个服务提供者和消费者。

首先创建 microservicecloud-eureka-7101 项目,对应的配置文件为

1

2

3

4

5

6

7

8

9

10

11

server:

port: 

7101

eureka:

instance:

hostname: eureka7101.com #eureka服务端的实例名称

client:

register-with-eureka: 

false

false

表示不向注册中心注册自己

fetch-registry: 

false

#

false

表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务

service-url:

# http:

//${eureka.instance.hostname}:${server.port}/eureka/  # 设置与Eureka Server 交互的地址查询服务和注册服务

defaultZone: http:

//eureka7102.com:7102/eureka/,http://eureka7103.com:7103/eureka/

其他的两个的服务发现文件基本上是一样的。

配置文件写好了创建一个

EurekaService7101Application类就可以了

1

2

3

4

5

6

7

8

@SpringBootApplication

@EnableEurekaServer

public

class

EurekaService7101Application {

public

static

void

main(String[] args){

SpringApplication.run(EurekaService7101Application.

class

);

}

}

启动项目使用浏览器登陆

eureka7101.com:7101就可以看到eureka的管理界面了

这里的eureka7101.com是在哪里设置的,我电脑是Windows系统c:\Windows\System32\drivers\etc\hosts设置的

详解SpringCloud中的Eureka

创建服务提供者 microservicecloud-provider-user-8001

对应的配置文件

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

server:

port: 

8001

mybatis:

config-location: classpath:mybatis/mybatis.cfg.xml # mybaits 配置文件所在路径

type-aliases-

package

: com.itgood.cloud.entity  # 所在Entity别名类所在包

mapper-locations:

- classpath:mybatis/mapper

*.xml

spring:

application:

name: microservicecloud-user

datasource:

type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型

driver-

class

-name: com.mysql.jdbc.Driver              # mysql驱动包

url: jdbc:mysql:

//10.6.204.35:3306/microservice?useSSL=false      # 数据库名称

username: root

password: root

dbcp2:

min-idle: 

5

# 数据库连接池的最小维持连接数

initial-size: 

5

# 初始化连接数

max-idle: 

5

# 最大连接数

max-wait-millis: 

200

eureka:

client:

service-url:

defaultZone: http:

//eureka7101.com:7101/eureka/,http://eureka7102.com:7102/eureka/,http://eureka7103.com:7103/eureka/

instance:

instance-id: microservicecloud-user8001

prefer-ip-address: 

true

# 访问时可以显示ip地址

这里我是用的是Mysql数据库,用的的mybatis来操作数据库。这是我的主运行类。

1

2

3

4

5

6

7

8

9

@SpringBootApplication

@EnableEurekaClient

//本地服务启动后自动注册到eureka服务中

public

class

UserProvider8001Application {

public

static

void

main(String[] args){

SpringApplication.run(UserProvider8001Application.

class

);

}

}

其他的类这里就不详细写了,代码我上传到github上了博客后面会给地址。

创建 microservicecloud-consumer-user-80 项目对应的配置文件:

1

2

3

4

5

6

7

8

server:

port: 

80

eureka:

client:           #客户端注册进eureka服务列表内

register-with-eureka: 

false

service-url:

defaultZone: http:

//eureka7102.com:7102/eureka/,http://eureka7101.com:7101/eureka/,http://eureka7103.com:7103/eureka/

对应的启动类

1

2

3

4

5

6

7

8

@SpringBootApplication

@EnableEurekaClient

public

class

UserConsumer80Application {

public

static

void

main(String[] args){

SpringApplication.run(UserConsumer80Application.

class

);

}

}

这个模块是消费者,通过RestTemplate的API调用对应的服务接口。具体的查看代码。

github:地址:https://github.com/bkinggod/microservice-springcloud

继续阅读