Spring Cloud Spring Boot mybatis分布式微服务云架构-config-bus(十三)--二

Posted leafitit

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Spring Boot mybatis分布式微服务云架构-config-bus(十三)--二相关的知识,希望对你有一定的参考价值。

4.3、启动类ConfigServerApplication.java

package com.jacky.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 ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}

五、客户端(microservice-config-client)

5.1、pom.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>miroservice-config</artifactId>
        <groupId>com.jacky</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>microservice-config-client</artifactId>
    <packaging>jar</packaging>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-bus-amqp</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <executions>
                    <!--设置在执行maven 的install时构建镜像-->
                    <execution>
                        <id>build-image</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--安装了docker的主机,并且打开了api remote接口设置-->
                    <dockerHost>http://192.168.6.130:5678</dockerHost>
                    <pushImage>true</pushImage><!--设置上传镜像到私有仓库,需要docker设置指定私有仓库地址-->
                    <!--镜像名称-->
                    <imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
                    <!--镜像的基础版本-->
                    <baseImage>java:openjdk-8-jdk-alpine</baseImage>
                    <!--镜像启动参数-->
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
 
</project>

5.2、application.yml文件

技术分享图片
server:
  port: 8081
View Code

5.3、bootstrap.yml文件

spring:
  cloud:
    config:
      username: jacky123  #configservice认证的用户名
      password: admin123   #认证密码
      label: master   # 仓库的分支节点
      discovery:
        enabled: true
        service-id: microservice-config-service
      profile: dev   #仓库中对应文件的环境,如dev、prod、test等
      fail-fast: true
    bus:
      trace:
        enabled: true  #开启消息跟踪
  application:
    name: microservice-config-client
  rabbitmq:
    host: 192.168.6.130
    port: 5672
    username: myuser
    password: mypass
eureka:
  client:
    serviceUrl:
      defaultZone: http://jacky:[email protected]:9511/eureka
  instance:
    prefer-ip-address: true
management:
  security:
    enabled: false    #刷新时关闭安全认证

注意:

上面这些属性必须配置在bootstrap.yml,服务端的配置内容才能正确加载。因为通过bootstrap.yml的加载优先级比配置中心的服务端的高,服务端加载优先于application.yml,所以如果你把上面的配置写在application.yml中,相当于默认不是从配置中心的服务端中读取的配置信息,而是spring boot的默认加载。启动的时候就会看到加载的配置,不是配置中心得配置内容,而是默认的本地的

5.4、启动类(ConfigClientApplication.java)

package com.jacky.cloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigClientApplication.class, args);
  }
}

5.5、控制层类(ConfigClientController.java)

package com.jacky.cloud.controller;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RefreshScope
@RestController
public class ConfigClientController {
 
  @Value("${profile}")
  private String profile;
 
  @GetMapping("/profile")
  public String getProfile() {
    return this.profile;
  }
}

六、在码云创建git项目,放置配置文件

地址:https://gitee.com/jacky-lulu/microservice-config-repo

七、测试

7.1、http://localhost:9511/

技术分享图片

完整项目的源码来源 技术支持2147775633


以上是关于Spring Cloud Spring Boot mybatis分布式微服务云架构-config-bus(十三)--二的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud 对应 Spring Boot 版本

spring-boot 指标与 spring-cloud 指标

Spring Cloud Spring Boot mybatis分布式微服务云架构使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程

Spring Cloud Spring Boot mybatis分布式微服务云架构使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程

Spring Cloud 学习 Spring Boot Admin

Spring Boot与Spring Cloud技术选型版本对应关系