微服务 注册中心 Eureka

使用Eureka作为服务注册中心

本文主要包含3部分,1. 启动注册中心;2. 服务提供者注册服务;3. 服务消费者,请求注册中心中发布的服务

1. 创建项目,添加maven依赖

创建maven项目,取名为eureka-server,添加以下依赖


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath />
	</parent>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>



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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
				<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2. 注册中心启动类

创建Eureka启动类EurekaServerApplication;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}	
}

3. 添加配置文件application.properties

spring.application.name=eureka-server
server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

spring.security.user.name=fragrans
spring.security.user.password=123456

eureka.server.enable-self-preservation=false
eureka.server.eviction-interval-timer-in-ms=5000

4. 启动注册中心

运行EurekaServerApplication ,访问localhost:8761,即可登陆到注册中心管理页面,如下图所示,输入配置文件中spring.security的用户名和密码,

在这里插入图片描述

在这里插入图片描述
看到上图所示的Web控制台,表示Eureka注册中心启动成功,接下来将服务提供者和消费者注册到该Eureka注册中心即可。

5. 代码样例github地址

代码github地址


服务器提供者注册服务 和 服务消费者,请参考接下来的文章…
参考资料:《Spring Cloud微服务 入门 实战与进阶》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值