资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springcloud学习入门之config配置教程

前言

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的巨鹿网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

本文主要给大家分享了关于spring cloud的入门教程,主要介绍了config配置的相关内容,下面话不多说了,来一起看看看详细的介绍吧。

简介

Spring cloud config 分为两部分 server client

  • config-server 配置服务端,服务管理配置信息
  • config-client 客户端,客户端调用server端暴露接口获取配置信息

config-server

创建config-server

首先创建config-server工程.

文件结构:

├── config-server.iml
├── pom.xml
└── src
 ├── main
 │ ├── java
 │ │ └── com
 │ │ └── lkl
 │ │ └── springcloud
 │ │ └── config
 │ │  └── server
 │ │  └── Application.java
 │ └── resources
 │ ├── application.properties
 │ └── bootstrap.properties
 └── test
 └── java

pom.xml内容:

<?xml version="1.0" encoding="UTF-8"?>

 4.0.0

 
 org.springframework.boot
 spring-boot-starter-parent
 1.2.3.RELEASE
  
 

 com.lkl.springcloud
 spring-cloud-config-server
 1.0-SNAPSHOT

 
 
 
 org.springframework.cloud
 spring-cloud-config
 1.0.4.RELEASE
 pom
 import
 
 
 
 
 
 org.springframework.cloud
 spring-cloud-starter-config
 

 
 
 org.springframework.boot
 spring-boot-starter-web
 

 
 
 org.springframework.boot
 spring-boot-starter-actuator
 

 
 org.springframework.cloud
 spring-cloud-config-server
 

 

 
 
 
 org.springframework.boot
 spring-boot-maven-plugin
 
 
 

创建启动类

Application.Java

package com.lkl.springcloud.config.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liaokailin on 16/4/28.
 */
@Configuration
@EnableAutoConfiguration
@RestController
@EnableConfigServer
public class Application {

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

其中 @EnableConfigServer为关键注解

在resources文件下创建application.properties

server.port=8888

配置工程监听端口8888,默认情况下client通过读取http://localhost:8888获取配置信息

创建bootstrap.properties

spring.cloud.config.server.git.uri: https://github.com/liaokailin/config-repo

该配置信息通过fork https://github.com/spring-cloud-samples/config-repo (本地下载)得到

通过spring.cloud.config.server.Git.uri指定配置信息存储的git地址

运行config-server

spring boot工程很方便启动,运行Application.java即可

获取git上的资源信息遵循如下规则:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

application:表示应用名称,在client中通过spring.config.name配置

profile:表示获取指定环境下配置,例如开发环境、测试环境、生产环境 默认值default,实际开发中可以是 dev、test、demo、

production等

label: git标签,默认值master

如果application名称为foo,则可以采用如下方式访问:

http://localhost:8888/foo/default

http://localhost:8888/foo/development

只要是按照上面的规则配置即可访问.

config-client

创建config-client

目录结构如下:

├── pom.xml
├── spring-cloud-config-client.iml
└── src
 ├── main
 │ ├── java
 │ │ └── com
 │ │ └── lkl
 │ │ └── springcloud
 │ │ └── config
 │ │  └── client
 │ │  └── Application.java
 │ └── resources
 │ └── bootstrap.yml
 └── test
 └── java

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

 4.0.0

 com.lkl.springcloud
 spring-cloud-config-client
 1.0-SNAPSHOT


 
 org.springframework.boot
 spring-boot-starter-parent
 1.2.3.RELEASE
  
 

 
 
 
 org.springframework.cloud
 spring-cloud-starter-parent
 1.0.1.RELEASE
 pom
 import
 
 
 


 
 
 org.springframework.boot
 spring-boot-starter-web
 
 
 org.springframework.cloud
 spring-cloud-config-client
 
 
 org.springframework.boot
 spring-boot-starter-actuator
 

 
 org.springframework.boot
 spring-boot-starter-test
 test
 
 
 
 
 spring-milestones
 Spring Milestones
 http://repo.spring.io/milestone
 
 false
 
 
 
 
 
 spring-milestones
 Spring Milestones
 http://repo.spring.io/milestone
 
 false
 
 
 

 
 
 
 org.springframework.boot
 spring-boot-maven-plugin
 
 
 

创建启动类Application.java

package com.lkl.springcloud.config.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liaokailin on 16/4/28.
 */
@SpringBootApplication
@RestController
public class Application {
 @Value("${name:World!}")
 String bar;

 @RequestMapping("/")
 String hello() {
 return "Hello " + bar + "!";
 }

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

创建bootstrap.properties文件,其内容如下:

spring.application.name: foo
spring.cloud.config.env:default
spring.cloud.config.label:master
spring.cloud.config.uri:http://localhost:8888

其中 spring.application.name 为应用名称,spring.cloud.config.uri 配置config-server暴露的获取配置接口,其默认值为http://localhost:8888

第二三项配置在前面已经提到过,配置的都为默认值,因此bootstrap.properties只需要配置应用名即可.

运行config-client

访问 http://localhost 得到 `Hello liaokailin` 获取到git上的配置信息

访问 http://localhost/env 得到所有的配置信息,可以发现获取配置信息成功.

{
profiles: [ ],
configService:https://github.com/liaokailin/config-repo/foo.properties: {
name: "liaokailin",
foo: "devoxxfr"
},
configService:https://github.com/liaokailin/config-repo/application.yml: {
info.description: "Spring Cloud Samples--lkl",
info.url: "https://github.com/spring-cloud-samples",
eureka.client.serviceUrl.defaultZone: "http://localhost:8761/eureka/"
},
commandLineArgs: {
spring.output.ansi.enabled: "always"
},
servletContextInitParams: { },
...}

ok ~ it's work ! more about is here(本地下载)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对创新互联的支持。


网页题目:springcloud学习入门之config配置教程
文章网址:http://cdkjz.cn/article/gcipei.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220