SpringCloud 注册中心的高可用(tomcat下部署项目)

  1. 自己注册自己
  2. 将自己注册给别人
  3. 将别人注册进来
    * 相关代码及配置如下:
POM文件
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!--<version>Brixton.SR5</version>-->
                <version>Dalston.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!--引入spring boot的web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排除内置的tomcat,排除后,无法再通过main方法启动,开发时可以暂时不排除,部署项目时记得排除-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--引入 servlet api-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>ROOT</finalName>
    </build>
主函数类
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
// 这里的继承主要是为了在tomcat上部署项目
public class ServerApplication_First extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ServerApplication_First.class);
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(ServerApplication_First.class).web(true).run(args);
    }
}
配置文件(我的案例是两个注册中心的使用,配置文件有三个)
主配置文件*application.properties
# 制定你要使用的配置文件,选择不同的配置文件生成不同的jar包
spring.profiles.active=server-first
一号注册中心的配置文件
# spring cloud server 一号注册中心,实现注册中心的高可用
spring.application.name=springcloudserver
server.port=9090

eureka.instance.hostname=springcloudserver-first
eureka.client.serviceUrl.defaultZone=http://springcloudserver-two:9091/eureka/
## 不向注册中心注册自己
#eureka.client.register-with-eureka=false
## 设置不去检索服务
#eureka.client.fetch-registry=false
二号注册中心的配置文件
# spring cloud 二号注册中心相关配置,实现注册中心的高可用
spring.application.name=springcloudserver
server.port=9091

eureka.instance.hostname=springcloudserver-two
eureka.client.serviceUrl.defaultZone=http://springcloudserver-first:9090/eureka/
## 不向注册中心注册自己
#eureka.client.register-with-eureka=false
## 设置不去检索服务
#eureka.client.fetch-registry=false

注意:修改host文件,添加如下内容

127.0.0.1 springcloudserver-first springcloudserver-two
码云上的项目地址
// 项目名spring-cloud-server
https://git.oschina.net/fushengruomengzhang_admin/Study_Work_Space/tree/master/Spring_Cloud_Server
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟阿达

成长总是需要时间和经历的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值