docker微服務部署之:一,搭建Eureka微服務項目

先說明一下docker須要搭建的微服務的基本狀況:html

  • 項目狀況:一個demo_parent項目,下面三個子模塊:demo_eureka(eureka服務)、demo_article(文章服務)、demo_zuul(網關服務)。
  • 用到的資源有:jdk1.八、mysql5.7。
  • 須要用到的docker容器有:mysql(mysql單獨一個容器)、jdk1.8(article、eureka、zuul三個服務運行在該jdk1.8容器中)

一,用idea新建父項目

File->New->Project->Maven,選擇Project SDK爲jdk8->GroupId:com.demo,ArtifactId:demo_parentjava

修改pom.xml文件,完整以下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>demo_parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

<!-- spring boot parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.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> <lombok.version>1.16.10</lombok.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
   <!-- 引入lombok依賴,能夠在使用了@Data註解的類中,不用寫getter和setter方法 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>

2、新增demo_eureka模塊,並編寫代碼

右鍵demo_parent->new->Module->Maven,選擇Module SK爲jdk8->ArtifactId:demo_eurekamysql

1.修改pom.xml<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo_parent</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo_eureka</artifactId>


    <dependencies>
<!-- eureka 服務端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>

2.在resources目錄下新增application.yml文件

server:
  port: 7000 #eureka的啓動端口,可自定義
spring:
  application:
    name: demo-eureka #eureka項目名稱,可自定義
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://127.0.0.1:${server.port}/eureka #在IDEA中運行時使用127.0.0.1,部署發佈時,請修改成虛擬機宿主機的ip地址

注:因爲這裏講的是單機部署,因此eureka的register-with-eureka和fetch-registry都設置爲了falseweb

3.新建com.demo.eureka包,並在該包下新建啓動類EurekaApplication

package com.demo.eureka;

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

/**
* 註冊中心微服務
*/
// 標註爲一個spring boot項目 @SpringBootApplication
// 標註爲Eureka服務端 @EnableEurekaServer
public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }

4.右鍵EurekaApplication,點Run 'EurekaApplication'啓動該demo-eureka項目

在瀏覽器輸入localhost:7000,看到如上界面,表示項目啓動成功。No instances available表示暫時尚未服務註冊到eureka上來。spring

 

docker微服務部署之:2、搭建文章微服務項目sql

相關文章
相關標籤/搜索