03-搭建Eureka注册中心和服务端

Eureka注册中心的搭建

首先以7001为例
在这里插入图片描述
首先导入依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

然后再配置文件(application.yml)中编写Eureka的依赖

server:
  port: 7001
eureka:
  instance:
    hostname: Eureka7001.com #eureka服务的注册名称
  client:
    register-with-eureka: false #表示是否想eureka注册中心注册自己
    fetch-registry: false #为false表示自己是注册中心
    service-url: #监控页面
      defaultZone: http://Eureka7002.com:7002/eureka/,http://Eureka7003.com:7003/eureka/#链接其余的两个注册中心形成集群。

spring:
  application:
    name: springcloud-eureka-7001

最后再启动类上增加@EnableEurekaServer 注解,该注解可以将项目作为SpringCloud中的注册中心。

package com.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @description:
 * @author: 陈声铭
 * @time: 2021/3/27 16:15
 */
@SpringBootApplication
@EnableEurekaServer //Eureka服务端启动类注解
public class EurekaServer_7001 {
    public static void main(String[] args){
        try{
            SpringApplication.run(EurekaServer_7001.class, args);
        } catch(Exception e) {
            System.out.println("SpringApplication----->" + e.getMessage());
        }
    }
}

7002和7003与之类似,但是要注意service-url.defaultZone的配置是有相同的
7002的配置为http://Eureka7001.com:7001/eureka/,http://Eureka7003.com:7003/eureka/
在这里插入图片描述
7003的配置为http://Eureka7001.com:7001/eureka/,http://Eureka7002.com:7002/eureka/
在这里插入图片描述

Eureka服务端

首先生产者的配置(application.yml)

server:
  port: 8002

eureka:
  instance:
    instance-id: provider_8002
    prefer-ip-address: true
  client:
    register-with-eureka: true #表示是否想eureka注册中心注册自己
#    fetch-registry: false #为false表示自己是注册中心
    service-url: #监控页面
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/

info:
  app.name: 生产者-8002
  company.name: provider_8002

spring:

  application:
    name: provider
  datasource:
    url: jdbc:mysql://localhost:3306/springcloudlearning?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
    #数据原
    type: com.alibaba.druid.pool.DruidDataSource
    #数据库驱动
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456



mybatis:
  config-location: classpath:mybatis\mybatis-config.xml
  mapper-locations: classpath:mybatis\mapper\*.xml
  type-aliases-package: com.eo

再启动类上添加@EnableEurekaClient注解

package com.springcloud.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @description:
 * @author: 陈声铭
 * @time: 2021/2/24 1:18
 */
@SpringBootApplication
//开启Eureka客户端
@EnableEurekaClient
public class spring_cloud_provider_8002 {
    public static void main(String[] args){
        try{
            SpringApplication.run(spring_cloud_provider_8002.class, args);
        } catch(Exception e) {
            System.out.println("SpringApplication----->" + e.getMessage());
        }
    }
}

消费者的配置(application.yml)

server:
  port: 80
eureka:
  client:
    service-url: #监控页面
      defaultZone: http://Eureka7001.com:7001/eureka/,http://Eureka7002.com:7002/eureka/,http://Eureka7003.com:7003/eureka/

消费者的启动类

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @description:
 * @author: 陈声铭
 * @time: 2021/3/27 15:02
 */
@SpringBootApplication
//开启Eureka客户端
@EnableEurekaClient

public class DepatSpringBootApplication {
    public static void main(String[] args){
        try{
            SpringApplication.run(DepatSpringBootApplication.class, args);
        } catch(Exception e) {
            System.out.println("DepatSpringBootApplication----->" + e.getMessage());
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值