Java(六):Eureka项目搭建、数据请求

这篇具有很好参考价值的文章主要介绍了Java(六):Eureka项目搭建、数据请求。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Eureka简介

Eureka采用的是Server/Client的模式进行设计。

Server扮演了服务注册中心的角色,为Client提供服务注册和发现的功能,维护着注册到自身的Client的相关信息,同时提供接口给Client获取到注册表中其他服务的信息。

Client将有关自己的服务的信息通过一定的方式登记到Server上,并在正常范围内维护自己信息的一致性,方便其他服务发现自己,同时可以通过Server获取到自己的依赖的其他服务信息,从而完成服务调用。

Eureka功能主要包括:服务注册服务续约服务剔除服务下线获取注册表信息远程调用等。

  • 以下是Eureka几个角色的解释:
    • Eureka服务端:负责服务注册、发现并管理每项服务的中心。
    • Eureka实例:服务(如订单系统)部署多个服务器,每个服务器上提供的服务都是实例。
    • Eureka服务:指提供特定服务功能的服务,例如:订单系统,同一服务可以提供多个实例;
    • Eureka客户端:主要向服务中心注册自己成为服务。但它既可以是服务提供者,也可以是消费者。它与Eureka实例感觉相似,但实际上意义不同。

Eureka项目创建

1、新建Maven项目

Java(六):Eureka项目搭建、数据请求

2、只保留Maven项目的依赖文件

pom.xml文件作为后续创建的子模块依赖的父依赖文件

Java(六):Eureka项目搭建、数据请求

3、创建子模块(Eureka服务模块)

Java(六):Eureka项目搭建、数据请求

Java(六):Eureka项目搭建、数据请求

Java(六):Eureka项目搭建、数据请求

4、修改pom.xml

demo/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.demo</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <modules>
        <module>demo-eureka</module>
    </modules>

    <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.6.13</spring-boot.version>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
    </properties>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

demo/demo-eureka/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.example.demo</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>demo-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-eureka</name>
    <description>demo-eureka</description>

</project>

5、创建并修改配置文件

demo/demo-eureka/src/main/resources/application.yml

server:
  port: 8000

spring:
  application:
    name: demo-eureka

eureka:
  instance:
    hostname: 127.0.0.1
  client:
    register-with-eureka: false   # 是否将自己注册到 Eureka-Server 中,默认的为 true
    fetch-registry: false         # 是否需要拉取服务信息,默认未true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  # 注册中心地址

6、添加Eureka注解

demo/demo-eureka/src/main/java/com/example/demo/demoeureka/DemoEurekaApplication.java

package com.example.demo.demoeureka;

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

@EnableEurekaServer
@SpringBootApplication
public class DemoEurekaApplication {

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

}

7、运行

Java(六):Eureka项目搭建、数据请求

8、创建其他服务

Java(六):Eureka项目搭建、数据请求

9、修改pom.xml

demo/pom.xml

<packaging>pom</packaging>

<modules>
    <module>demo-eureka</module>
    <module>demo-one</module>
    <module>demo-two</module>
</modules>

demo/demo-one/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.example.demo</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>demo-one</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-one</name>
    <description>demo-one</description>

</project>

demo/demo-two/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.example.demo</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>demo-two</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-two</name>
    <description>demo-two</description>

</project>

10、创建并修改配置文件

把服务注册到Eureka中

demo/demo-one/src/main/resources/application.yml

server:
  port: 8001

spring:
  application:
    name: demo-one

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8000/eureka/

demo/demo-two/src/main/resources/application.yml

server:
  port: 8002

spring:
  application:
    name: demo-two

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8000/eureka/

11、添加Eureka注解

demo/demo-one/src/main/java/com/example/demo/demoone/DemoOneApplication.java

package com.example.demo.demoone;

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

@EnableEurekaClient
@SpringBootApplication
public class DemoOneApplication {

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

}

demo/demo-two/src/main/java/com/example/demo/demotwo/DemoTwoApplication.java

