spring-cloud笔记

本文介绍Spring Cloud中的服务注册与发现机制,包括Eureka Server作为服务注册中心的配置,以及如何使用@EnableEurekaServer和@EnableEurekaClient注解实现服务的自动注册与发现。同时介绍了Spring Cloud Config Server的配置方法,以及如何通过它实现外部配置文件管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring-cloud 日记

-服务注册发现
@EnableEurekaServer
spring-cloud client
@EnableEurekaClient
spring-cloud config server 配置服务器
@EnableConfigServer //也可以用@EnableEurekaServer 注册到注册服务器上

基本配置
@EnableEurekaServer

@SpringBootApplication
@RestController
@EnableEurekaServer
public class SpringCloudApplication {

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

yml配置

#eureka服务发现
server:
  port: 8761
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
           #客户端可用此地址将服务注册到注册服务器上
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

@EnableEurekaClient 客户端配置

@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope
public class SpringCloudConfigServerApplication {
    //这里是读取的远程git上的配置文件 在bootstrap.yml中配置好, config server的地址
    @Value("${spring.datasource.username}")
    String username;
    @Value("${spring.datasource.url}")
    String url;

    @RequestMapping("/hello")
    public String home() {
        return "username =="+username+"////name="+url;}

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

yml 配置

#服务注册中心
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:8761/eureka/

spring-boot 会先加载bootstrap.yml 文件
这里是通过配置服务器读取git上的配置文件

server:
  port: 8762
spring:
  application:
    name: application-YaoZhiYang
  cloud:
    config:
      #配置服务器的地址
      uri: http://localhost:8091/
      profile: dev
      #git分支
      label: master
     //关闭security的默认加载 不然refresh时会401错误 这里是临时配置
management:
  security:
    enabled: false

@EnableConfigServer 配置服务器
配置git项目路径 等配置

server:
  port: 8091
spring:
  cloud:
    config:
      server:
        git:
          uri: http://10.73.1.87/gsaves/springbootStudy.git
          #git项目中的路径
          search-paths: /src/main/resources
          default-label: master
          username: yaozhiyang
          password: yangzai666
#spring-cloud config提供的 访问格式 profile dev
#/{application}/{profile}[/{label}]
#/{application}-{profile}.yml
#/{label}/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.properties
  application:
    name: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

—–待修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值