微服务——注册中心

1. Eureka注册中心

eureka服务端

  1. 引入依赖:pom.xml
<dependencies>
	<!-- eureka server 注册中心服务端依赖-->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	</dependency>
</dependencies>
  1. 启动类上标注:@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
  1. 配置文件:application.yml
server:
  port: 50101
spring:
  application:
    name: eureka-server

eureka:
  client:
    register-with-eureka: false    # 表示是否将自己注册到Eureka Server。
    fetch-registry: false          # 表示是否从Eureka Server获取注册的服务信息。
    service-url:			# 多个注册中心时使用
      defaultZone:  http://192.168.1.222:50101/eureka, http://192.168.1.222:50102/eureka
  server:
    enable-self-preservation: false   # 是否开启自我保护模式,在开发阶段建议关闭自保护模式
    eviction-interval-timer-in-ms: 60000   # 服务注册表清理间隔,单位毫秒,默认是 60*1000

eureka客户端

  1. 引入依赖:pom.xml
<dependencies>
	<!-- eureka server 注册中心服务端依赖-->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>
</dependencies>
  1. 启动类上标注:@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}
  1. 配置文件:application.yml
server:
  port: 50101
spring:
  application:
    name: eureka-client1

eureka:
  client:
    register-with-eureka: true    # 表示是否将自己注册到Eureka Server。
    fetch-registry: true          # 表示是否从Eureka Server获取注册的服务信息。
    service-url:             # 多个注册中心时使用
      defaultZone: http://192.168.1.222:50101/eureka
  instance:
    prefer-ip-address: true      #是否显示IP地址
    ip-address: 192.168.1.222   #指定此实例的ip
    lease-renewal-interval-in-seconds: 30   #心跳发发送频率,指定续约更新频率。如果在eureka在指定时间后没有收到心跳,将摘除改实例。
    lease-expiration-duration-in-seconds: 90   #设置过期剔除时间(单位秒)

2. Nacos注册中心

nacos服务端

  1. 下载nacos:nacos:下载页面
  2. 运行:
    解压下载的压缩包
    双击nacos\bin\startup.cmd

nacos客户端

  • 引入依赖:pom.xml

    <dependencies>
    	<!-- nacos 注册中心客户端依赖-->
    	<dependency>
    		<groupId>com.alibaba.cloud</groupId>
    		<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    	</dependency>
    </dependencies>
    
  • 启动类上标注:@EnableDiscoveryClient

    @EnableDiscoveryClient
    @SpringBootApplication
    public class NacosClientApplication {
        public static void main(String[] args) {
            SpringApplication.run(NacosClientApplication .class, args);
        }
    }
    
  • 配置文件:application.yml

    server:
      port: 50101
    spring:
      application:
        name: nacos-client1
      cloud:
      	nacos:
       	  discovery:
       	  	server-addr: 127.0.0.1:8848
    

nacos管理页面

  • 浏览器访问:localhost:8848/nacos
    用户名:nacos
    密 码:nacos
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值