Spring Cloud(1) Eureka - 服务注册中心搭建

构建服务注册中心

引入依赖库


        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
        Edgware.SR1
    

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

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

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

配置文件 application.properties

# 端口号
server.port=5000

# 服务名
spring.application.name=eureka-server

# 是否注册自己
eureka.client.register-with-eureka=false
# 是否需要检索服务
eureka.client.fetch-registry=false

启动类 MainApplication

@EnableEurekaServer //  开启注册中心服务
@SpringBootApplication
public class MainApplication {

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

}

启动服务

访问 http://localhost:5000/

image.png

显示上图,说明服务注册中心搭建完成

注册一个应用account到注册中心

引入依赖库


        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
        Edgware.SR1
    

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

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

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

配置文件 application.properties

# 端口号
# 端口号
server.port=8000

# 服务名
spring.application.name=account

# eureka服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/

启动类 MainApplication

@EnableEurekaClient
@SpringBootApplication
@RestController
@RequestMapping
public class MainApplication {

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


    @GetMapping("hello")
    public String hello(@RequestParam("name") String name) {
        return "hello " + name;
    }

}

启动服务

访问 http://localhost:5000/

image2.png

图中可以看出,account服务已经注册到了eureka-server中

访问 http://localhost:8000/hello?name=liuxd

image.png

account服务可以正常访问

Spring Cloud Eureka 重要参数详解

你可能感兴趣的:(Spring Cloud(1) Eureka - 服务注册中心搭建)