Eureka注册中心的介绍与使用

一、注册中心的主要功能
1、服务发现
1)、服务注册/反注册:保存服务提供者和服务调用者的信息
2)、服务订阅/取消订阅:服务调用者订阅服务提供者的信息,最好有实时推送的功能
3)、服务路由(可选):具有筛选整合服务提供者的能力。
2、服务配置
1)、配置订阅:服务提供者和服务调用者订阅微服务相关的配置
2)、配置下发:主动将配置推送给服务提供者和服务调用者
3、服务健康监测
检测服务提供者的健康情况
二、Eureka的基本架构
1、 Eureka Server
提供服务注册和发现
2、Service Provider
a、 服务提供方
b、将自身服务注册到Eureka,从而使服务消费方能够找到
3、Service Consumer
a、服务消费方
b、从Eureka获取注册服务列表,从而能够消费服务
三、Eureka交互流程与原理
在这里插入图片描述
四、搭建Eureka注册中心
1、创建一个注册中心子模块
2、引入maven坐标

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

3、配置application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/
registerWithEureka: 是否将自己注册到Eureka服务中,本身就是所有无需注册
fetchRegistry : 是否从Eureka中获取注册信息
serviceUrlEureka: 客户端与Eureka服务端进行交互的地址
4、配置启动类
创建启动类 EurekaServerApplication
java @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
五、将服务注册到注册中心
1、在服务模块中引入坐标
在服务 的pom文件中添加eureka client的相关坐标

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


2、配置application.yml文件
在工服务程的 application.yml 中添加Eureka Server的主机地址
eureka:
client:
serviceUrl: # eureka server的路径
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true #使用ip注册
3、修改启动类添加服务注册注解

@SpringBootApplication
		//@EnableDiscoveryClient
		//@EnableEurekaClient
		public class UserApplication {
		 public static void main(String[] args) {
		 SpringApplication.run(UserApplication.class, args);
		 }
		}

从Spring Cloud Edgware版本开始, @EnableDiscoveryClient 或 @EnableEurekaClient 可
省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上
六、对服务的调用
这里涉及Eurreka的元数据
元数据有两种:标准元数据和自定义元数据
1、标准元数据:主机名、IP地址、端口号、状态页和健康检查等信息,这些信息都会被发布在服务注
册表中,用于服务之间的调用。
2、自定义元数据:可以使用eureka.instance.metadata-map配置,符合KEY/VALUE的存储格式。这
些元数据可以在远程客户端中访问。
在程序中可以使用DiscoveryClient 获取指定微服务的所有元数据信息

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class RestTemplateTest {
	 @Autowired
	 private DiscoveryClient discoveryClient;
	 @Test
	 public void test() {
	 //根据微服务名称从注册中心获取相关的元数据信息
	 List<ServiceInstance> instances = discoveryClient.getInstances("shopservice-product");
	 //获取唯一的一个元数据
	ServiceInstance instance = instances.get(0);
	//根据元数据中的主机地址和端口号拼接请求微服务的URL
	Product product = null;
	//如何调用商品服务?
	product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/product/1",Product.class);
	return product;
	 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值