SpringCloud分布式微服务搭建(三)

本例子是一个springcloud的configserver,client例子

利用git存储各个服务的配置文件

server获取配置文件的仓库位置,并把server注册到eureka中,同时为了实现HA,多开几个server

client通过server获得git的地址,运行时利用获得的git配置文件来配置服务自身。

这样子服务的配置修改更加方便

git上的配置文件仓库地址:https://github.com/linjiaqin/springcloud-config-repo

 

本例子代码:https://github.com/linjiaqin/scdemo

一.ConfigServer端的配置 

1.引导类

package com.ljq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {

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

}

 2.配置文件

server.port=9000
spring.application.name=config-server-9000
#表示配置中心所在仓库的位置
spring.cloud.config.server.git.uri=https://github.com/linjiaqin/springcloud-config-repo.git
#仓库路径下的的相对搜索位置,可以配置多个
spring.cloud.config.server.git.search-paths=scdemo
#git的用户名
spring.cloud.config.server.git.username=×××××××
#git的密码
spring.cloud.config.server.git.password=×××××××

 3.可以直接看server获取git上配置文件的json效果

4. 为了实现高可用,开启两个configserver

 已经写入start-all.sh的一键脚本中

#开启两个config server
cd /home/linjiaqin/log_stream_platform/source/scdemo/configserver
nohup mvn spring-boot:run -Dserver.port=40001 > /dev/null 2>&1 &
nohup mvn spring-boot:run -Dserver.port=40002 > /dev/null 2>&1 &

5. 必须先开启configserver,才能开启configclient,configclient获取到server的地址之后就无所谓了

 

二.客户端

任何一个服务都可以是configclient

通过eureka获取到configserver的serverID,然后找到一个server地址去获取到git的地址,label指定了master分支,profile指定了dev配置文件

然后运行这个configclient的时候就会按照获取到的git上配置文件来运行

1.下面这个本地配置文件必须名为bootstrap

#配置服务名
spring.application.name=config-client
#服务id
server.port=50000
#配置对应文件规则中的{profile}部分
spring.cloud.config.profile=dev
#配置对应文件规则中的{label}
spring.cloud.config.label=master
#配置中心的地址,有了eureka之后不再手动配置
#spring.cloud.config.uri=http://localhost:9000/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

eureka.client.serviceUrl.defaultZone=http://mu01:8761/eureka,http://cu01:8762/eureka,http://cu02:8763/eureka

 2.Controller

package com.ljq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class HelloController {
    //value注解的作用是获得配置文件上的对应的key
    @Value("${Parameter}")
    private String Parameter;

    @Value("${server.port}")
    private String port;
    @Autowired
    private Environment environment;
    @GetMapping("/get_name")
    public String name(){
        return "Parameter:"+Parameter;
    }
    @GetMapping("/get_port")
    public String port(){
        return "Port:"+port;
    }
    @GetMapping("/get_name_env")
    public String name_env(){
        return environment.getProperty("Parameter","undefine");
    }
}

 

 

 

 

3. git上的dev配置文件

Parameter:dev
spring.application.name=config-client-by-git-dev-properties
#服务id
server.port=50001

因此启动configclient时是以这个配置文件启动的,优先级比本地的要高


 

 

posted @ 2019-04-26 23:23  linjiaqin  阅读(523)  评论(0编辑  收藏  举报