美文网首页
Spring Cloud(1) Eureka - 服务注册中心搭

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

作者: 挨板砖的码农 | 来源:发表于2018-01-31 13:54 被阅读0次

构建服务注册中心

引入依赖库

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置文件 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到注册中心

引入依赖库

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置文件 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 - 服务注册中心搭

      本文链接:https://www.haomeiwen.com/subject/otukzxtx.html