Spring Cloud工程搭建及Docker容器部署

微服務搭建及部署

基於Spring Cloud 搭建微服務項目,目前我們用到的微服務組件有EurekaHystrixRibbonZuul
初始化maven結構的Spring Boot 項目,可以進http://start.spring.io/,需要用什麼組件直接選擇就行。目前主流IDE(STS(eclipse)、Intellij Idea等)都已經集成了這個功能。
下面的例子就是從Idea初始化的目錄結構開始,如下圖

├─.mvn
│  └─wrapper
├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─example
│  │  │          └─demo
│  │  └─resources
│  │      ├─static
│  │      └─templates
│  └─test
│      └─java
│          └─com
│              └─example
│                  └─demo
└─target
    ├─classes
    │  ├─com
    │  │  └─example
    │  │      └─demo
    │  └─templates
    └─test-classes
        └─com
            └─example
                └─demo

Eureka 服務端

  • 首先在pom 文件中引入
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  • 然後在Spring Boot入口啓動類EurekaServerApplication加入Eureka客戶端註解@EnableEurekaClient
package com.example.seven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
  • Eureka Server 配置applications.yml
server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      default-zone: http://localhost:${server.port}/eureka/

Eureka 客戶端1

  • 首先在pom 文件中引入
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 然後入口啓動類加入Eureka客戶端註解@EnableEurekaClient註解
package com.example.seven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class BussinessApplication {

    public static void main(String[] args) {
        SpringApplication.run(BussinessApplication.class, args);
    }
}
  • Eureka Client配置applications.yml
server:
  port: 8762
spring:
  application:
    name: microservice-bussiness
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

Eureka 客戶端2

  • 首先在pom 文件中引入
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 然後入口啓動類加入Eureka客戶端註解@EnableEurekaClient註解
package com.example.seven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class BussinessApplication {

    public static void main(String[] args) {
        SpringApplication.run(BussinessApplication.class, args);
    }
}
  • Eureka Client配置applications.yml
server:
  port: 8763
spring:
  application:
    name: microservice-bussiness
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
        <!-- 這裏是我遇到的一大坑,項目可以正常啓動沒有任何問題,但是沒有註冊到服務中心 -->
<!--         <dependency> -->
<!--             <groupId>org.springframework.cloud</groupId> -->
<!--             <artifactId>spring-cloud-netflix-eureka-client</artifactId> -->
<!--         </dependency> -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

相繼啓動Eureka Server、Client1、Client2 三個工程
訪問localhost:8761
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章