Eureka入门教程

Eureka入门教程

​ 一如码农深似海,今朝有酒今朝醉。入坑已经五年多了,人总有所坚持,立个flag,想把spring、springboot、springcloud从入门教程到源码分析,希望能对于读者有用,ekko写文章喜欢详尽,源代码有点多,不喜勿喷。


环境搭建

  • IDEA(个人推荐,功能强大不多说)、其次Spring Tools 4,工作这么多年,很大牛用这工具,也是不错的选择;其次eclipse。
  • jdk1.8环境。(google或者百度)
  • maven(gradle少部分公司用,暂不介绍,当然,gradle更强大)

版本

参考官方https://spring.io/projects/spring-cloud

spring cloud版本 springboot版本
Hoxton 2.2.x
Greenwich 2.1.x
Finchley 2.0.x
Edgware 1.5.x
Dalston 1.5.x

我选择spring cloud Hoxton.SR4、springboot版本2.2.5.RELEASE。(如果是公司项目建议用老版本,原因无它,坑少网上解决办法多)。


父级pom.xml文件



    4.0.0

    com.duoduo
    duoduo-parent
    1.0-SNAPSHOT
    pom

    
        UTF-8
        1.8
        1.8
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR4
                pom
                import
            

            
                org.springframework.boot
                spring-boot-dependencies
                2.2.5.RELEASE
                pom
                import
            

        
    



    
        duoduo-parent
        
            
                src/main/resources
                true
            
        
        
            
                org.apache.maven.plugins
                maven-resources-plugin
                
                    
                        $
                    
                
            
        
    

    
        duoduo-eureka
        duoduo-provider-service
    


duoduo-eureka子项目POM文件



    
        duoduo-parent
        com.duoduo
        1.0-SNAPSHOT
    
    4.0.0

    duoduo-eureka

    

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

        
            org.springframework.boot
            spring-boot-devtools
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        


        
            org.projectlombok
            lombok
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
    

eureka启动类

package com.duoduo.cloud;

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

/**
 * @author ekko
 * eureka启动类
 */
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class,args);
              // 官方文档,编译报错,编译报错,可以使用下面代码,个人还是喜欢上面那行代码,启动
              //new SpringApplicationBuilder(ServerApplication.class)
                .web(true).run(args);
      //new SpringApplicationBuilder(ServerApplication.class)
                .web(WebApplicationType.SERVLET).run(args);
      

    }

}

application.yml文件

server:
  port: 8761  #端口

eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    registerWithEureka: false #false表示不向注册中心注册自己。
    fetchRegistry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。

ServerApplication方法启动

2020-05-23 14:49:39.025  INFO 8031 --- [      Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl    : Renew threshold is: 1
2020-05-23 14:49:39.025  INFO 8031 --- [      Thread-17] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
2020-05-23 14:49:39.042  INFO 8031 --- [      Thread-17] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2020-05-23 14:49:39.112  INFO 8031 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8761 (http) with context path ''
2020-05-23 14:49:39.114  INFO 8031 --- [  restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761

控制台显示以上信息说明,eureka服务端启动成功。可以访问http://localhost:8761/。


duoduo-provider-service POM文件



    
        duoduo-parent
        com.duoduo
        1.0-SNAPSHOT
    
    4.0.0

    duoduo-provider-service

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

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
        

        
            org.projectlombok
            lombok
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
    

启动类

package com.duoduo.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ekko 不需要添加@EnableEurekaClient,后续有说明
 * eureka启动类
 */
@SpringBootApplication
@RestController
public class ServiceApplication {

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

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

}

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: DUODUO-PROVIDER-SERVICE # 项目服务名,如果不写服务名,默认服务名UNKNOWN

启动ServiceApplication方法。在访问http://localhost:8761/,会发现eureka控制台Instances currently registered with Eureka多了一个DUODUO-PROVIDER-SERVICE的服务。

可能遇到问题

  • eureka自我保护机制(不用看到控制台保红就认为eureka出问题了)

    不知道广大读者有没有了解过CAP模型,eureka是典型AP模型,这里提一句题外话,zookeeper是典型的CP模型,在eureka server运行期间,会统计客户端心跳的比例,15分钟是否超过85%,超过则把会把实例注册信息保护起来。再说一嘴zookeeper,在zk集群中,网络出现问题,zk重新选举leader,需要耗费不少的时间,这个期间,不可以zk注册服务,而eureka不同,可以继续注册。原因说这么多,面试官经常会问zk、eureka的区别。这就是这道题的痛点吧。


github地址

https://github.com/itekko/duoduo-parent.git

你可能感兴趣的:(Eureka入门教程)