01、SpringCloud Eureka Server

一、代码实例

说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
1.maven依赖
parent pom.xml

    
        1.8
        Greenwich.SR5
        2.1.13.RELEASE
        ${java.version}
        UTF-8
        ${java.version}
    

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

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

server子模块依赖

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

2.application.yml配置

server:
  port: 7001
spring:
  application:
    name: server
eureka:
  client:
    #要不要去注册中心获取其他服务的地址
    fetch-registry: false
    #自己就是注册中心,不用注册自己
    register-with-eureka: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  instance:
    hostname: localhost

3.启动类
启动类增加@EnableEurekaServer表示该服务是一个eureka注册服务

@SpringBootApplication
@EnableEurekaServer
public class Server7001Application {
    public static void main(String[] args) {
        SpringApplication.run(Server7001Application.class,args);
    }
}

测试验证

访问http://localhost:7001/
结果如下:

image.png

表明服务启动成功

以上为单个server,若是多个server,则需要进行相互注册,本利以windows下模拟为例。
首先在C:\Windows\System32\drivers\etc\hosts中添加如下映射

127.0.0.1 eureka7001
127.0.0.1 eureka7002
127.0.0.1 eureka7003

以上表示,访问eureka7001、eureka7002、eureka7003时其实访问的时本地。
将上述的server复制2份,分别为server2、server3,注意修改端口号,同时修改application.yml中的eureka.client.service-url.defaultZone,代码如下:
server2的application.yml

server:
  port: 7002
spring:
  application:
    name: server
eureka:
  client:
    #要不要去注册中心获取其他服务的地址
    fetch-registry: false
    #自己就是注册中心,不用注册自己
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7003:7003/eureka/
  instance:
    hostname: localhost
    instance-id: server-7002

server3的application.yml

server:
  port: 7003
spring:
  application:
    name: server
eureka:
  client:
    #要不要去注册中心获取其他服务的地址
    fetch-registry: false
    #自己就是注册中心,不用注册自己
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/
  instance:
    hostname: localhost
    instance-id: server-7003

当然server的eureka.client.service-url.defaultZone也要做相应修改,对应值份server2和server3的地址

      defaultZone: http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/

分别启动server、server2、server3,然后分别访问http://eureka7001:7001/、http://eureka7002:7002/、http://eureka7003:7003/
效果分别如下:

image.png

image.png

image.png

说明配置生效。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
参考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
还有尚硅谷周阳老师的视频

你可能感兴趣的:(01、SpringCloud Eureka Server)