SpringCloud-Eureka 服务注册与发现,搭建 服务端和客户端

注册中心服务端搭建

1 新建springboot项目,添加 Spring-cloud-Discovery》》》》》》》Eureka Server依赖

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

2 配置Application.yml文件

server:
  port: 8761

eureka:
  instance:
    #指定主机地址
    hostname: 192.168.0.12
  client:
    #声明自己是个服务端
    #指定是否要注册到注册中心(注册中心服务 不需要开启)
    registerWithEureka: false
    #指定是否要从注册中心获取服务(注册中心不需要开启)
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

#  server:
#    #关闭保护模式 (默认是开启)
#    enable-self-preservation: false

#服务的名称
spring:
  application:
    name: eureka-service

3 启动类上添加注解 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer //开启Eureka注册中心服务
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Eureka客户端搭建 (所有其它的服务都按这个模式来)
1 新建springboot项目,添加 Spring-cloud-Discovery》》》》》》》Eureka Discovery Client依赖

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

2 配置 Application.yml文件

server:
  port: 8771


#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.0.12:8761/eureka/

#服务的名称
spring:
  application:
    name: product-service

3 启动类上添加注解 @EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient //Eureka客户端
public class ProductServiceApplication {

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

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值