SpringCloud(eureka组件直连式使用)

SpringCloud 和 SpringBoot 版本选择
在这里插入图片描述
我选择的版本选择

  • SpringBoot 2.3.5.RELEASE
  • SpringCloud Hoxton.SR6

1 - 开发父项目(maven聚合 版本管理)

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2 - 开发eureka server (eureka注册中心)

  • pom.xml
    <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>
  • 入口类
@SpringBootApplication
@EnableEurekaServer   
public class EurekaServerApplication8761 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication8761.class, args);
    }
}
	@EnableEurekaServer   //开启eureka服务(注册中心)
  • 配置编写 application.yml
server:
  port: 8761   #默认端口号
spring:
  application:
    name: EUREKASERVER  # eureka服务注册中心的服务名称(微服务的唯一标识)
eureka:
  client:
    fetch-registry: false  # 关闭立即向自己注册自己
    register-with-eureka: false  # false 表示我是eureka服务注册中心
    service-url:
      defaultZone: http://localhost:8761/eureka  # 向外暴露服务!端口号必须和自己的一致
eureka:
  client:
    fetch-registry: false  # 关闭立即向自己注册自己
    这个一定必须是false  不然会出现错误  因为自己还没开启就向自己注册这个时候会有错误,虽然不会影响。推荐关闭

3 - 发开eureka client 客户端

  • pom.xml
    <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>
  • 入口类
@SpringBootApplication
@EnableEurekaClient
public class EurekaClient8080 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClient8080.class, args);
    }
}
  • 配置 application.yml

spring:
  application:
    name: EUREKACLIENTPRODUCT
server:
  port: 8080

eureka:
  client:
    register-with-eureka: true
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka
service-url:
  defaultZone: http://localhost:8761/eureka
  客户端的defaultZone 要指向服务注册中心暴露的服务

4 - 开发注意事项

  1. 服务注册中心自己向自己注册的配置项一定要关闭
  2. 服务注册中心把自己设置为服务注册中心的配置项改为false(false表示自己是注册中心,true表示自己不是注册中心)
  3. 注册中心一定比客户端先开启
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值