SpringCloud学习(一)Eureka服务注册和发现组件问题引入和代码详细过程

项目代码GitHub地址:SpringCloud入门学习

SpringCloud并不是一个新的技术栈,它是微服务架构的集大成者,为分布式微服务提供了一整套解决方案,将一系列优秀的组件进行了整合,让我们熟悉Spring框架的开发者极易上手,通过简单的配置即可达到想要的效果
SpringBoot目的就是简化Spring应用的搭建,旨在让我们通过少量的配置即可开箱使用,让开发者真正关注业务逻辑的开发
SpringCloud依托SpringBoot搭建一个个服务

下面慢慢的引入我们的第一个SpringCloud组件,Eureka服务注册发现中心

我们先从没有注册中心的微服务开始说起

  1. 没有注册中心,消费者想要消费提供者提供的服务,需要使用RestTemplate,向容器里面添加RestTemplate组件

    @Configuration
    public class RestConfig {
    
        /**
         * 向容器里面添加RestTemplate组件
         */
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    
    }
    
  2. 使用RestTemplate调用服务

    	/**
         * 定义url常量
         */
        private static final String REST_URL = "http://127.0.0.1:8081";
    
        @Autowired
        private RestTemplate restTemplate;
    
        /**
         * 获取员工列表,调用服务
         * 1. 通过RestTemplate调用服务,get请求参数列表:url、返回值类型(参数列表写在url中)
         * 2. 通过RestTemplate调用服务,post请求参数列表:url、参数列表、返回值类型
         */
        @GetMapping("/list")
        public List<EmployeeModel> list() {
            return restTemplate.getForObject(REST_URL + "/emp/list", List.class);
        }
    

    可以发现调用服务,每次都需要明确服务的url地址,是很麻烦的
    那有什么好的方法替代呢
    引入Eureka服务注册和发现组件,只需要将提供者提供的服务注册到Eureka中,然后消费者就可以通过服务名称访问该服务,就不需要再像上面一样写难以记住的url地址

  3. 搭建Eureka服务中心,添加依赖

    <!-- eureka服务注册中心 -->
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
       <!-- 这里会自动引入版本,类似parent标签继承 -->
    </dependency>
    
    <dependencyManagement>
            <dependencies>
                <!-- <scope>import</scope>解决单继承问题,类似parent标签, -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    </dependencyManagement>
    
  4. yml文件编写配置

    #eureka配置
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false  #表示不向注册中心注册自己
        fetch-registry: false  #表示不向注册中心检索信息(自己就是注册中心)
        service-url:
          defaultZone: http://localhost:7000/eureka/
    
  5. SpringBoot启动类添加@EnableEurekaServer注解 ,表明这个eureka的服务端(服务中心)

  6. 将服务提供者的服务注册到Eureka中,添加依赖

    		<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                <!-- 这里会自动引入版本,类似parent标签继承 -->
            </dependency>
    
        <dependencyManagement>
            <dependencies>
                <!-- <scope>import</scope>解决单继承问题,类似parent标签, -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
  7. 编写服务提供者yml配置

    spring:
      application:
        name: springcloud-employee-provider  # 应用名称,注册到服务中心
    #eureka服务中心的位置,作为客户端用于发现和注册服务
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7000/eureka/
    

    注意spring.application.name不能缺少,这就是用于注册到服务中心的名称以及给消费者调用的名称

  8. SpringBoot启动类添加@EnableEurekaClient注解 ,开启向服务中心eureka发现和注册服务(作为客户端)

  9. 服务消费者,添加依赖,和服务提供者一致

  10. 编写服务消费者yml配置

    spring:
    	application:
    		name: springcloud-employee-provider  # 应用名称,注册到服务中心
    #eureka服务中心的位置,作为客户端用于发现和注册服务
    eureka:
    	client:
    		register-with-eureka: false  #表示不向注册中心注册自己(消费者不需要注册自己)
    		service-url:
    			defaultZone: http://localhost:7000/eureka/
    
  11. SpringBoot启动类添加@EnableEurekaClient注解 ,开启向服务中心eureka发现和注册服务(作为客户端)。这里虽然是服务消费者,但是同样对于Eureka来说是客户端,也会向Eureka注册自己,但同时有向Eurek发现服务的功能

  12. 这时候再来到消费者端,使用RestTemplate调用服务,需要做一下修改,

    @Configuration
    public class RestConfig {
    
        /**
         * 向容器里面添加RestTemplate组件
         */
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    
    }
    

    添加了@LoadBalanced注解,表示开启负载均衡,使用默认的Ribbon组件,就是告诉RestTemplate向eureka中心调用哪个服务(将在下一节详细讲解)

  13. 最后消费者调用服务

    	/**
         * 获取员工列表,从eureka调用服务(使用application name代替ip+端口)
         * 1. 通过RestTemplate调用服务,get请求参数列表:url、返回值类型(参数列表写在url中)
         * 2. 通过RestTemplate调用服务,post请求参数列表:url、参数列表、返回值类型
         * 3. 上面的返回值类型就是:以什么方式去接受响应体的内容
         */
        @GetMapping("/list2")
        public List<EmployeeModel> list2() {
            return restTemplate.getForObject("http://SPRINGCLOUD-EMPLOYEE-PROVIDER/emp/list", List.class);
        }
    

    可以对比一下,跟上面的不同,这里使用了前面定义的spring.application.name代替难以记住的url

  14. 至此完成了服务在Eureka组件下的服务调用,算是SpringCloud入门吧,如有不足谢谢指正

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值