SpringCloud学习笔记(1):Eureka注册中心

简介

Eureka是Netflix开源的基于rest的服务治理方案,分为Server端和Client端,Server端为注册中心,其他微服务通过Client端连接Server端进行服务的注册和发现。

项目介绍

  1. sc-parent,父模块
  2. sc-provider,提供者模块
  3. sc-eureka,注册中心
  4. sc-consumer-discovery,消费者模块

搭建父模块

创建父模块sc-parent,pom.xml:


  4.0.0
   com.cf
  sc-parent
  0.0.1-SNAPSHOT
  pom
   
   
   
       org.springframework.boot
       spring-boot-starter-parent
       2.1.6.RELEASE
        
   
   
   
    
    3.1.1
   
   
   
        
        
            org.springframework.boot
            spring-boot-starter-web
        
   
   
   
        
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                Greenwich.SR2
                pom
                import
            
        
   
   
   
        
               
               
                   org.springframework.boot
                   spring-boot-maven-plugin
               
               
               
               
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    
                        1.8
                        1.8
                    
                
        
   

搭建注册中心

1.在父模块下创建子模块项目sc-eureka,pom.xml:


  4.0.0
  
    com.cf
    sc-parent
    0.0.1-SNAPSHOT
  
  sc-eureka
  
  
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-server
    
  

2.创建启动类eureka.EurekaApplication:

package eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

//声明该类为SpringBoot服务的入口
@SpringBootApplication
//声明该微服务为注册中心,提供服务发现和注册的功能
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

3.创建配置文件/src/main/resources/application.yml:

server:
  port: 8080 #当前服务端口

eureka:
  instance:
    hostname: localhost #当前Eureka实例主机名
  client:
    registerWithEureka: false #表示不向注册中心注册自己
    fetchRegistry: false #表示此客户端不需要从Eureka注册中心获取Eureka注册表信息
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/ ##eureka对外提供的地址(客户端连接的地址)

其他配置信息可以参考EurekaInstanceConfigBean和EurekaClientConfigBean两个配置类

4.运行启动类EurekaApplication,在浏览器中访问http://localhost:8080/,出现如下图表示注册中心搭建成功:

SpringCloud学习笔记(1):Eureka注册中心_第1张图片

提供者与服务注册

1.在父模块下创建子模块项目sc-provider,pom.xml:


  4.0.0
  
    com.cf
    sc-parent
    0.0.1-SNAPSHOT
  
  sc-provider
  
  
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
  

2.创建启动类provider.ProviderApplication:

package provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

3.创建Controller:provider.controller.BookController

package provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/book")
@RestController
public class BookController {
    
    @GetMapping("/list")
    public String getBookList(){
        //模拟从service返回数据
        return "[\"Java入门到放弃\",\"C++入门到放弃\",\"Python入门到放弃\",\"C入门到放弃\"]";
    }
}

4.创建配置文件/src/main/resources/application.yml:

server:
  port: 8081

spring:
  application:
    name: sc-provider #注册到Eureka注册中心上的服务名称,对应Eureka界面上的Application列
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/ #注册中心的访问地址
  instance:
    preferIpAddress: true #表示将自己的IP注册到Eureka注册中心。默认为false,表示将hostname注册到注册中心

5.依次启动注册中心sc-eureka和提供者sc-provider,当提供者启动时,会将自己的信息注册到Eureka注册中心,在浏览器中访问http://localhost:8080/,提供者sc-provider已经在注册中心注册。

SpringCloud学习笔记(1):Eureka注册中心_第2张图片

消费者和服务发现

1.在父模块下创建子模块项目sc-consumer-discovery,pom.xml:


  4.0.0
  
    com.cf
    sc-parent
    0.0.1-SNAPSHOT
  
  sc-consumer-discovery
  
  
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
  

2.创建启动类consumer.ConsumerDiscoveryApplication:

package consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumerDiscoveryApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerDiscoveryApplication.class, args);
    }
    
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

3.创建调用提供者服务的Controller:consumer.controller.ConsumerDiscoveryController

package consumer.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerDiscoveryController {
    @Autowired
    private DiscoveryClient discoveryClient;
    
    @Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/getBookList")
    public String getBookList(){
        //通过服务名获取实例信息
        List list = discoveryClient.getInstances("sc-provider");
        if (list != null && list.size() > 0 ) {
            //调用服务,并返回服务结果
            return restTemplate.getForObject(list.get(0).getUri() + "/book/list", String.class);
        }
        return null;
    }
}

4.创建application.yml:

server:
  port: 8082

spring:
  application:
    name: sc-consumer-discovery
    
eureka:
  client:
    registerWithEureka: false #在本实例中消费者不提供服务,所以无需到注册中心注册。在实际应用中,消费者也可能是提供者。
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

5.依次启动注册中心sc-eureka、提供者sc-provider、消费者sc-consumer-discovery,当消费者启动时,会从注册中心查询可用的服务列表及其网络地址。直接访问提供者和消费者调用提供者结果如下:

SpringCloud学习笔记(1):Eureka注册中心_第3张图片

总结

在传统的应用程序中,都是把提供者的网络地址硬编码在代码中,导致提供者和消费者耦合度高,当提供者网络地址发生了变化,则需要修改消费者配置并重新发布。Eureka起到了解耦的作用,提供者到Eureka注册中心中注册,消费者从Eureka注册中心中获取提供者的网络地址并进行调用,当提供者网络地址变更时会重新到注册中心注册。

你可能感兴趣的:(SpringCloud学习笔记(1):Eureka注册中心)