SpringCloud初体验之Eureka

Eureka简介

SpringBoot简化了Spring工程的复杂度,之前复杂的Spring工程被拆分成了一个个小的SpringBoot工程。那么SpringBoot之间如何通讯,相互获取信息呢?这就用到了SpringCloud的Eureka小弟来组合管理这一个个小的SpringBoot,称其为微服务架构。Eureka就相当于管家,SpringBoot工程通过他来相互调用。当然Eureka也是一个SpringBoot工程,通过修改配置文件来让他变身成管家。那我们现在就需要创建3个SpringBoot工程,一个用来当作管家,两个用当作奴隶,由管家来统治他们。

管家Eureka的创建

添加依赖

通过Idea创建一个普通的SpringBoot工程,不要再用Eclipse了,Idea更好用。
SpringCloud与SpringBoot的版本问题还是得注意,我公司使用SpringBoot版本为1.5.10.RELEASE那么与其相对应的SpringCloud版本就为Dalston.RELEASE.其他相对应得版本
所以在pom.xml文件中添加SpringCloud的依赖以及eureka依赖:

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

修改application.properties文件

server.port=8888

# 不向注册中心注册自己
eureka.client.register-with-eureka=false

# 不需要检索服务
eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

添加@EnableEurekaServer注解

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

此时访问http://localhost:8888/ 就能看到eureka界面了.因为目前没有服务注册,所以Application是空的。

用户user和订单order的建立

添加依赖

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

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在application.properties配置文件中加上注册中心的配置:

eureka.client.service-url.defaultZone=http://localhost:8888/eureka/
# 以自身ip注册,不然默认注册本机
# instance:
#   prefer-ip-address: true
#   ip-address: 10.68.29.152
server.port=8282

并在主类中加入@EnableDiscoveryClient注解即可完成注册服务。

可以看到,此时Application已经注册了一个DIS-USER服务。

猜你喜欢

转载自www.cnblogs.com/bihanghang/p/10191720.html