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

SpringCloud-config(配置版config的eureka服务器)

程序员文章站 2022-03-16 18:52:10
...

                        配置版config的服务端和客户端

注明:此项目为本人学习尚硅谷老师的教学视频然后整理核心的配置文件,所有的项目均在以下地址下载。
https://github.com/xwbGithub/microservicecloud下载

讲解内容请参考:microservicecloud-config-eureka-client-7001,microservicecloud-config-dept-client-8001

新建模块:microservicecloud-config-eureka-client-7001

config服务器端

pom

<dependencies>
    <!-- SpringCloudConfig配置 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    <!-- 热部署插件 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

bootstrap.xml

spring:
  cloud:
    config:
      name: microservicecloud-config-eureka-client #需要从gitHub上读取的资源名称,注意没有yml后缀名
      profile: dev #对应配置服务中的{profile}
      label: master #对应的是访问那个分支
      uri: http://config-3344.com:3344 #SpringCloud获取服务地址

application.yml

spring:
  application:
    name: microservicecloud-config-eureka-client

主启动类Config_Git_EurekaServerApplication

 

/** EurekaServer服务器端启动类,接受其他微服务注册景来*/
@SpringBootApplication
@EnableEurekaServer
public class Config_Git_EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
    }
}

先启动microservicecloud-config-3344微服务,再启动microservicecloud-config-eureka-client-7001微服务,然后访问地址:http://eureka7001.com:7001/

新建config的eureka的客户端

新建模块microservicecloud-config-dept-client-8001

pom

<dependencies>
		<!-- SpringCloudConfig配置 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>com.atguigu.springcloud</groupId>
			<artifactId>microservicecloud-api</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

Bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名
      #profile配置是什么就取什么配置dev or test
      profile: dev
      #profile: test
      label: master
      uri: http://config-3344.com:3344  #SpringCloudConfig获取的服务地址

application.yml

spring:
  application:
    name: microservicecloud-config-dept-client

主启动类

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient //服务发现
public class DeptProvider8001_Config_App
{
   public static void main(String[] args)
   {
      SpringApplication.run(DeptProvider8001_Config_App.class, args);
   }
}

其他业务逻辑代码

SpringCloud-config(配置版config的eureka服务器)

测试:

首先启动config-3344配置服务器启动,然后启动micriservicecloud-config-eureka—client-7001服务端

最后启动microservicecloud-config-dept-client-8001客户端

原配置文件地址

https://github.com/zzyybs/microservicecloud-config/blob/master/microservicecloud-config-dept-client.yml

Test配置默认访问

                            http://localhost:8001/dept/list 看到访问的数据库是cloudDB02

SpringCloud-config(配置版config的eureka服务器)

dev配置默认访问

                            http://localhost:8001/dept/list 看到访问的数据库是cloudDB01

SpringCloud-config(配置版config的eureka服务器)

 

相关标签: SpringCloud config