spring cloud 配置中心配置哪些东西_druid连接池配置优化

spring cloud 配置中心配置哪些东西_druid连接池配置优化访问http://localhost:8761/访问http://localhost:8761/访问http://localhost:8889/writer

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

Config加入Eureka服务治理

前面我们演示的案例是我们有一个Config Server 和一个 Config Client ,我们的Config Client直接从Config Server读取配置,这里九存在一个比较严重的耦合问题,假如我们的单一的Config Server挂掉了的IP或者端口变化了,我们Config Client将无法读取配置。这里我们也可以将Config Server作为一个普通的微服务应用,纳入Eureka的服务治理体系中。这样我们的微服务应用就可以通过配置中心的服务名来获取配置信息,这种方式比起传统的实现模式来说更加有利于维护,因为对于服务端的负载均衡配置和客户端的配置中心指定都通过服务治理机制一并解决了,既实现了高可用,也实现了自维护。

注册config-server

pom.xml

添加spring-cloud-starter-netflix-eureka-client依赖

<?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>scexample</artifactId>
        <groupId>com.pubutech</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Jetbrains全家桶1年46,售后保障稳定

application.yml

修改application.yml增加eureka注册中心信息

server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Jaysong2012/scexample     # 配置git仓库的地址
          search-paths: springcloud-config-repo                             # git仓库地址下的相对地址,可以配置多个,用,分割。
          username:                                             # git仓库的账号(私有库必填)
          password:                                             # git仓库的密码(私有库必填)
      label: master                                        #配置仓库的分支

eureka:
  client:
    service-url:
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

ConfigServerApplication.java

修改ConfigServerApplication激活注册

@SpringBootApplication(exclude = { 
   DataSourceAutoConfiguration.class})
@EnableConfigServer
//激活注册发现
@EnableDiscoveryClient
public class ConfigServerApplication { 
   

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

}

启动测试

访问http://localhost:8761/
high config

config-client发现

pom.xml

修改pom.xml增加spring-cloud-starter-netflix-eureka-client依赖

<?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>scexample</artifactId>
        <groupId>com.pubutech</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

bootstrap.yml

修改bootstrap.yml去掉spring.cloud.config.uri远程server的地址,并且添加注册中心的配置。

spring:
  cloud:
    config:
      name: springcloud-config             #对应{application}部分
      profile: pro                         #对应{profile}部分
      #uri: http://localhost:8888/ #配置中心的具体地址
      label: master                        #对应git的分支。如果配置中心使用的是本地存储,则该参数无用
      discovery:
        service-id: config-server      #指定配置中心的service-id,便于扩展为高可用配置集群。
      enabled: true #开启Config服务发现支持

eureka:
  client:
    service-url:
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

ConfigClientApplication.java

修改ConfigClientApplication.java激活注册发现

@SpringBootApplication(exclude = { 
   DataSourceAutoConfiguration.class})
//激活注册发现
@EnableDiscoveryClient
public class ConfigClientApplication { 
   

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

}

启动测试

访问http://localhost:8761/
8761

此时访问http://localhost:8889/writer
8889

访问成功,读取远程配置。

高可用与负载均衡

同zuul的负载均衡一样,单个config-server容易出现故障,我们可以创建启动多个config-server来避免这个问题。

application-bakcup.yml

案例演示,我们可以在config-server的resources的目录下新建application-bakcup.yml

server:
  port: 8887

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Jaysong2012/scexample     # 配置git仓库的地址
          search-paths: springcloud-config-repo                             # git仓库地址下的相对地址,可以配置多个,用,分割。
          username:                                             # git仓库的账号(私有库必填)
          password:                                             # git仓库的密码(私有库必填)
      label: master                                        #配置仓库的分支

eureka:
  client:
    service-url:
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

启动测试

java -jar config-server-0.0.1-SNAPSHOT.jar
java -jar config-server-0.0.1-SNAPSHOT.jar  --spring.profiles.active=backup

访问http://localhost:8761/
muilt config

此时 我们关闭8888 config-server
访问http://localhost:8761/
在这里插入图片描述

此时访问http://localhost:8889/writer
writer

GitHub源代码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/219243.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【Altium Designer】PCB设计中利用board cutout做板子开孔开槽「建议收藏」

    【Altium Designer】PCB设计中利用board cutout做板子开孔开槽「建议收藏」有时候在pcb板子设计完成后,为了在使用中固定pcb板子,需要在pcb板子四个角开孔,3D视图中如下图所示。实现的方法不止一个,本文主要记录用boardcutout实现开圆形槽的方法,使用软件为AD18。1.选中mechanical1层或者Keep-outlayer层,先画出和孔径大小相同的圆;如下图2.设计-板子形状-定义板切割(快捷键DSC),在模式下沿着圆边点…

    2022年9月24日
    0
  • 一个标准的x.509数字证书包括哪些内容?(数字证书的功能是)

    1、什么叫数字签名数字签名:将报文按双方约定的HASH算法计算得到一个固定位数的报文摘要。在数学上保证:只要改动报文中任何一位,重新计算出的报文摘要值就会与原先的值不相符。这样就保证了报文的不可更改性。将该报文摘要值用发送者的私人密钥加密,然后连同原报文一起发送给接收者,而产生的报文即称数字签名2、什么叫数字证书数字证书:数字证书就是互联网通讯中标志通讯各方身份信息的一系列数据,提供了一种在In

    2022年4月15日
    121
  • AEJoy —— 介绍神奇的 10 个 AE 表达式附带 2 种简单的调试方法

    对于那些不熟悉AE的人来讲很快那么AE表达式是什么呢?AE表达式是一个基于Javascript编程语言的代码,您可以通过按Alt+左键点击小码表来插入到AE中。如图所示AE表达式非常强大,可以非常方便地制作酷炫的运动图形特效。它令人惊奇的地方是,仅仅需要少许的编程代码,它可以就可以帮助您的动画生动地运动起来。AE表达式可以显著改善您的工作流程,当您创建一个拥有复杂代码的动画时,甚至可以真切地实现惊人的动态图形特效。弹性表达式弹性表达式…

    2022年4月6日
    31
  • input之File对象的简单介绍

    input之File对象的简单介绍Input标签的file类型,提供了上传文件的功能。通过此类型,可以上传文件到服务器。但是如何实现上传呢?今天就来好好的说道说道。文件上传这个功能是比较常用的功能,实现起来也不是特别的难,稍微会点

    2022年7月1日
    22
  • 树莓派 gpio usb_树莓派gpio编程

    树莓派 gpio usb_树莓派gpio编程概览树莓派最令人兴奋的特点之一是它有一个GPIO连接器可以用来接其他的硬件设备。GPIO连接器实际上是由许多不同类型的接口组成的:真正的GPIO(GeneralPurposeInputOutput,通用输入/输出)针脚,你可以用来控制LED灯的开和关。I2C(Inter-IntegratedCircuit)接口针脚,使你能够仅使用2个控制针脚连接硬件模块。SPI(SerialPeriph…

    2022年10月14日
    0
  • 一篇万字长文讲清如何做数据治理

    一篇万字长文讲清如何做数据治理

    2021年7月5日
    106

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号