Spring Cloud 学习笔记-01-Eureka

Spring Cloud Eureka

Eureka Server,注册中心

Eureka Client,所有要进行注册的微服务通过Eureka Client连接到Eureka Server,完成注册。

代码实现

1.创建Maven工程,pom.xml加入依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.7.RELEASE</version>
    <relativePath/>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--解决 JDK9以上没有 JAXB API的问题-->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Eureka Server 代码实现

2.在父工程下创建Module(名字eurekaserver), pom.xml加入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

3.在resources文件夹下创建配置文件application.yml,添加Eureka Server相关配置

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/

属性说明

server.port:当前Eureka Server服务端口

eureka.client.register-with-eureka:是否将当前的Eureka Server服务作为客户端进行注册

eureka.client.fetch-registry:是否获取其他Eureka Server服务的数据

eureka.client.service-url.defaultZone:注册中心的访问地址

4.创建启动类

在java文件夹创建类

package com.example;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args){
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

注解说明:

@SpringBootApplication 声明该类是Spring Boot服务的入口

@EnableEurekaServer 声明该类是一个Eureka Server微服务,提供服务注册和服务发现功能,即注册中心

5.启动注册中心,访问:localhost:8761

Eureka Client 代码实现

创建Module(名字eurekaclient), pom.xml加入依赖

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

创建配置文件application.yml,添加Eureka Client相关配置

server:
  port: 8010
spring:
  application:
    name: provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: false

属性说明:

spring.application.name:当前服务注册在Eureka Server上的名称

eureka.client.service-url.defaultZone:注册中心的访问地址

eureka.client.instance.prefer-ip-address:是否将当前服务的IP注册到Eureka Server

创建启动类

package com.example;

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

启动

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值