SpringCloud学习-Eureka,config配置中心

SpringCloud Eureka学习

在这里插入图片描述

因为我们同时还学习多模块,所以先新建一个项目,然后把其他的东西都删除,值留下一个pom.xml和.md文件。其他的都删除。

新建项目,此处我们使用阿里云的io路径

:https://start.aliyun.com/
在这里插入图片描述
要记得选SpringCloud,此处新建后吧其他的都删除,只留这两个
在这里插入图片描述

  • 我们看一下pom文件,此处需要加入
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
    <!--  需要引入module  -->
<modules>
    <module>eureka-service</module>
</modules>
  • 我们配置yml文件
# 应用名称
spring:
  application:
    name: eureka-service
#端口号
server:
  port: 8100

eureka:
  instance:
#    服务注册中心实例的主机名
    hostname: 127.0.0.1
  client:
#    是否向服务注册中心注册自己
    registerWithEureka: false
#    是否检索服务
    fetchRegistry: false
#    服务注册中心的配置内容,指定服务注册中心的位置
    serviceUrl:
      # 如果配置了密码
      #      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
      #没有密码的配置
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

  • 修改启动类注释, @EnableEurekaServer
package com.example.eurekaservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author zhang
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {

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

}

  1. 启动测试
    这是成功服务成功后的页面

Eureka客户端服务-Eureka-config配置中心

  1. 新增eureka-config模块包
    在这里插入图片描述
    在父包中添加
<module>eureka-config</module>
  1. 配置yml文件
server:
  port: 8101

# 应用名称
spring:
  application:
    name: eureka-config
  profiles:
    active: dev
  cloud:
    config:
      # git 上的仓库分支
      label: master
      server:
        git:
          uri: https://gitee.com/zhang_jun_fa/spring-cloud-config.git
          #本地路径
          basedir: D:\home
          # git 的用户名和密码
          # password:
          # username:

#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8100/eureka/
  instance:
    prefer-ip-address: true

  1. 配置启动类注释
package com.example.eurekaconfig;

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

@SpringBootApplication
@EnableConfigServer  //加入注解声明为配置中心
@EnableEurekaClient
public class EurekaConfigApplication {

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

}

  1. 启动测试
    Eureka发现服务测试成功
    成功后,服务已经发现
    测试config配置中心
    地址:http://192.168.2.187:8101/eurekaconfig/dev/master
    测试成功结果
    或者:http://192.168.2.187:8101/eurekaconfig-dev.yml
    在这里插入图片描述

Eureka客户端服务和Eureka-config客户端-使用

  1. 在父包中引入
        <module>eureka-admin</module>

本包的pom中需要引入,用来动态刷新

<!--Spring Boot Actuator,感应服务端变化-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 配置yml文件
server:
  port: 8102

#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8100/eureka/
  instance:
    prefer-ip-address: true

# 应用名称
spring:
  application:
    name: eureka-admin
  profiles:
    active: dev
  cloud:
    config:
      profile: dev
      label: master #git仓库的分支
      uri: http://localhost:8101/
      name: eurekaconfig  #要读取的Git仓库中的文件名称

#开启刷新端点
management:
  endpoints:
    web:
      exposure:
        include:
          - refresh # 开启热刷新服务, 也可以在Gitee中的配置文件内定义。
          - info
          - health

  1. 配置启动类,添加注释
package com.example.eurekaadmin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author zhang
 */
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class EurekaAdminApplication {

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

}
  1. 编写测试Controller
package com.example.eurekaadmin.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0.0
 * @className: TestController
 * @description:
 * @author: zhangjunfa
 * @date: 2022/8/22 12:48
 */
@RestController
@RequestMapping
@RefreshScope   //热刷新
public class TestController {

    @Value("${version}")
    private String value;

    @GetMapping("/test")
    public String getEv() {
        return value;
    }
}

  1. 启动测试
    此处启动会报一个常见的错误:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'version' in value "${version}"

刚学习的时候,不知道为啥,www.baidu了好久。有说是yml中要加配置的。也有说是版本。但是都不满足我这个地方的BUG。还有说是application.yml文件要命名为bootstrap.yml,然后就没有然后了。启动成功》》》》
还有一个错误,也贴出来:就是我启动后。都注册成功了。然后给我注销了

Saw local status change event StatusChangeEvent [timestamp=1661144320316, current=DOWN, previous=UP]

一百度:说是需要

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

引入这个就可以了。
启动正常:
成功启动,Eureka注册成功
我们用Postman测试
测试1: 测试读取Eureka-Config配置中心中的数据
成功结果
测试二:热刷新
我们在 版本后面修改为test
刷新成功
测试成功

结语

至此,我们的SpringCloud的Eureka发现服务和注册服务都完美运行了。还有SpringCloud的Config配置中心也完美运行。后续可以加入SpringCloud的openfeign来实现远程调用。
Config配置中心配置文件Gitte: https://gitee.com/zhang_jun_fa/spring-cloud-config.git
本项目Gitte:https://gitee.com/zhang_jun_fa/ross-eureka.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Cloud Config Server注册到Eureka注册中心的步骤如下: 1. 首先需要在pom.xml文件中添加Eureka Client的依赖,如下所示: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 2. 在应用程序的配置文件中添加以下配置: ```yaml eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ ``` 其中,`defaultZone`指定了Eureka Server的地址。 3. 在Spring Boot应用程序类上添加@EnableEurekaClient注解,以将Config Server注册到Eureka Server上: ```java @EnableConfigServer @EnableDiscoveryClient @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` 4. 运行Config Server应用程序,查看Eureka Server的管理页面(http://localhost:8761)上是否已经注册成功。 5. 客户端应用程序可以通过以下方式从Config Server获取配置: ```yaml spring: application: name: client-service cloud: config: uri: http://localhost:8888 fail-fast: true retry: maxAttempts: 20 multiplier: 1.5 maxInterval: 5000 label: master ``` 其中,`cloud.config.uri`指定了Config Server的地址,`spring.application.name`指定了客户端应用程序的名称,`label`指定了Git仓库的分支名称。 6. 运行客户端应用程序,即可从Config Server获取配置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拈㕦一笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值