十五、springcloud(一)注册中心Eureka

1、Eureka的基本架构

a、Eureka Server

    提供服务注册和发现

  b、Service Provider

    服务提供方

    将自身服务注册到Eureka,从而使服务消费方能够找到

  c、Service Consumer

    服务消费方

    从Eureka获取注册服务列表,从而能够消费服务

2、 创建父工程spring-cloud(pom),添加依赖

  

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- spring boot -->
        <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>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <repositories>
         <repository>
             <id>spring-milestones</id>
             <name>Spring Milestones</name>
             <url>https://repo.spring.io/milestone</url>
             <snapshots>
                 <enabled>false</enabled>
             </snapshots>
         </repository>
     </repositories>

3、创建子工程spring-cloud-eureka(jar),为保证项目找到main方法

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>spring-cloud-houge-eureka</finalName>
    </build>
扫描二维码关注公众号,回复: 3575361 查看本文章

4、application.properties

spring.application.name=spring-cloud-eureka

server.port=8000
#是否将自己注册到Eureka Server
#eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册信息
eureka.client.fetch-registry=false

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

5、main方法

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

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

6、启动工程后,访问:http://localhost:8000/  

 

 

猜你喜欢

转载自www.cnblogs.com/monkeybrother/p/9785871.html