Eureka注册中心

一、创建Eureka注册中心

1. 创建项目Eureka_0720
2.导入依赖
XML 复制代码
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
XML 复制代码
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
3.在项目上新建模块EurkaServer
4.导入依赖
XML 复制代码
   <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
5.编写启动器
java 复制代码
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer7001.class, args);
        System.out.println("注册中心启动成功!");
    }
}
6.书写yml配置
XML 复制代码
server:
  port: 7001
eureka:
  instance:
    hostname: localhost  #主机名
  client:
    register-with-eureka: false #1、不注册自己(不做服务提供者)
    fetch-registry: false #2、不获取配置(不做消费者)
    service-url: #指定注册中心地址,配置集群逗号分隔即可
      defaultZone: http://localhost:7001/eureka
7.访问 http://localhost:7001/

二、创建服务提供者

1.在项目上新建模块Server
2.导入依赖
XML 复制代码
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
3.编写启动器
java 复制代码
@EnableEurekaClient
@SpringBootApplication
public class Server8001 {
    public static void main(String[] args) {
        SpringApplication.run(Server8001.class, args);
    }
}
4.书写yml配置
XML 复制代码
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
5.提供一个外部接口然后测试
java 复制代码
@RestController
@RequestMapping("/test")
public class TestController {
    @Value("${server.port}")
    private String port;

    @RequestMapping("/test1")
    public String test1(){
        return String.format("/port==>%s,service ok",port);
    }
}
6.先启动注册中心,然后启动服务提供者,然后查看注册中心能看到服务提供者即为成功
7.测试外部接口是否正常
8.复制多个sever模拟分布式部署
9.找到 VM options,不同版本界面可能不相同,添加 -Dserver.port配置端口
10.启动多个server

三、创建消费者

1.新建模块Client
2.导入依赖
XML 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
3.编辑启动器
java 复制代码
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Client80 {
    public static void main(String[] args) {
        SpringApplication.run(Client80.class, args);
    }
}
4.熟悉yml配置
XML 复制代码
server:
  port: 80
spring:
  application:
    name: cloud-order-service
eureka:
  client:
    register-with-eureka: true #向注册中心注册自己
    fetch-registry: true  #可以获取注册信息
    service-url:
      defaultZone: http://localhost:7001/eureka
5.RestTemplate配置

在配置类中添加Bean声明即可:

java 复制代码
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Client80 {
    public static void main(String[] args) {
        SpringApplication.run(Client80.class, args);
    }

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
6.通过注册中心拉取服务提供者列表 进行随机访问
java 复制代码
@RestController
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/test")
    public String test() {
        List<ServiceInstance> serverList = discoveryClient.getInstances("cloud-payment-service");
        System.out.println(serverList);
        int index = new Random().nextInt(serverList.size());
        ServiceInstance serviceInstance = serverList.get(index);
        String url = String.format("http://%s:%s/test/get", serviceInstance.getHost(), serviceInstance.getPort());
        return restTemplate.getForObject(url, String.class);
    }
}

server中

java 复制代码
@RestController
@RequestMapping("/test")
public class TestController {
    @Value("${server.port}")
    private String port;

    @RequestMapping("/test1")
    public String test1(){
        return String.format("/port==>%s,service ok",port);
    }

    @RequestMapping("/get")
    public String test() {
        return port;
    }
}
7.测试

访问消费者的test接口

多次刷新,访问的提供者是随机的,可以通过端口判断

相关推荐
lhldsg2 小时前
全民健身解决方案小程序开发:从0到1的技术实战
java·数据库·需求分析
jason成都3 小时前
Spring WebFlux 适配达梦新方案|dm‑r2dbc:Netty 异步传输的实验性 R2DBC 驱动
java·后端·spring
泡海椒3 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
辰烨chenye3 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode
小羊没烦恼!3 小时前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php
小陈不好吃4 小时前
从单体到微服务:Spring Cloud Gateway 动态路由实战与踩坑记录
微服务·云原生·架构
小荷才露尖尖角,早有蜻蜓立上头5 小时前
过滤器的4种创建方式
java
大圣编蚕5 小时前
Java ByteArrayInputStream 详解:从入门到实战
java·开发语言·算法
程序员阿明5 小时前
idea把插件啥的不默认放在C盘
java·ide·intellij-idea
linweidong6 小时前
中科闻歌Java面试题及参考答案
java·python·大模型·提示词·ai agent·mcp·rag原理