二、Eureka注册中心

Eureka注册中心服务端

引入依赖

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

编写启动类@EnableEurekaServer

复制代码
package com.gwf;

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

/**
 * @Author gwf
 * @Data 2024/2/17 下午5:24
 **/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

编写配置文件

复制代码
# 访问端口
server:
  port: 10086

#项目名
spring:
  application:
    name: eureka-server

# eureka-service配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka/

Eureka服务注册

引入依赖

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

配置

userservice
复制代码
server:
  port: 9000

spring:
  application:
    name: userservice

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
orderservice
复制代码
server:
    port: 9000

spring:
    application:
        name: orderservice
eureka:
    client:
        service-url:
            defaultZone: http://127.0.0.1:10086/eureka

多次启动userService

复制服务修改端口启动

远程调用

给RestTemplate这个Bean添加一个@LoadBalanced注解

复制代码
package com.gwf.orderservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

修改调用

复制代码
package com.gwf.orderservice;

import com.gwf.orderservice.entity.Order;
import com.gwf.orderservice.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author gwf
 * @Data 2024/2/17 下午3:39
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId")Long orderId){
        Order order = new Order();
        order.setId(orderId);
        order.setUserId(1L);
        order.setName("手机");
        order.setNum(2);
        order.setPrice(1999L);

        String url ="http://userservice:8081/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        return order;
    }
}
相关推荐
雨辰AI4 小时前
信创数仓分层建模|国产数据库 ODS/DWD/DWS 分层落地规范(金仓 / 达梦 / 高斯适配)
数据库·云原生·政务
阿里云云原生5 小时前
实验:回测、离线实验平台与题目级 Rubric丨AgentLoop 数据飞轮实践(四)
云原生·agent
LlmCraft|大模型工程实践7 小时前
08. Docker Compose 一键编排:多容器应用的指挥家
java·spring cloud·eureka
雾隐隐o8 小时前
Docker 镜像归档与容器管理
java·docker·eureka
Ningcode_cloud9 小时前
什么是CICD? GitLab + Jenkins 持续集成实战部署手册
运维·ci/cd·云原生·容器·gitlab·jenkins
瞬间&永恒~10 小时前
【Kubernetes】(十五)维护与升级、ETCD 备份和恢复
linux·运维·docker·云原生·kubernetes
小小程序员.¥11 小时前
docker基础
云原生·eureka
harmony&21 小时前
Docker 容器技术从入门到实战:容器、网络、存储与监控全解析
docker·云原生
分布式存储与RustFS1 天前
RustFS 生命周期与分层:让冷数据自动搬家、热数据自己回家
云原生·开源·对象存储·分布式存储·s3·rustfs·性能基准
阿里云云原生1 天前
从 Inform 到 Operate:构建可持续运行的云成本巡检机制与 Mission 调度
云原生