package com.example.demo.demotwo;

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

@EnableEurekaClient
@SpringBootApplication
public class DemoTwoApplication {

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

}

12、添加项目服务

Java(六):Eureka项目搭建、数据请求

13、运行

Java(六):Eureka项目搭建、数据请求

14、访问404解决

Java(六):Eureka项目搭建、数据请求

开启监控服务配置demo/demo-one/src/main/resources/application.yml
开启监控服务配置demo/demo-two/src/main/resources/application.yml

management:
  endpoints:
    web:
      exposure:
        include: "*"

Java(六):Eureka项目搭建、数据请求

15、项目目录结构

Java(六):Eureka项目搭建、数据请求

搭建Eureka集群

应对服务挂了找不到服务的问题

1、准备

SwitchHosts:用于修改 IP 和 域名 的映射

https://github.com/oldj/SwitchHosts/releases

Java(六):Eureka项目搭建、数据请求

2. 创建 Eureka Server 模块

Java(六):Eureka项目搭建、数据请求

demo/pom.xml

<packaging>pom</packaging>

<modules>
    <module>eureka-server-01</module>
    <module>eureka-server-02</module>
    <module>eureka-server-03</module>
    <module>eureka-client-01</module>
</modules>

demo/eureka-server-01demo/eureka-server-02demo/eureka-server-03

  • 启动类
    • demo/eureka-server-01/src/main/java/com/example/demo/eurekaserver01/EurekaServer01Application.java
      package com.example.demo.eurekaserver01;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
      
      @EnableEurekaServer
      @SpringBootApplication
      public class EurekaServer01Application {
          public static void main(String[] args) {
              SpringApplication.run(EurekaServer01Application.class, args);
          }
      }
      
    • demo/eu¬reka-server-02/src/main/java/com/example/demo/eurekaserver01/EurekaServer02Application.java
      package com.example.demo.eurekaserver02;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
      
      @EnableEurekaServer
      @SpringBootApplication
      public class EurekaServer02Application {
          public static void main(String[] args) {
              SpringApplication.run(EurekaServer02Application.class, args);
          }
      }
      
    • demo/eureka-server-03/src/main/java/com/example/demo/eurekaserver01/EurekaServer03Application.java
      package com.example.demo.eurekaserver03;
        
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
        
      @EnableEurekaServer
      @SpringBootApplication
      public class EurekaServer03Application {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServer03Application.class, args);
        }
      }
      
  • 配置项
    • demo/eureka-server-01/src/main/resources/application.yml
      server:
      port: 8001
      
      spring:
      application:
      name: eureka-server-01
      
      eureka:
      instance:
      hostname: www.eureka-server-01.com
      client:
      register-with-eureka: false   # 是否将自己注册到 Eureka-Server 中,默认的为 true
      fetch-registry: false         # 是否需要拉取服务信息,默认未true
      service-url:
        defaultZone: http://www.eureka-server-02.com:8002/eureka/,http://www.eureka-server-03.com:8003/eureka/ # 注册中心地址
      
    • demo/eureka-server-02/src/main/resources/application.yml
      server:
        port: 8002
      
      spring:
        application:
          name: eureka-server-02
      
      eureka:
        instance:
          hostname: www.eureka-server-02.com
        client:
          register-with-eureka: false   # 是否将自己注册到 Eureka-Server 中,默认的为 true
          fetch-registry: false         # 是否需要拉取服务信息,默认未true
          service-url:
            defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-03.com:8003/eureka/ # 注册中心地址
      
    • demo/eureka-server-03/src/main/resources/application.yml
      server:
        port: 8003
      
      spring:
        application:
          name: eureka-server-03
      
      eureka:
        instance:
          hostname: www.eureka-server-03.com
        client:
          register-with-eureka: false   # 是否将自己注册到 Eureka-Server 中,默认的为 true
          fetch-registry: false         # 是否需要拉取服务信息,默认未true
          service-url:
            defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-02.com:8002/eureka/ # 注册中心地址
      

