(三)SpringBoot整合Apollo
一、将Apollo的源码依赖打入到本地仓库
1、解压apollo-master.zip,进入apollo-master\scripts目录
2、点击build.bat文件就能打入依赖(要配置好maven的环境,他会自动的找到你maven的本
地仓库)
二、创建一个SpringBoot项目
1、创建项目springboot-apollo
2、导入依赖信息
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.0.1.RELEASE</version> 5 <relativePath /> <!-- lookup parent from repository --> 6 </parent> 7 8 <properties> 9 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 10 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 11 <java.version>1.8</java.version> 12 <spring-cloud.version>Finchley.RC1</spring-cloud.version> 13 </properties> 14 15 <groupId>org.ybl.test</groupId> 16 <artifactId>springboot-apollo</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 <name>springboot-apollo</name> 19 <description>Demo project for Spring Boot</description> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-web</artifactId> 33 <!-- <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> 34 <artifactId>jackson-databind</artifactId> </exclusion> </exclusions> --> 35 </dependency> 36 37 <!-- apollo 携程apollo配置中心框架 --> 38 <dependency> 39 <groupId>com.ctrip.framework.apollo</groupId> 40 <artifactId>apollo-client</artifactId> 41 <version>1.0.0</version> 42 </dependency> 43 <dependency> 44 <groupId>com.ctrip.framework.apollo</groupId> 45 <artifactId>apollo-core</artifactId> 46 <version>1.0.0</version> 47 </dependency> 48 49 50 <dependency> 51 <groupId>org.projectlombok</groupId> 52 <artifactId>lombok</artifactId> 53 <optional>true</optional> 54 </dependency> 55 <dependency> 56 <groupId>com.alibaba</groupId> 57 <artifactId>fastjson</artifactId> 58 <version>1.2.3</version> 59 </dependency> 60 <dependency> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-starter-test</artifactId> 63 <scope>test</scope> 64 </dependency> 65 </dependencies> 66 <dependencyManagement> 67 <dependencies> 68 <dependency> 69 <groupId>org.springframework.cloud</groupId> 70 <artifactId>spring-cloud-dependencies</artifactId> 71 <version>${spring-cloud.version}</version> 72 <type>pom</type> 73 <scope>import</scope> 74 </dependency> 75 </dependencies> 76 </dependencyManagement> 77 78 <build> 79 <plugins> 80 <plugin> 81 <groupId>org.springframework.boot</groupId> 82 <artifactId>spring-boot-maven-plugin</artifactId> 83 </plugin> 84 <plugin> 85 <groupId>org.apache.maven.plugins</groupId> 86 <artifactId>maven-compiler-plugin</artifactId> 87 <configuration> 88 <source>1.8</source> 89 <target>1.8</target> 90 </configuration> 91 </plugin> 92 <plugin> 93 <groupId>org.apache.maven.plugins</groupId> 94 <artifactId>maven-resources-plugin</artifactId> 95 <version>3.0.1</version> 96 <executions> 97 <execution> 98 <id>copy-conf</id> 99 <phase>package</phase> 100 <goals> 101 <goal>copy-resources</goal> 102 </goals> 103 <configuration> 104 <encoding>UTF-8</encoding> 105 <outputDirectory>${project.build.directory}/ext/conf</outputDirectory> 106 <resources> 107 <resource> 108 <directory>ext/conf</directory> 109 <includes> 110 <include>logback.xml</include> 111 </includes> 112 <filtering>true</filtering> 113 </resource> 114 </resources> 115 </configuration> 116 </execution> 117 </executions> 118 </plugin> 119 <plugin> 120 <groupId>org.jacoco</groupId> 121 <artifactId>jacoco-maven-plugin</artifactId> 122 <version>0.7.5.201505241946</version> 123 <executions> 124 <execution> 125 <id>default-prepare-agent</id> 126 <goals> 127 <goal>prepare-agent</goal> 128 </goals> 129 </execution> 130 <execution> 131 <id>default-prepare-agent-integration</id> 132 <goals> 133 <goal>prepare-agent-integration</goal> 134 </goals> 135 </execution> 136 </executions> 137 </plugin> 138 <plugin> 139 <groupId>com.spotify</groupId> 140 <artifactId>docker-maven-plugin</artifactId> 141 <version>0.4.3</version> 142 <configuration> 143 <imageName>hy_uav_gateway</imageName> 144 <dockerDirectory>src/main/docker</dockerDirectory> 145 <resources> 146 <resource> 147 <targetPath>/</targetPath> 148 <directory>${project.build.directory}</directory> 149 <include>${project.build.finalName}.jar</include> 150 <include>ext/conf/logback.xml</include> 151 </resource> 152 </resources> 153 </configuration> 154 </plugin> 155 </plugins> 156 </build> 157 158 <repositories> 159 <repository> 160 <id>spring-milestones</id> 161 <name>Spring Milestones</name> 162 <url>https://repo.spring.io/milestone</url> 163 <snapshots> 164 <enabled>false</enabled> 165 </snapshots> 166 </repository> 167 </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,到此为止,成功搭建好了!