写点什么

深入微服务 - 服务注册与发现 SpringCloud Eureka 之基础

作者:janyxe
  • 2022 年 5 月 19 日
  • 本文字数:2387 字

    阅读完需:约 8 分钟

深入微服务-服务注册与发现 SpringCloud Eureka之基础

微服务系列文章目录


如果本文对你们的开发之路有所帮助,请帮忙点个赞,您的支持是我坚持写博客的动力

【面试相关】微信关注【Java 从零学架构】,后台回复【面试题目】自取

前言

本系列带着大家深入微服务 Spring 体系的各个框架的底层原理。上一篇文章介绍了 SpringCloud 调用组件 Feign,本节将带着大家掌握微服务服务注册与发现框架 Spring Eureka



什么是服务注册与发现?

  • 服务注册需要解决的问题:当微服务相互调用的时候,A 服务调用 B 服务,A 服务需要获取 B 服务的调用地址这个就是服务发现

  • 服务注册:服务注册与发现中有一个注册中心(Service Registry),当接入的客户端(服务提供方)启动的时候会把自己的服务 url 以及端口号等信息注册到注册中心(Service Registry)上

  • 服务发现:服务消费方调用服务提供方,需要从注册中心(Service Registry)获取服务实际的通信地址,实现 rpc 远程调用

  • 目前市面上服务注册与发现框架有:Eureka、Consul、Nacos、etcd、Dubbo(注册中心为 Zookeeper)


服务注册与发现框架

SpringCloud 集成 Eureka

注册中心搭建

1.引入 Maven 库

代码如下(示例):


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

2.开启注册中心功能

@EnableEurekaServer 注解:启动 EurekaServer


@EnableEurekaServer@SpringBootApplicationpublic class EurekaServer {   public static void main(String[] args) {    SpringApplication.run(EurekaServer.class, args);  } }
复制代码


配置文件 application.yaml


server:  port: 8007 # server 端口号eureka:  instance:    hostname: 127.0.0.1 # eureka 注册的主机地址  client:    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    register-with-eureka: false  # 指定是否从注册中心获取服务(注册中心不需要开启)    fetch-registry: false  # 指定是否注册到注册中心(注册中心不需要开启)
复制代码



服务提供方

1、引入 Maven

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

2、集成服务

1)集成启动类 @EnableEurekaClient 注解为客户端开启服务注册与发现功能


@SpringBootApplication@EnableEurekaClientpublic class ProviderApplication{   public static void main(String[] args) {    SpringApplication.run(ProviderApplication.class, args);  } }
复制代码


2)配置注册 Eureka 相关配置信息


server:  port: 8001spring:    application:        name: provider-servereureka:  client:    service-url:           defaultZone: http://localhost:8007/eureka ##填写注册中心地址    register-with-eureka: true # 指定是否从注册中心获取服务    fetch-registry: true  #指定是否注册到注册中心
复制代码


3)提供数据服务接口


@RestControllerpublic class HelloController { @RequestMapping("/hello")public String hello() {    return "hello";  }}
复制代码

服务消费者

1)集成启动类 @EnableEurekaClient 注解为客户端开启服务注册与发现功能 @LoadBalanced 启用服务负载均衡功能


@SpringBootApplication@EnableEurekaClientpublic class ConsumerApplication{   public static void main(String[] args) {    SpringApplication.run(ConsumerApplication.class, args);  }    @Bean  @LoadBalanced  RestTemplate restTemplate() {    return new RestTemplate();  } }
复制代码


2)配置注册 Eureka 相关配置信息


server:  port: 8002spring:    application:        name: consumer-servereureka:  client:    service-url:       defaultZone: http://localhost:8007/eureka ##填写注册中心地址    register-with-eureka: true # 指定是否从注册中心获取服务    fetch-registry: true  #指定是否注册到注册中心
复制代码


3)服务调用


@RestController@Slf4jpublic class DataController {  @Autowired  private RestTemplate restTemplate;   @RequestMapping("/getHello")  public String getHello() {    // rpc调用服务提供方的接口    String providerUrl = "http://provider-server/hello";    String result = restTemplate.getForObject(providerUrl, String.class);    log.info("服务调用结果 {}",result)    return result;  } }
复制代码

Eureka 集成认证中心

1)Maven 依赖


<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>
复制代码


修改 application.yaml


server:  port: 8002spring:    application:        name: consumer-server    security: #配置SpringSecurity登录用户名和密码      user:        name: user        password: passwordeureka:  client:    service-url:           defaultZone: http://localhost:8007/eureka ##填写注册中心地址    register-with-eureka: true # 指定是否从注册中心获取服务    fetch-registry: true  #指定是否注册到注册中心
复制代码


3)Security 配置类


@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception { super.configure(http); http.csrf().disable(); http.headers().frameOptions().disable(); }}
复制代码


发布于: 刚刚阅读数: 6
用户头像

janyxe

关注

勤能补拙 2020.05.26 加入

程序员一枚,喜欢 云原生、前端、后端开发领域

评论

发布
暂无评论
深入微服务-服务注册与发现 SpringCloud Eureka之基础_微服务_janyxe_InfoQ写作社区