3、运行

Java(六):Eureka项目搭建、数据请求

4、创建 Eureka Client 模块并注册到Eureka服务中

Java(六):Eureka项目搭建、数据请求

  • 启动类
    • demo/eureka-client-01/src/main/java/com/example/demo/eurekaclient01/EurekaClient01Application.java
      package com.example.demo.eurekaclient01;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
      
      @EnableEurekaClient
      @SpringBootApplication
      public class EurekaClient01Application {
          public static void main(String[] args) {
              SpringApplication.run(EurekaClient01Application.class, args);
          }
      }
      
  • 配置项
    • demo/eureka-client-01/src/main/resources/application.yml
      server:
        port: 8081
      
      spring:
        application:
          name: eureka-client-01
      
      eureka:
        instance:
          hostname: www.eureka-client-01.com
          instance-id: eureka-client-01
        client:
          service-url:
            defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-02.com:8002/eureka/,http://www.eureka-server-03.com:8003/eureka/
      
      management:
        endpoints:
          web:
            exposure:
              include: "*"
      

5、运行

Java(六):Eureka项目搭建、数据请求

6、项目目录结构

Java(六):Eureka项目搭建、数据请求

网络请求获取数据

1、 创建项目

Java(六):Eureka项目搭建、数据请求

Java(六):Eureka项目搭建、数据请求

Java(六):Eureka项目搭建、数据请求

demo/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.demo</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <modules>
        <module>service-01</module>
        <module>service-02</module>
    </modules>

    <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.6.13</spring-boot.version>
    </properties>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

demo/service-01/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.example.demo</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>service-01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-01</name>
    <description>service-01</description>
   
</project>

demo/service-02/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.example.demo</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>service-02</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-02</name>
    <description>service-02</description>

</project>

demo/service-01/src/main/resources/application.yml

server:
  port: 8001

demo/service-02/src/main/resources/application.yml

server:
  port: 8002

2、 创建请求

demo/service-01/src/main/java/com/example/demo/service01/TestController.java

package com.example.demo.service01;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping
public class TestController {

    private final RestTemplate restTemplate = new RestTemplate();

    @GetMapping("msg")
    public String msg() {
        return "service-01 服务中请求 service-02";
    }

    @GetMapping("say")
    public String say() {
        String html = restTemplate.getForObject("https://www.example.com/", String.class);
        String msg = restTemplate.getForObject("http://127.0.0.1:8002/msg", String.class);
        return String.format("%s<h1 style=\"text-align: center;\">%s</h1>", html, msg);
    }

}

demo/service-01/src/main/java/com/example/demo/service01/TestController.java

package com.example.demo.service02;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping
public class TestController {

    private final RestTemplate restTemplate = new RestTemplate();

    @GetMapping("msg")
    public String msg() {
        return "service-02 服务中请求 service-01";
    }

    @GetMapping("say")
    public String say() {
        String html = restTemplate.getForObject("https://www.example.com/", String.class);
        String msg = restTemplate.getForObject("http://127.0.0.1:8001/msg", String.class);
        return String.format("%s<h1 style=\"text-align: center;\">%s</h1>", html, msg);
    }

}

3、 运行

Java(六):Eureka项目搭建、数据请求

4、 目录结构

Java(六):Eureka项目搭建、数据请求

利用Eureka实现服务间数据请求

Java(六):Eureka项目搭建、数据请求

1、 创建项目

Java(六):Eureka项目搭建、数据请求

Java(六):Eureka项目搭建、数据请求

Java(六):Eureka项目搭建、数据请求

demo/pom.xml

<packaging>pom</packaging>

<modules>
    <module>eureka-server</module>
    <module>service-01</module>
    <module>service-02</module>
</modules>

2、 添加依赖

demo/pom.xml

<!-- 用于服务间请求的依赖 -->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3、 demo/eureka-server

