欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud Config

程序员文章站 2022-07-14 22:45:14
...

Spring Cloud Config

Spring Cloud Config
pom.xml

	<!--		spring-cloud-config-server-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

application.yml


server:
  port: 6809

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:6888/eureka/

spring:
  application:
    name: spring-cloud-config-server

  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/mingshine/SpringCloud-Config/ #GitHub上面的git仓库名字
#          search-paths: SpringCloud-Config


SpringCloudConfigServerApplication.java

package com.mingshine.cloud;

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 SpringCloudConfigServerApplication {

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

}

Spring Cloud Config
pom.xml

<!--		spring-cloud-config-client-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>

application.yml


server:
  port: 6810

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:6888/eureka/

spring:
  application:
    name: spring-cloud-config-client

bootstrap.yml

spring:
  cloud:
    config:
      label: master
      uri: http://localhost:6809/  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

SpringCloudConfigClientApplication.java

package com.mingshine.cloud;

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

@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudConfigClientApplication {

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

}

ClientController.java

package com.mingshine.cloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ClientController {

    @Value("${test}")
    String test;

    @RequestMapping(value = "/test")
    public String test() {
        return test;
    }
}


相关标签: Spring Cloud