2.cloud-Eureka服务注册与发现

1.Eureka单点

1.1 建Module

1.2 POM

复制代码
<dependencies>

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

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

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

1.3 Yml

复制代码
server:
  port: 7001

eureka:
  instance:
    hostname: localhost  #eureka服务端的实例名字
  client:
    register-with-eureka: false    #表识不向注册中心注册自己
    fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
      service-url:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址

1.4 主启动

复制代码
@EnableEurekaServer
@SpringBootApplication
public class EurekaMain7001 {

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

2.支付服务注册

2.1 增加POM

复制代码
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.2 写YML

复制代码
server:
  port: 8001
spring:
  application:
    name: cloud-payment-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

2.3 主启动

复制代码
@EnableEurekaClient
@SpringBootApplication
public class PaymentMain {

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

2.4 服务中心

3.订单服务注册

3.1 增加POM

复制代码
<!--eureka-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.2 写Yml

复制代码
server:
  port: 8001

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka
spring:
  application:
    name: cloud-payment-service

3.3 使用@LoadBalanced

复制代码
@Configuration
public class ApplicationContextConfig {

    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

3.4 业务代码

复制代码
@RestController()
@RequestMapping("order")
@Slf4j
public class OrderController {
 
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";


    @GetMapping("/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    
}

4.actuator微服务信息完善

4.1 服务名称修改

复制代码
eureka:
  instance:
    instance-id: payment${server.port}

4.2 访问有ip信息提示

复制代码
eureka:
  instance: 
    prefer-ip-address: true

5.关闭保护机制

5.1 Server端yml

复制代码
eureka: 
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

5.2 服务端

复制代码
eureka:
  instance: 
    lease-renewal-interval-in-seconds: 10
    lease-expiration-duration-in-seconds: 10

5.3 最后集群配置

复制代码
server:
  port: 7002
eureka:
  instance:
    hostname: eureka7002.com
    ip-address: 192.168.2.3 #强制指定IP地址,默认会获取本机的IP地址
    prefer-ip-address: true
    instance-id: ${eureka.instance.ip-address}:${server.port}
  client:
    fetch-registry: false       #表示是否从Eureka Server获取注册的服务信息
    register-with-eureka: false #falase表示不向注册中心注册自己
    service-url:
      defaultZone: http://192.168.2.17:7001/eureka/    #设置与eureka server交互的地址查询服务和注
  server:
    enable-self-preservation: false     #关闭自我保护机制
    eviction-interval-timer-in-ms: 2000 #移除失效服务时间

spring:
  config:
    location: classpath:/,file:./config/
  cloud:
    inetutils:
      # 忽略指定网卡,支持正则表达式(这里使用正则表达式忽略所有虚拟机网卡)
      ignored-interfaces: ['VMware.*']
      use-only-site-local-interfaces: true
相关推荐
秋播1 天前
国内本地WSL2编译rancher源码
云原生
小猿姐3 天前
MySQL Top 10 热点问题 AI 运维实战:从内核诊断到云原生运维
mysql·云原生·aiops
阿里云云原生4 天前
深入内核:拆解 OpenTelemetry eBPF 探针如何优雅地“透视”多语言微服务?
云原生
JLWcai202510094 天前
铸造领域树脂砂轮|金利威多场景解决方案,20 + 配方覆盖全需求
mongodb·zookeeper·eureka·spark·rabbitmq·memcached·storm
java_cj4 天前
深入kube-apiserver认证机制:从Bearer Token到mTLS的完整认证链解析
linux·运维·服务器·云原生·容器·kubernetes
互联网推荐官4 天前
上海软件定制开发公司推荐:从PaaS工程化路径看D-coding的技术取舍
云原生·云计算·paas·软件开发·开发经验·上海
sbjdhjd4 天前
从零搭建企业级 CI/CD(下):Jenkins+GitLab+Harbor 全链路实战指南
git·servlet·ci/cd·云原生·云计算·gitlab·jenkins
java_cj5 天前
深入kubectl create源码:从YAML到Pod的完整链路拆解
运维·云原生·容器·kubernetes
步步为营DotNet5 天前
基于.NET Aspire 实现云原生应用的高效监控与可观测性
云原生·.net·wpf
牛奶咖啡135 天前
KVM虚拟化与企业应用实践——给远端主机创建虚拟机
云原生·qemu·kvm·给远端主机创建虚拟机·创建uefi模式的虚拟机·安装openeulersp2·vnc与虚拟机环境搭建