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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • U盘pe(理论大白菜、优启通、微PE都可以) 装ESXI方案 (非通用UltraISO重做启动U盘),省U盘「建议收藏」

    U盘pe(理论大白菜、优启通、微PE都可以) 装ESXI方案 (非通用UltraISO重做启动U盘),省U盘「建议收藏」此文是我发的一篇的准备工作,因为ESXi6.7刚发布的原因,很多同学等着升级,故而先写了出来。原文如下:戴尔R730安装升级VMwarevSphereHypervisor(ESXi)6.7https://koolshare.cn/thread-139061-1-1.html这一篇,介绍怎样从U盘加载ISO镜像,并引导电脑/服务器,完成新安装/升级系统的操作,当然,实际中不仅仅用于ESXi的安装升级,也可以用于NAS4Free、FreeNAS、ProxmoxVE虚拟机系统,爱快软路由系统、Pan

    2022年9月3日
  • 十大安全数字货币钱包排行榜_全球数字货币排行

    十大安全数字货币钱包排行榜_全球数字货币排行链客,专为开发者而生,有问必答!此文章来自区块链技术社区,未经允许拒绝转载。随着全球加密数字货币规模正在超高速的增长,数字货币的安全存储成为了关键的一环,也是决定其健康可持续发展的基石。市场崛起引发了全球对加密数字货币未来前景的关注和期待,数字货币钱包也就应运而生。大批投资者涌入币圈炒币,都离不开数字货币钱包,那么哪些数字货币钱包比较安全呢?下面我们就来一起探讨一下吧。1、Imtoken…

    2022年10月9日
  • 红帽子虚拟机安装教程_红帽子联网后怎么安装gcc

    红帽子虚拟机安装教程_红帽子联网后怎么安装gcc在WindowsXP中安装虚拟机VMware的方法跟普通软件基本一致,需要填入注册密钥.安装完成以后就可以安装红帽子企业版5操作系统了,首先需要新建一台虚拟的计算机,为其分配内存,硬盘空间,CPU等信息,然后选择启动的位置,是硬盘中的ISO文件还是光驱中的启动,然后就可以启动虚拟机进行安装了.在安装红帽子企业版时选择定制安装,作为初学者可以选择把所有的东西都安装上,这…

    2022年9月14日
  • 新的Visual C++代码优化器

    微软在5月4日发布了新的高级代码优化器,服务于VisualC++的后端编译器。提高了代码性能,可以压缩代码体积,将编译器带入了一个新的境界。VisualC++的团队在博客上称,这将

    2021年12月23日
  • tcping命令详解

    tcping命令详解tcping命令用法以及帮助文档的各项参数的解释。

    2022年6月23日
  • cxf框架调用webservice_webservice json

    cxf框架调用webservice_webservice json本文主要包括以下内容ant工具的使用利用cxf实现webservicecxf与spring整合ajax访问webserviceant工具1、为什么要用到ant这个工具呢?Ant做为一种工具已经广泛被使用,并且历史悠久。使用ant的内置命令,可以编译java源文件(javac),运行java文件(java),给class文件打包(jar、war、ear),也可以创建(mkdir)、删除…

    2022年9月15日

发表回复

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

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