demo/eureka-server/src/main/resources/application.yml

server:
  port: 8001

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: www.eureka-server.com
  client:
    register-with-eureka: false   # 是否将自己注册到 Eureka-Server 中,默认的为 true
    fetch-registry: false         # 是否需要拉取服务信息,默认未true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册中心地址

demo/eureka-server/src/main/java/com/example/demo/eurekaserver/EurekaServerApplication.java

package com.example.demo.eurekaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

4、 demo/service-01

demo/service-01/src/main/resources/application.yml

server:
  port: 8081

spring:
  application:
    name: service-01

eureka:
  instance:
    hostname: www.eureka-server.com
    instance-id: service-01
  client:
    service-url:
      defaultZone: http://www.eureka-server.com:8001/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"

demo/service-01/src/main/java/com/example/demo/service01/Service01Application.java

package com.example.demo.service01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Service01Application {

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

}

demo/service-01/src/main/java/com/example/demo/service01/api/IService02.java

package com.example.demo.service01.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "SERVICE-02")
public interface IService02 {
    @GetMapping("/getMsg")
    public String getMsg();
}

demo/service-01/src/main/java/com/example/demo/service01/Service01Controller.java

package com.example.demo.service01;

import com.example.demo.service01.api.IService02;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping
public class Service01Controller {

    @Resource
    private IService02 service02;

    @GetMapping("/say")
    public String say(){
        return service02.getMsg();
    }

    @GetMapping("getMsg")
    public String getMsg() {
        return "SERVICE-02 调用 SERVICE-01";
    }

}

5、 demo/service-02

demo/service-02/src/main/resources/application.yml

server:
  port: 8082

spring:
  application:
    name: service-02

eureka:
  instance:
    hostname: www.eureka-server.com
    instance-id: service-02
  client:
    service-url:
      defaultZone: http://www.eureka-server.com:8001/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"

demo/service-02/src/main/java/com/example/demo/service02/Service02Application.java

package com.example.demo.service02;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Service02Application {

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

}

demo/service-02/src/main/java/com/example/demo/service02/api/IService01.java

package com.example.demo.service02.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "SERVICE-01")
public interface IService01 {
    @GetMapping("/getMsg")
    public String getMsg();
}

demo/service-02/src/main/java/com/example/demo/service02/Service02Controller.java

package com.example.demo.service02;

import com.example.demo.service02.api.IService01;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping
public class Service02Controller {

    @Resource
    private IService01 service01;

    @GetMapping("/say")
    public String say(){
        return service01.getMsg();
    }

    @GetMapping("getMsg")
    public String getMsg() {
        return "SERVICE-01 调用 SERVICE-02";
    }

}

6、 运行

优先运行 EurekaServerApplication 启动类

Java(六):Eureka项目搭建、数据请求

7、 目录结构

Java(六):Eureka项目搭建、数据请求文章来源地址https://www.toymoban.com/news/detail-506410.html

