SpringCloud之Eureka学习

Eureka学习

Eureka的作用

Eureka服务端安装

1.新建模块,这里新建的是Eureka_server1111
在这里插入图片描述

2.修改pom文件,添加以下内容

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <!--新版的加了netflix,旧版的没有-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

3.修改主启动类,在主启动类上添加注解

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

4.修改yml文件

#端口号
server:
  port: 1111

#应用名称
spring:
  application:
    name: eureka_server1111

#Eureka配置
eureka:
  instance:
    #eureka服务端实例的名称
    hostname: eureka1111
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己就是服务端注册中心,我的职责技术维护服务端实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。集群的话,Eureka需要互相注册
      defaultZone: http://localhost:1111/eureka

5.打开浏览器输入地址,可以看到如下内容,这说明Eureka服务端成功启动了。
在这里插入图片描述

Eureka服务注册

将支付服务注册到Eureka中

在原有代码的基础上做如下改动:
1.修改pom文件,在pom文件中添加Eureka客户端的坐标

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

2.在启动类中添加注解@EnableEurekaClient
在这里插入图片描述
3.修改yml文件,添加Eureka的配置

#eureka配置
eureka:
  client:
    #表示是否将自己注册进EurekaServer,默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true,才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:${server.port}/eureka

4.启动服务后可以看到如下内容
在这里插入图片描述

将订单服务注册到Eureka中

在原有代码上进行改动:
1.修改pom文件,在pom文件中添加Eureka客户端的坐标

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

2.在启动类中添加注解@EnableEurekaClient
在这里插入图片描述
3.修改yml文件,添加Eureka的配置

eureka:
  client:
    #表示是否将自己注册进EurekaServer,默认为true,
    register-with-eureka: true
    #是否从EuerkaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能ribbon实现负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:1111/eureka

4.启动服务后可以看到如下内容
在这里插入图片描述

Eureka集群

Eureka集群配置的要点在于Eureka服务端相互注册,服务要同时注册到这两个注册中心
1.新建一个模块Eureka_server1111,这两个模块基本一样,只有yml文件和主类存在差别。
在这里插入图片描述
2.修改pom文件,添加Eureka客户端

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!--新版的加了netflix,旧版的没有-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
    </dependencies>

3.修改Eureka_server1111模块和Eureka_server1110模块的yml文件。
Eureka_server1111模块的yml文件

server:
  port: 1110

spring:
  application:
    name: eureka_server1110

eureka:
  instance:
    #eureka服务端实例的名称
    hostname: eureka1110
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己就是服务端注册中心,我的职责技术维护服务端实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。集群的话,Eureka需要互相注册
      defaultZone: http://localhost:1111/eureka

Eureka_server1110模块的yml文件,和没有集群之前的配置差别在于defaultZone,集群需要相互注册,这里注册的是其它的Eureka服务端。

eureka:
  instance:
    #eureka服务端实例的名称
    hostname: eureka1110
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己就是服务端注册中心,我的职责技术维护服务端实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。集群的话,Eureka需要互相注册
      defaultZone: http://localhost:1111/eureka

4.修改支付服务的yml文件,将支付服务注册到两个Eureka服务端中

#eureka配置
eureka:
  client:
    #表示是否将自己注册进EurekaServer,默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true,才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:1111/eureka,http://localhost:1110/eureka

5.启动以后可以看到如下内容,这里没有进行域名和ip的映射,所以不好理解。
在这里插入图片描述

服务集群

1.创建一个新的支付服务模块
在这里插入图片描述
2.修改pom文件,和原来的支付服务模块的yml文件一致

    <dependencies>
        <dependency>
            <groupId>com.sks</groupId>
            <artifactId>api_commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

3.修改yml文件

server:
  port: 8002

spring:
  application:
    name: CLOUD-PAYMENT-SERVICE
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_cloud?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: root

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:1111/eureka,http://localhost:1110/eureka

#mybatis配置
mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.sks.entity

4.修改订单服务模块的URL,这里和之前的不一样,这样写是因为我们进行了服务集群,如果还是原来的写法那么通过地址只能访问到单个服务,而不能自动实现负载均衡,
在这里插入图片描述
5.启动服务,可以看到,同样的链接可以访问到两个不同的支付服务,这说明我们的服务集群成功。
在这里插入图片描述
在这里插入图片描述

参考

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值