SpringCloud (一)、Eureka註冊中心初體驗-v1.0

一、新建Springboot工程就叫myeureka吧spring

二、添加引用app

<!--eureka server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<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>

三、啓動配置spring-boot

server:
    port: 8761 #端口

security:
    basic:
        enabled: true #開啓認證
    user:
        name: user #認證名稱
        password: 123456 #認證密碼

eureka:
    instance:
        hostname: localhost #服務host
    client:
        registerWithEureka: false #關閉自我註冊
        fetchRegistry: false
        serviceUrl:
            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

四、開啓註冊中心服務fetch

啓動類添加註解 @EnableEurekaServer

五、啓動服務3d

暫無服務註冊server

六、建立註冊服務blog

新建一個Springboot工程就叫myclientip

七、添加依賴ci

<!--eureka server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 用於註冊中心訪問帳號認證 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

八、客戶端配置it

server:
    port: 8081 #8181
spring:
    application:
        name: myclient

eureka:
    client:
        serviceUrl:
            defaultZone: http://user:123456@localhost:8761/eureka  #註冊 中心已經開啓認證
    instance:
        prefer-ip-address: true
        instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}

九、啓動EnableEureka客戶端

啓動類添加註解 @EnableEurekaClient 啓動EnableEureka客戶端
@EnableEurekaClient //啓動EnableEureka客戶端
@EnableDiscoveryClient
@SpringBootApplication
public class MyClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyClientApplication.class, args);
    }
}

服務註冊成功   另外一個服務是註冊中心能夠忽略下次再講

相關文章
相關標籤/搜索