springcloudspringboot

springcloud2.x gitlab consul集成配置

2019-05-23  本文已影响0人  一个忙来无聊的人

一、踩坑篇

下面这个异常卡了我两天 有句MMP不知道当不当将 idea 这点感觉好坑

IllegalStateException:You need to configure a uri for the git repository.

完整异常如下

16:45:08.363 [main] INFO  org.apache.catalina.core.StandardService - Starting service [Tomcat]
16:45:08.363 [main] INFO  org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.14]
16:45:08.368 [main] INFO  org.apache.catalina.core.AprLifecycleListener - The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Program Files\Java\jdk1.8.0_202\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;D:\Program Files\Java\jdk1.8.0_202\bin;D:\Program Files\Java\jdk1.8.0_202\jre\bin;D:\Program Files\maven\apache-maven-3.6.0\bin;d:\Program Files\Git\cmd;D:\Program Files\cli\spring-2.1.4.RELEASE\bin;D:\Program Files\nodejs\;D:\Program Files (x86)\GitExtensions\;C:\Users\DELL\AppData\Local\Microsoft\WindowsApps;C:\Users\DELL\AppData\Roaming\npm;.]
16:45:08.467 [main] INFO  org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
16:45:08.468 [main] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1274 ms
16:45:08.547 [main] WARN  com.netflix.config.sources.URLConfigurationSource - No URLs will be polled as dynamic configuration sources.
16:45:08.548 [main] INFO  com.netflix.config.sources.URLConfigurationSource - To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
16:45:08.558 [main] INFO  com.netflix.config.DynamicPropertyFactory - DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@7cfac39f
16:45:08.839 [main] ERROR org.springframework.boot.web.embedded.tomcat.TomcatStarter - Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthIndicatorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicatorRegistry]: Factory method 'healthIndicatorRegistry' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerHealthIndicator' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'configServerHealthIndicator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.config.server.config.CompositeConfiguration': Unsatisfied dependency expressed through method 'setEnvironmentRepos' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/DefaultRepositoryConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: You need to configure a uri for the git repository.
16:45:08.854 [main] INFO  org.apache.catalina.core.StandardService - Stopping service [Tomcat]
16:45:08.859 [main] WARN  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
16:45:08.870 [main] INFO  org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener - 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
16:45:08.876 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Invalid config server configuration.

Action:

If you are using the git profile, you need to set a Git URI in your configuration.  If you are using a native profile and have spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

异常复现: 配置文件新增 search-paths
spring.cloud.config.server.git.search-paths= '{application}'
或者新增 /{application}
spring.cloud.config.server.git.uri=http://192.168.1.1:6060/root/config.git/{application}

不增加 程序启动OK
添加 程序启动错误
删除 程序启动错误
解决办法: pom文件中删除 config jar包引入,build下,重新导入(一定要在程序停止的时候操作啊)

如果有大佬阅读到这儿,有其他解决办法欢迎留言,感激不尽

言归正传,下面从0-1搭建配置中心服务器server和客户端。

二、搭建服务端server

创建springboot项目 添加依赖

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


    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

启动类 application

package com.zm.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {

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

}

2.3 application.properties 配置信息

#srpingboot 集成配置中心
server.port= 8700
spring.application.name=config-server
#配置git地址信息
#spring.cloud.config.enabled=true
spring.cloud.config.server.git.uri=http://192.168.1.1:8080/root/config.git
#配置用户名密码
spring.cloud.config.server.git.username=root
spring.cloud.config.server.git.password=root
# 不对服务器SSL 证书进行验证
spring.cloud.config.server.git.skip-ssl-validation=true
#设置超时时间是4秒
spring.cloud.config.server.git.timeout=4
#占位符支持查找指定路径文件
spring.cloud.config.server.git.search-paths= '{application}'
spring.cloud.config.label=master
#设置扫描路径--使用应用名作为搜索路径
#spring.cloud.config.server.git.search-paths='{application}'
# 2.X 与1.X区别
management.endpoints.web.exposure.include="*"
#强制获取覆盖
spring.cloud.config.server.git.multiple-pull=true
#删除未跟踪的分支
spring.cloud.config.server.git.delete-untracked-branches=true

yml配置文件 里面集成了consul 若不需要 去掉即可

##scb-config####
server:
  port: 8820
spring:
  application:
    name: config-server #配置服务名称
  cloud:
    #配置consul
    consul:
      host: localhost
      port: 8500
      discovery:
        hostname: 127.0.0.1
#健康检查
        healthCheckPath: /health
        healthCheckInterval: 15s
        instance-id: id-config-server
    # 配置中心git仓库
    config:
      enabled: true
      server:
        git:
          #配置git仓库的地址
          uri: http://192.168.1.1:8080/root/config.git
          username: root
          password: root
          # 强制获取覆盖
          force-pull: true
          # 不对服务器SSL 证书进行验证
          skip-ssl-validation: true
          #设置超时时间是4秒
          timeout: 4
          # 删除存储库中未跟踪的分支
          delete-untracked-branches: true
management:
  endpoints:
    web:
      exposure:
        include: "*"

服务搭建好之后可以直接通过浏览器访问测试是否成功
http://localhost:8820/eduweb-test.yml

三、搭建客户端server

添加maven依赖
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.1.2.RELEASE</version>
 </dependency>

客户端配置文件 注意 一定是 bootstrap.yml

spring:
  application:
    #本机服务名称  对接配置中心 和git里的文件名对应 如使用 master下配置文件 eduweb-test.yml 
    name: eduweb
  cloud:
    config:
#配置客户端的ip地址和端口号
      uri: http://localhost:8820/
      enabled: true
    #多个环境表明使用后缀 一般分为 dev开发,test测试,prod生产三个路径
      profile: test
      label: master
image.png

四 、关于gitlab项目路径

若 配置文件直接放在master根目录下 server 根据
spring.application.name 配置值 与 spring.cloud.config.profile 共同确认文件信息

如下命名为 eduweb-test.yml

image.png

若 配置文件直接放在master分支sysadmin文件下下 server 根据
spring.cloud.config..name 查找文件夹,在根据 spring.cloud.config.profile 确定使用哪个文件


image.png

写在最后 过程很辛苦,结局很满意
github demo地址: https://github.com/gxiaobin/consul-config

上一篇 下一篇

猜你喜欢

热点阅读