1. 程式人生 > >SpringCloud環境搭建--configServer

SpringCloud環境搭建--configServer

配置中心

搭建過程

  • 1.引入依賴:
<dependencies>
        <!--配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- Eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    1. 修改啟動類。新增@EnableConfigServer,@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class DorConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(DorConfigApplication.class, args);
    }
}
    1. 修改配置檔案,application.yml
spring:
  application:
    name: doraemon-configServer
  profiles:
    active: local
  cloud:
    config:
      server:
        git:
          # 遠端倉庫地址(需要新建一個倉庫,使用專案的倉庫也是可以的,但是.......)
          uri: https://gitee.com/fangjiaxiaobai/doraemon-config.git
          # 遠端倉庫的查詢地址
          search-paths: config
      #    username:
      #    password:
      #    遠端倉庫的分支
      label: local
server:
  port: ${config_port:8770}
eureka:
  client:
    service-url:
      defaultZone: http://${eureka_service_url:eureka-peer1:8761}/eureka/

4.然後啟動,檢視Eureka頁面:
config註冊到了Eureka上

高可用的配置中心

1.複製兩份application.yml,分別命名為:application-prod1.yml,application-prod2.yml
application-prod1.yml

spring:
  application:
    name: doraemon-configServer
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/fangjiaxiaobai/doraemon-config.git
          search-paths: config
      label: prod
server:
  port: ${config_port:8771}
eureka:
  client:
    service-url:
      defaultZone: http://${eureka_service_url:eureka-peer1:8761}/eureka/

application-prod2.yml:

spring:
  application:
    name: doraemon-configServer
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/fangjiaxiaobai/doraemon-config.git
          search-paths: config
      label: prod
      discovery:
        service-id: doraemon-configServer
server:
  port: ${config_port:8772}
eureka:
  client:
    service-url:
      defaultZone: http://${eureka_service_url:eureka-peer1:8761}/eureka/

然後分別啟動兩個例項:

java -jar dor-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod1
java -jar dor-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod2

然後,檢視eureka。就會有兩個config例項。

整合Bus,實現動態重新整理叢集配置

在上面的是基礎上,修改pom.xml,新增依賴

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

也可以使用kafka實現,那樣的新增的依賴就是:

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

修改application-prod1.yml和application-prod2.yml新增rabbitMq(Kafka)的配置:

spring:
  rabbitmq:
    host: ${MQ_IP:10.127.138.75}
    port: ${MQ_PORT:5672}
    username: ${MQ_USERNAME:guest}
    password: ${MQ_PASSWORD:guest}

重新啟動即可。

docker整合

修改pom.xml

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!--指定映象的名稱-->
                    <imageName>${project.artifactId}:${project.version}</imageName>
                    <!--<baseImage>java</baseImage>-->
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <forceTags>true</forceTags>
                    <!--<entryPoint>["java","-Djava.security.egd=file:/dev/./urandom", "-jar",-->
                    <!--"/${project.build.finalName}.jar"]-->
                    <!--</entryPoint>-->
                    <!--那些需要和Dockerfile放在一起,在構建映象時使用的檔案,一般應用 jar 包需要納入-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <!--用於指定需要複製的根目錄,${project.build.directory}表示target目錄-->
                            <directory>${project.build.directory}</directory>
                            <!--用於指定需要複製的檔案,${project.build.finalName}.jar指的是打包後的jar包檔案。-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

在src/main/docker下新建Dockerfile:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD dor-config-0.0.1-SNAPSHOT.jar app.jar
RUN chmod 755 /app.jar
EXPOSE 8771
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"

這個還是同上一篇Eureka的搭建一樣,可以編譯映象的過程放到pom中。