第一篇:springCloud 服务的注册与发现

一. spring Cloud 简介

        spring cloud 基于 spring boot , 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、时间总线、全局锁、决策锁、分布式会话等。

       基于spring cloud 开发的应用程序特别适合在Docker或者其他专业平台Paas(平台即服务,如Cloud Foundry)部署,所以又称作原生云应用(Cloud Native Application)。

二. 创建服务注册中心

      在这里,我们需要在用到的组件上Spring Cloud Netflix的Eureka , eureka 是一个服务注册模块。

       2.1 首先创建一个maven主工程

       2.2 然后创建2个model工程,一个model工程为注册中心,即Eureka Server ,另一个作为Eureka Client 

             先创建一个server,以server为例,详细说明创建过程:

             右键工程 -> 创建mould -> 选择spring initialir ,如下图

第一篇:springCloud 服务的注册与发现_第1张图片

下一步 -> 选择 Spring Cloud Config -> Config Server,然后一直下一步就可以了

第一篇:springCloud 服务的注册与发现_第2张图片

 

 创建完后的工程pom.xml文件如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
         
    
    com.example
    eurekaserver
    0.0.1-SNAPSHOT
    eurekaServer
    Demo project for Spring Cloud Eureka

    
        1.8
        Hoxton.SR1
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.3 启动一个服务注册中心

      只需要一个注解 @EnableEurekaServer ,这个注解在spring boot 工程的启动类上加,即可

/**
 * 启动一个服务注册中心,只需要一个注解:@EnableEurekaServer
 * 这个注解需要在spring boot 工程的启动 application类上添加
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

2.4 Eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,(可以在内存中完成),在默认的情况下,eureka server 也是一个eureka client ,因此必须要指定一个server。eureka server的配置文件如下:

server:
  port: 9001

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
通过eureka.client.registerWithEureka:false 和fetchRegistry:false来表明自己是一个eureka server

2.5 eureka server 是有界面的,启动工程,打开浏览器访问:

      http://localhost:9001,界面如下:

第一篇:springCloud 服务的注册与发现_第3张图片

因为没有服务来注册,所以显示为:No instances available  

 

三. 创建一个服务提供者(eureka client)

当 client 向 server 注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个 client 实例接收心跳消息。 如果心跳超时,则通常将该实例从注册 server 中删除。

创建过程同server一样,创建完后pom文件如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
         
    
    com.example
    eurekaclient
    0.0.1-SNAPSHOTconfig
    eurekaClient
    Demo project for Spring Boot

    
        1.8
        Hoxton.SR1
    

    
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-config-server
            2.0.2.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.springframework.cloud
            spring-cloud-netflix-eureka-client
            2.2.1.RELEASE
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

 通过注解@EnableEurekaClient ,标明自己是一个  eureka client 

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

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

    @Value("${server.port}")
    String port;
    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi "+name+",i am from port:" +port;
    }

}

还需要在配置文件中报名自己的服务注册中心地址,application.yml 配置文件如下:

server:
  port: 9002

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/

spring:
  application:
    name: service-hi

需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个 name 。
启动工程,打开http://localhost:9001 ,即eureka server 的网址:

第一篇:springCloud 服务的注册与发现_第4张图片

你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862

这时打开 http://localhost:9002/hi?name=Tom ,你会在浏览器上看到 :

hi Tom,i am from port:9002

 

你可能感兴趣的:(SpringCloud)