Spring Cloud Eureka--Zuul高可用/Turbine/配置中心工具

x33g5p2x  于2021-11-18 转载在 Spring  
字(8.1k)|赞(0)|评价(0)|浏览(266)

Zuul高可用

设置两个启动配置

sp06zuul-3001
--server.port=3001
sp06zuul-3002
--server.port=3002

启动zuul

现在只能分开监控,如果要同时监控所有服务器,使用Turbine

  • Turbine作用:聚合Hystrix监控数据,hystrix dashboard仪表盘可以从turbine抓取聚合的日志数据

使用Turbine工具

1.创建sp08-turbine工程

2.添加依赖

eureka client 、 turbine

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>springcloud1</artifactId>
        <groupId>cn.tedu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>cn.tedu</groupId>
    <artifactId>sp08-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sp08-turbine</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <mainClass>cn.tedu.sp08.Sp08TurbineApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3.配置yml

# 聚合服务的id列表
zuul,item-service,user-service............. 
# 为聚合后的数据命名 名字任意 default为默认名
new String("default")
************************************************************
spring:
  application:
    name: turbine
server:
  port: 5001
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
turbine:
  app-config: zuul  # aaa,bb,cc可聚合多个,用逗号隔开
  cluster-name-expression: new String("default")

4. 启动类添加注解

@EnableTurbine

package cn.tedu.sp08;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableTurbine
public class Sp08TurbineApplication {

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

5. 访问日志数据

http://localhost:5001/turbine.stream

eureka注册中心效果

hystrix dashboard效果

eureka只是注册中心!!!!!!!!!

Springcloud Config配置中心

  • 集中管理配置文件
  • 支持多种存储方式:数据库、本地磁盘文件、git仓库
  • 默认git仓库存储:配置中心启动 先连接git仓库 得到所有配置 各个模块从配置中心得到配置

准备Git仓库

1. 在springcloud1工程下新建config文件夹

2. 复制02.03.04配置文件到congfig目录 改名

item-service-dev.yml
order-service-dev.yml
user-service-dev.yml
dev为开发环境,根据不同环境进行不同的配置
不加-dev为主配置,主配置与不同环境的配置同步加载
-dev -test -pro…

3. 修改配置

  • 从配置中心下载的配置(优先级最高)会覆盖本地配置、本地命令参数,为避免覆盖,配置override-none=true
spring:
  cloud:
    config:
      override-none: true

4. 创建本地git仓库

已经创建仓库的就不用创建了

  1. Git–create git repository
  2. 选择springcloud1设置为仓库
    一个工程一个仓库(多数情况)

  1. 把代码提交本地仓库
    ctrl+k 或者右上角√按钮
    选择所有文件,填写条信息,完成提交

  1. 复制仓库地址
    已经有远程仓库不要重复创建
    gitee 中点➕下拉菜单,新建仓库
    仓库名称:springcloud1
    设置为开源仓库
    创建完成后,复制仓库地址

  1. 本地仓库推送到远程仓库
    ctrl+shift+k
    6.点define remote链接,粘贴gitee仓库地址,完成推送

搭建配置中心

1.新建模块sp09-config

2.添加依赖

eureka-client config server

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>springcloud1</artifactId>
        <groupId>cn.tedu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>cn.tedu</groupId>
    <artifactId>sp09-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sp09-config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <mainClass>cn.tedu.sp09.Sp09ConfigApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3.yml配置

#Git仓库的地址:
https://gitee.com/pigeon01/springcloud1
#存放配置文件的目录
config
********************************
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/pigeon01/springcloud1
          search-paths: config
server:
  port: 6001
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka

4.启动类添加注解

@EnableConfigServer

package cn.tedu.sp09;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class Sp09ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(Sp09ConfigApplication.class, args);
    }
}

5.访问验证

http://localhost:6001/item-service/dev
其实就在访问item-service-dev.yml
http://localhost:6001/user-service/dev
http://localhost:6001/order-service/dev

检查eureka中心

访问配置文件

配置中心的客户端模块

修改2.3.4连接配置中心,下载配置

1.删除application.yml(或代码注释掉)

2.添加依赖

config client

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

3.bootstrap.yml配置

#从注册表获得配置中心的地址 再从配置中心下载配置文件
#eureka 地址
#指定配置中心的服务id
#下载指定的配置文件:
- user-service
- dev
*********************************************************
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
  cloud:
    config:
      discovery:
        enabled: true  #允许使用服务发现 去发现配置中心
        service-id: config-server
      name: user-service  #文件名
      profile: dev        # user-service-dev.yml 为完整文件名

如果只启动user-service

相关文章