1.创建一个maven主工程,在其pom文件中引入依赖,spring boot 版本为2.0.x以上,spring cloud版本为Greenwich.SR。这个pom文件作为父pom文件,起到依赖版本控制的作用,其它module工程继承该<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.holley</groupId> <artifactId>springdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<modules>
<module>eureka-server</module>
<module>service-hi</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- dependencies与dependencyManagement的区别 主项目dependencies下的依赖,即使子项目不写该依赖项,子项目仍会全部继承,而主项目dependencyManagement下的依赖则需要子项目显示引入该依赖 为了项目的正常运行,必须让所有子模块的依赖项使用统一的版本号。
比如在此项目中,有一个子模块eureka-server, 在主项目中设置dependencyManagement元素,通过它管理eureka-server的依赖版本,在eureka-server中声明该元素下的依赖时可以不标注版本号, maven工程会向上级查询该依赖的版本号。
而如果eureka-server依赖中声明了版本号,则eureka-server会使用该声明的版本号 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type> <scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建两个model工程,一个model工程为服务注册中心,即Eureka Server,另一个作为Eureka Client
注册中心eureka server
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.holley</groupId> <artifactId>springdemo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.holley</groupId> <artifactId>eurkea-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
eureka-server的yml配置文件(注意:1.application.yml采用的注释符号是#而不是//。2.缩进必须使用空格,而不是tab键。3.冒号后面必须有空格。)
server: port: 8761 eureka: instance: hostname: localhost client: #通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application: name: eureka-server
eureka-server入口类的注解:表示启动一个服务注册中心
@SpringBootApplication @EnableEurekaServer public class EurkaServerApplication { public static void main(String[] args) { SpringApplication.run(EurkaServerApplication.class, args); } }
配置成功之后启动eureka-server项目,访问localhost:8761会出现如下界面
创建eureka-client(服务提供者)
pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.holley</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.holley</groupId> <artifactId>springdemo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml文件(需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name )
server: port: 8762 spring: application: name: eureka-client eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
子模块入口
package com.holley.eurekaclient; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @SpringBootApplication @EnableEurekaClient
@RestController
public class EurekaClientApplication {
// 通过注解@EnableEurekaClient 表明自己是一个eurekaclient. public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam(value = "name",defaultValue = "holley")String name){ return "hi,"+name+",i come from port:"+port; } }
访问地址:http://localhost:8762/hi?name=
负载均衡ribbon+restTemplate和feign(启动eureka-client多个实例,只改变端口值,此时就相当于一个小的集群,然后使用ribbon或者feign(feign默认集成了ribbon)访问eureka-client的接口,会随机访问eureka-client实例)
首先创建一个springboot的工程,取名为service-ribon,其pom文件内容如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.holley</groupId> <artifactId>ribbondemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ribbondemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件如下
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: name: service-ribbon
在启动类中,通过EnableDiscoveryClient向服务注册中心注册,并且向程序的ioc注入一个bean:restTemplate;并且通过@LoadBalances注解表明这个restTemplate开启负载均衡的功能
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class RibbondemoApplication { public static void main(String[] args) { SpringApplication.run(RibbondemoApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } }
控制层和普通的springboot的控制层一样,service层需要调用服务的接口
@Service public class HelloService { @Autowired RestTemplate restTemplate; public String hiService(String name){ // 通过服务名称和uri进行调用 return restTemplate.getForObject("http://eureka-client/hi?name="+name,String.class); } }
也可以直接在主项目下创建一个module项目作为ribbon,此时只有pom文件和单独的工程项目不同,其他代码一致
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.holley</groupId> <artifactId>springdemo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.holley</groupId> <artifactId>ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ribbon</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
使用feign实现负载均衡(如果依赖报错,注意版本号是否正确,或者springboot的版本与springcloud的版本是否兼容)
创建一个新的springboot子模块,取名为service-feign,其pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.holley</groupId> <artifactId>springdemo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.holley</groupId> <artifactId>service-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-feign</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project>
配置参数如下:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8765 spring: application: name: service-feign
启动类上需要添加@EnableFeignClients注解开启Feign的功能
@SpringBootApplication @EnableEurekaClient @EnableFeignClients @EnableDiscoveryClient public class ServiceFeignApplication { public static void main(String[] args) { SpringApplication.run(ServiceFeignApplication.class, args); } }
定义一个feign接口,通过@FeignClient("服务名")来指定调用哪个服务
@FeignClient(value = "eureka-client") @Component public interface Hiservice { @RequestMapping(value = "/hi",method = RequestMethod.GET) String hi(@RequestParam(value = "name") String name); }
在web的controller层,对外暴露一个“/hi”的api接口,通过上面定义的接口来调用服务
@RestController public class HiController { @Autowired Hiservice hiservice; @GetMapping(value = "/hi") public String sayHi(@RequestParam String name) { return hiservice.hi( name ); } }
启动程序,多次访问http://localhost:8765/hi?name=forezp,浏览器交替显示:
断路器(如果较底层的服务出现故障,当对此服务的调用的不可用达到一个阈值(如:Hystric是5秒20次)断路器将会被打开)
在之前的ribbon模块的pom文件中添加如下依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
在启动类中添加注解@EnableHystrix注解开启Hystrix
在HelloService类中添加如下方法
@HystrixCommand(fallbackMethod = "error") public String hystrixTextService(String name){ return restTemplate.getForObject("http://eureka-client/test?name="+name,String.class); } /** * @Author holley * @Description 断路器回调函数 * @Date 2019/9/16 17:40 * @Param [name] * @return java.lang.String */ public String error(String name){ return "hi,"+name+",sorry,error!"; }
在controller层添加调用接口
@RequestMapping(value = "/test",method = RequestMethod.GET) public String hystrixTest(@RequestParam("name")String name){ return helloService.hystrixTextService(name); }
如果此时http://eureka-client/test?name=zhangsan不可访问,则结果如下:
feign中是自带断路器的,但是在D版本之后默认是关闭,需要在配置文件中打开
feign: hystrix: enabled: true
在service层,只需要在FeignClient注解中添加fallback的指定类即可
@FeignClient(value = "eureka-client",fallback = FeignHystrixTest.class) @Component public interface Hiservice { @RequestMapping(value = "/hi",method = RequestMethod.GET) String hi(@RequestParam(value = "name") String name); }
同时新建的fallback指定类必须要实现Hiservice接口,并注入到ioc容器中,代码如下:
@Component public class FeignHystrixTest implements Hiservice{ @Override public String hi(String name) { return "对不起,服务不可用"; } }
zuul的主要功能是路由转发和过滤器
其pom文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.holley</groupId> <artifactId>springdemo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.holley</groupId> <artifactId>zuuldemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>zuuldemo</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies> </project>
启动类上需要添加@EnableZuulProxy注解,开启zuul功能
@SpringBootApplication @EnableEurekaClient @EnableZuulProxy @EnableDiscoveryClient public class ZuuldemoApplication { public static void main(String[] args) { SpringApplication.run(ZuuldemoApplication.class, args); } }
配置文件如下:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8769 spring: application: name: service-zuul
#以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务; zuul: routes: api-a: path: /api-a/** serviceId: service-ribbon api-b: path: /api-b/** serviceId: service-feign
zuul还能进行过滤,做一些安全验证
package com.holley.servicezuul.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author Holley * @Description 请输入一句话进行描述 * @create 2019-09-17 16:55 **/ @Component public class MyFilter extends ZuulFilter{ private static Logger log = LoggerFactory.getLogger(MyFilter.class); /** filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型 * pre:路由之前 * routing:路由之时 * post:路由之后 * error:发送错误调用 */ @Override public String filterType() { return "pre"; } /** * 过滤的顺序 */ @Override public int filterOrder() { return 0; } /** * 这里可写逻辑判断,判断是否过滤,true表示过滤,false表示不过滤 */ @Override public boolean shouldFilter() { return true; } /** * 过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问 */ @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s >>> %s",request.getMethod(),request.getRequestURL().toString())); Object accessToken = request.getParameter("token"); if(accessToken == null){ log.warn("token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try{ ctx.getResponse().getWriter().write("token is empty"); }catch (Exception e){ e.printStackTrace(); } return null; } log.info("ok"); return null; } }
此时访问http://localhost:8769/api-b/hi?name=,token为空时会直接返回错误信息
spring cliud config(分布式配置中心组件) 方便服务配置文件统一管理,实时更新。在spring cloud config组件中,分为两个角色,一个是config server,一个是config client
新建一个主工程,pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.holley</groupId> <artifactId>springcloudconfig</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloudconfig</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <modules> <module>config-server</module> <module>config-client</module> </modules> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
config server
pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.holley</groupId> <artifactId>springcloudconfig</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.holley</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-server</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件(application.properties)如下
spring.application.name=config-server #默认是git,也可以使用本地配置,或者subversion(svn),native(本地) spring.application.profiles.active=git server.port=8888 #配置git仓库地址(最后不需要带/,否则会出现:No custom http config found for URL: XXX) spring.cloud.config.server.git.uri=https://github.com/zhanghouli/hello-world.git #git仓库地址下的相对搜索地址(可用使用通配符),可以配置多个,用,分割。可以{application}实现按应用查配置 spring.cloud.config.server.git.search-paths=application #配置仓库的分支 spring.cloud.config.label=master #访问git仓库的用户名(如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写) spring.cloud.config.server.git.username= #访问git仓库的用户密码 spring.cloud.config.server.git.password=
此外需要在启动类上添加@EnableConfigServer注解
/{application}/{profile}[/{label}]
[/{label}]/{application}-{profile}{.yml|.properties|.json}
规则简单说明:{application}=配置消费方应用名称(即:config client的项目名,通俗讲:就是谁用这个配置就是谁的名字),{profile}=配置环境(如:dev开发环境,test测试环境,prod生产环境),{label}=仓库分支名(git或svn方式指定,native本地方式无需指定),.yml|.properties|.json表示指定的响应返回格式,{}表示必需,[]表示可选,|表示或的关系,例如本例中访问:http://localhost:8888/config-server/prod
config client
pom文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.holley</groupId> <artifactId>springcloudconfig</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.holley</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-client</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件(bootstrap.properties)如下
spring.application.name=config-client #指明远程仓库的分支 spring.cloud.config.label=master #dev开发环境 test测试环境 pro正式环境 spring.cloud.config.profile=dev #指明配置服务中心的网址 spring.cloud.config.uri=http://localhost:8888/ server.port=8881
在启动类写一个接口,读取配置文件中holley变量的值
@SpringBootApplication @RestController public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${holley}") String holley; /** * 返回从配置中心读取的holley变量的值 * @return */ @RequestMapping(value = "/hi") public String hi(){ return holley; } }