(三)SpringBoot整合Apollo

2020-12-13 04:21

阅读:324

标签:inf   put   build   vat   local   本地   --   图片   hang   

一、将Apollo的源码依赖打入到本地仓库   

  1、解压apollo-master.zip,进入apollo-master\scripts目录

  2、点击build.bat文件就能打入依赖(要配置好maven的环境,他会自动的找到你maven的本

地仓库)

二、创建一个SpringBoot项目

  1、创建项目springboot-apollo

  2、导入依赖信息

parent>
    groupId>org.springframework.bootgroupId>
    artifactId>spring-boot-starter-parentartifactId>
    version>2.0.1.RELEASEversion>
    relativePath /> 
parent>

properties>
    project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    java.version>1.8java.version>
    spring-cloud.version>Finchley.RC1spring-cloud.version>
properties>

groupId>org.ybl.testgroupId>
artifactId>springboot-apolloartifactId>
version>0.0.1-SNAPSHOTversion>
name>springboot-apolloname>
description>Demo project for Spring Bootdescription>

dependencies>
    dependency>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starterartifactId>
    dependency>
    dependency>
        groupId>org.springframework.cloudgroupId>
        artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    dependency>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-webartifactId>
        
    dependency>

    
    dependency>
        groupId>com.ctrip.framework.apollogroupId>
        artifactId>apollo-clientartifactId>
        version>1.0.0version>
    dependency>
    dependency>
        groupId>com.ctrip.framework.apollogroupId>
        artifactId>apollo-coreartifactId>
        version>1.0.0version>
    dependency>


    dependency>
        groupId>org.projectlombokgroupId>
        artifactId>lombokartifactId>
        optional>trueoptional>
    dependency>
    dependency>
        groupId>com.alibabagroupId>
        artifactId>fastjsonartifactId>
        version>1.2.3version>
    dependency>
    dependency>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-testartifactId>
        scope>testscope>
    dependency>
dependencies>
dependencyManagement>
    dependencies>
        dependency>
            groupId>org.springframework.cloudgroupId>
            artifactId>spring-cloud-dependenciesartifactId>
            version>${spring-cloud.version}version>
            type>pomtype>
            scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

build>
plugins>
plugin>
    groupId>org.springframework.bootgroupId>
    artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugin>
    groupId>org.apache.maven.pluginsgroupId>
    artifactId>maven-compiler-pluginartifactId>
    configuration>
        source>1.8source>
        target>1.8target>
    configuration>
plugin>
plugin>
    groupId>org.apache.maven.pluginsgroupId>
    artifactId>maven-resources-pluginartifactId>
    version>3.0.1version>
    executions>
        execution>
            id>copy-confid>
            phase>packagephase>
            goals>
                goal>copy-resourcesgoal>
            goals>
            configuration>
                encoding>UTF-8encoding>
                outputDirectory>${project.build.directory}/ext/confoutputDirectory>
                resources>
                    resource>
                        directory>ext/confdirectory>
                        includes>
                            include>logback.xmlinclude>
                        includes>
                        filtering>truefiltering>
                    resource>
                resources>
            configuration>
        execution>
    executions>
plugin>
plugin>
    groupId>org.jacocogroupId>
    artifactId>jacoco-maven-pluginartifactId>
    version>0.7.5.201505241946version>
    executions>
        execution>
            id>default-prepare-agentid>
            goals>
                goal>prepare-agentgoal>
            goals>
        execution>
        execution>
            id>default-prepare-agent-integrationid>
            goals>
                goal>prepare-agent-integrationgoal>
            goals>
        execution>
    executions>
plugin>
plugin>
    groupId>com.spotifygroupId>
    artifactId>docker-maven-pluginartifactId>
    version>0.4.3version>
    configuration>
        imageName>hy_uav_gatewayimageName>
        dockerDirectory>src/main/dockerdockerDirectory>
        resources>
            resource>
                targetPath>/targetPath>
                directory>${project.build.directory}directory>
                include>${project.build.finalName}.jarinclude>
                include>ext/conf/logback.xmlinclude>
            resource>
        resources>
    configuration>
plugin>
plugins>
build>

repositories>
    repository>
        id>spring-milestonesid>
        name>Spring Milestonesname>
        url>https://repo.spring.io/milestoneurl>
        snapshots>
            enabled>falseenabled>
        snapshots>
    repository>
repositories>

  3、添加配置文件

  application.yml

server:
  port: 8001
spring:
  application:
    name: springboot-apollo
eureka:
  client:
    service-url:
      defaultZone: http://192.168.159.128:8080/eureka  

  eureka:client:service-url:defaultZone后面的地址是Apollo启动后的地址

技术图片

  apollo-env.properties:

local.meta=http://192.168.159.128:8080
dev.meta=http://192.168.159.128:8080
fat.meta=${fat_meta}
uat.meta=${uat_meta}
lpt.meta=${lpt_meta}
pro.meta=${pro_meta}

  在resources目录下新建一个META-INF文件夹,在这个文件夹下新建一个app.properties   

app.id=test_apollo

app.id的名字要和Apollo中的AppId名字一样

 技术图片

三、修改环境

修改/opt/settings/server.properties(Mac/Linux)或C:\opt\settings\server.properties(Windows)文件,设置env为DEV:

这里我们是在windows环境下运行,所以在windows目录下创建

 技术图片

 

技术图片

四、测试

修改主启动类,在主启动类上添加@EnableApolloConfig注解

@EnableApolloConfig
@SpringBootApplication
public class SpringbootApolloApplication {

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

}

新建IndexController.java文件

 

@RestController
public class IndexController {
    @Value("${zhangsan:test}")
    private String zhangsan;

    @RequestMapping("/getZhangsan")
    public String getZhangsan(){
        return zhangsan;
    }
}

在Apollo中新建zhangsan的配置文件

 技术图片

点击发布:

启动springboot项目:

访问http://127.0.0.1:8001/getZhangsan

 技术图片

成功访问!!

修改value的值为“wangmazi”后,点击发布

 技术图片

OK,到此为止,成功搭建好了!

 

  

 

(三)SpringBoot整合Apollo

标签:inf   put   build   vat   local   本地   --   图片   hang   

原文地址:https://www.cnblogs.com/lindev/p/11108558.html


评论


亲,登录后才可以留言!