到了这里,关于Java(六):Eureka项目搭建、数据请求的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Cloud学习笔记:Eureka简介,Eureka简单样例

    这是本人学习的总结,主要学习资料如下 - 马士兵教育 Eureka 是 SpringCloud Nexflix 的核心子模块,其中包含 Server 和 Client 。 Server 提供服务注册,存储所有可用服务节点。 Client 用于简化和 Server 的通讯复杂度。 下面是 Eureka 的简单架构图 每一个服务节点需要在 Eureka Server 中注册

    2024年04月16日
    浏览(34)
  • Eureka 简介

    “Eureka 来源于古希腊词汇,意为\\\"发现了”,在软件领域,Eureka 是Netflix 在线影片公司开源的一个服务注册和发现组件,和其他的Netflix公司的服务组件(例如负载均衡器,熔断器,网关等)一起,被SpringCloud 社区整合为Spring Cloud Netflix 模块 和Zookeeper 类似,Eureka 是一个服务注

    2024年02月13日
    浏览(20)
  • Eureka简介及使用

    Eureka的功能主要有以下几点: 1、注册服务信息 服务提供者启动时候向Eureka注册自己的信息,Eureka保存这些信息。 2、拉取服务 Eureka可以根据服务名称来查询保存的信息,拉取服务。 这里为了保证拉取的服务都是可用的,Eureka有一个检测心跳的功能,服务提供者每30s向Eurek

    2024年02月03日
    浏览(19)
  • [SpringCloud] Eureka 与 Ribbon 简介

    目录 一、服务拆分 1、案例一:多端口微服务 2、案例二:服务远程调用 二、Eureka 1、Eureka 原理分析 2、Eureka 服务搭建(注册 eureka 服务) 3、Eureka 服务注册(注册其他服务) 4、Eureka 服务发现(服务拉取) 三、Ribbon 负载均衡  1、负载均衡原理 2、负载均衡策略 3、饥饿加载

    2024年02月08日
    浏览(31)
  • Java之 Spring Cloud 微服务 Eureka (第一个阶段)【二】【SpringBoot项目实现商品服务器端是调用】

    | Java之 Spring Cloud 微服务的 SpringCloud Config 配置中心(第四个阶段)【二】【SpringBoot项目实现商品服务器端调用】 | | | Java之 Spring Cloud 微服务的开源配置中心Apollo(第四个阶段)【三】【SpringBoot项目实现商品服务器端调用】 | | Java之 Spring Cloud 微服务 Eureka 【二】【SpringBoot项

    2024年04月12日
    浏览(34)
  • Vue+Element UI 生鲜管理系统简介及项目搭建,页面布局(一)

    自从入了这家公司,就没分配过前端的工作了,在上一家还能前后端都写写,现在真是对vue的代码真是望尘莫及哇,前几天跟前端朋友交流前端知识的时候,发现自己脑子里面的前端代码好像被偷了一样,赶紧找个项目练练,虽然现在是java,以后还是想要做全栈呢( ▽ )(哈哈

    2024年02月11日
    浏览(30)
  • SpringCloud(二)Eureka简介与依赖导入

      Eureka能够自动注册并发现微服务,然后对服务的状态、信息进行集中管理,这样当我们需要获取其他服务的信息时,我们只需要向Eureka进行查询就可以了。   像这样的话,服务之间的强关联性就会被进一步削弱。 那么现在我们就来搭建一个Eureka服务器,只需要创建一个新

    2024年02月08日
    浏览(29)
  • 最好用的文本与文件查询软件AnyTXT Searcher与Listary简介

    Listary是一个革命性的Windows搜索工具,借助 Listary软件,你可以快速搜索电脑文件、定位文件、执行智能命令、记录访问历史、快速切换目录、收藏常用项目等。 Listary为Windows传统低效的文件打开/保存对话框提供了便捷、人性化的文件(夹)定位方式,同时改善了常见文件管

    2024年02月12日
    浏览(41)
  • 二、Spring Cloud Eureka 简介、快速入门

    Eureka 来源于古希腊词汇,意为“发现了”。在软件领域, Eureka 是 Netflix 在线影片公司开源的一个 服务注册与发现的组件 ,和其他 Netflix 公司的服务组件(例如负载均衡、熔断器、网关等) 一起,被 Spring Cloud 社区整合为 Spring Cloud Netflix 模块。 Eureka 是 Netflix 贡献给 Spring

    2024年02月12日
    浏览(65)
  • SpringCloud 尚硅谷 微服务简介以及Eureka使用

    该系列博客仅用于本人学习尚硅谷课程SpringCloud笔记,其中的错误在所难免,如有错误恳请指正。 官方源码地址:https://github.com/zzyybs/atguigu_spirngcloud2020    Spring Cloud是微服务一站式服务解决方案,微服务全家桶。它是微服务开发的主流技术栈。SpringCloud 和 SpringCloud Alibaba 目

    2024年02月13日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包