SpringCloud学习之旅15--微服务集中化配置

由于众多的微服务,所以产生了管理的问题。例如启动的参数等配置。此时就可以放置到远程仓库上面,便于修改和管理。


先看服务端:

build.gradle文件:

// buildscript 代码块中脚本优先执行
buildscript {


// ext 用于定义动态属性
ext {
springBootVersion = '2.0.0.M3'
}


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
// mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


// 依赖关系
dependencies {


// classpath 声明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}


// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


// 指定了生成的编译文件的版本,默认是打成了 jar 包
group = 'com.waylau.spring.cloud'
version = '1.0.0'


// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
//mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


ext {
springCloudVersion = 'Finchley.M2'
}


// 依赖关系
dependencies {


// Eureka Client
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')

// Spring Cloud Config Server
compile('org.springframework.cloud:spring-cloud-config-server')


// 该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')
}


dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}

}



application.properties文件:

spring.application.name: micro-weather-config-server
server.port= 8888


eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/


#配置github远程仓库
spring.cloud.config.server.git.uri=https://github.com/ChenXbFrank/MySpringCloudLearn
#该仓库里面的子文件夹

spring.cloud.config.server.git.searchPaths=config-repo



package com.waylau.spring.cloud.weather.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Hello Controller.
 * 
 * @since 1.0.0 2017年11月20日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@RestController
public class HelloController {

//@RequestMapping("/hello")
@GetMapping("/hello")
public String hello() {
return "Hello World!";
}

}



package com.waylau.spring.cloud.weather;


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;


@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class Application {


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

}





客户端:

build.gradle文件:

// buildscript 代码块中脚本优先执行
buildscript {


// ext 用于定义动态属性
ext {
springBootVersion = '2.0.0.M3'
}


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
// mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


// 依赖关系
dependencies {


// classpath 声明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}


// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


// 指定了生成的编译文件的版本,默认是打成了 jar 包
group = 'com.waylau.spring.cloud'
version = '1.0.0'


// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
//mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


ext {
springCloudVersion = 'Finchley.M2'
}


// 依赖关系
dependencies {


// Eureka Client
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')


// Config Client
compile('org.springframework.cloud:spring-cloud-config-client')


// 该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')
}


dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}

}



application.properties文件:


spring.application.name: micro-weather-config-client


eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/


#指定开发环境
spring.cloud.config.profile=dev
#configserver的位置

spring.cloud.config.uri=http://localhost:8888/





package com.waylau.spring.cloud.weather.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Hello Controller.
 * 
 * @since 1.0.0 2017年11月20日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@RestController
public class HelloController {

//这里就是配置中心的配置的值
@Value("${auther}")
private String auther;

//@RequestMapping("/hello")
@GetMapping("/hello")
public String hello() {
return "Hello World! "+auther;
}

}


package com.waylau.spring.cloud.weather;


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


@SpringBootApplication
@EnableDiscoveryClient
public class Application {


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

}



最后两个项目启动:都是基于eureka服务的


访问链接:

http://localhost:8080/hello      

后面的chenxb.com就是在远程仓库取的值。






猜你喜欢

转载自blog.csdn.net/qq_33371766/article/details/80662605
今日推荐