eureka服务注册中心的使用

因为springcloud中的服务基于springboot搭建,所以是由eureka之前要在要引入父依赖

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <properties>
        <spring.cloud-version>Hoxton.SR6</spring.cloud-version>
    </properties>

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

1.eureka分为 eureka server 和 eureka client ,首先要搭建eureka server,首先创建一个springboot项目作为eureka server。这个项目仅仅是用来当作服务的注册中心使用。

1.1在springboot中引入web和eureka server的依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
1.2 在配置文件中配置服务注册中心的地址和端口(默认8761)

server.port=8761
#服务名称很重要,在注册时会以服务名当作标识使用,建议全大写,因为在注册完后,UI页面显示的是全大写 
spring.application.name=EUREKASERVER
#如果是搭建eureka server 集群,可以用逗号分割,填入集群的地址
eureka.client.service-url.defalutZone=http://localhost:8761/eureka
1.3 在启动类上加入 @EnableEurekaServer注解开启服务
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

这样注册中心就注册好了,可以访问http://localhost:8761 访问eureka的UI界面

注意! 在启动时可能会报错,因为eureka server会把自己先注册到服务中心,因为server尚未启动,所以会注册失败报错,但是不影响使用。

**解决方法:**关闭eureka server 自己注册自己

#关闭立即注册
eureka.client.fetch-registry=false
#让当前的应用仅仅为注册中心 
eureka.client.register-with-eureka=false

2.eureka client的使用
2.1
eureka client 相当于微服务中的每一个服务,在向服务中心注册时,应该先引入eureka client的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

注意这次时client的依赖。

2.2 依旧时三步配置properties
server.port=8989
spring.application.name=EUREKACLIENT
#如果eureka server 是个集群,应该把所有集群的地址都配置上,用逗号分割
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
2.3在启动类上配置注解@EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class,args);
    }
}

启动服务,打开http://localhost:8761 (服务注册中心的UI地址),就可以看见注册成功了!

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值