博客信息

Eureka的初步使用

发布时间:『 2019-04-28 03:44』  博客类别:SpringCloud  阅读(693)

搭建Eureka服务注册中心

前面说过eureka是c/s模式的  server服务端就是服务注册中心,其他的都是client客户端,服务端用来管理所有服务,客户端通过注册中心,来调用具体的服务;

我们先来搭建下服务端,也就是服务注册中心;


新建 module   microservice-eureka-server-2001;

在pom文件中加入eureka-server依赖;

在yml文件中添加Eureka服务端相关信息;

在启动类中添加@EnableEurekaServer注解;

pom.xml 

application.yml 

MicroserviceEurekaServer2001Application.java 


搭建成功截图如下

小李飞刀_SpringCloud


Eureka中注册服务提供者

这里我们在原来的服务提供者项目 microservice-student-provider-1001  上面直接修改:

首先pom.xml修改,加上eureka客户端依赖:

 <!--添加注册中心Eureka相关配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>


然后application.yml上加上配置:

eureka:
  instance:
    hostname: localhost  #eureka客户端主机实例名称
    appname: microservice-student  #客户端服务名
    instance-id: microservice-student:1001 #客户端实例名称
    prefer-ip-address: true #显示IP
  client:
    service-url:
      defaultZone: http://localhost:2001/eureka   #把服务注册到eureka注册中心

这里的defaultZone要和前面服务注册中心的暴露地址一致;


最后 启动类加上一个注解 @EnableEurekaClient

@EntityScan("com.javaxl.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProvider1001Application {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceStudentProvider1001Application.class, args);
    }

}


然后我们测试下,先启动服务注册中心,再启动这个服务提供者;

 

然后运行:http://localhost:2001/

服务提供者成功注册截图如下

小李飞刀_SpringCloud


点击实例状态结果如下

小李飞刀_SpringCloud




解决方案如下

1首先在服务提供者项目pom.xml里加入actuator监控依赖:

2、然后服务提供者项目application.yml加上info配置:


<!-- actuator监控引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


info:
  groupId: com.javaxl.testSpringcloud
  artifactId: microservice-student-provider-1001
  version: 1.0-SNAPSHOT
  userName: http://47.100.191.44
  phone: 123456



结果图如下

小李飞刀_springcloud


关键字:     SpringCloud  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有