当前位置:   article > 正文

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

eureka java

Eureka简介

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

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

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

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

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

Eureka项目创建

1、新建Maven项目

在这里插入图片描述

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

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

在这里插入图片描述

3、创建子模块(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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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/  # 注册中心地址
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

7、运行

在这里插入图片描述

8、创建其他服务

在这里插入图片描述

9、修改pom.xml

demo/pom.xml

<packaging>pom</packaging>

<modules>
    <module>demo-eureka</module>
    <module>demo-one</module>
    <module>demo-two</module>
</modules>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

12、添加项目服务

在这里插入图片描述

13、运行

在这里插入图片描述

14、访问404解决

在这里插入图片描述

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

management:
  endpoints:
    web:
      exposure:
        include: "*"
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

15、项目目录结构

在这里插入图片描述

搭建Eureka集群

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

1、准备

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

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

在这里插入图片描述

2. 创建 Eureka Server 模块

在这里插入图片描述

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    • 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);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    • 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);
        }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
  • 配置项
    • 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/ # 注册中心地址
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
    • 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/ # 注册中心地址
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
    • 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/ # 注册中心地址
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

3、运行

在这里插入图片描述

4、创建 Eureka Client 模块并注册到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);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
  • 配置项
    • 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: "*"
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20

5、运行

在这里插入图片描述

6、项目目录结构

在这里插入图片描述

网络请求获取数据

1、 创建项目

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

server:
  port: 8001
  • 1
  • 2

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

server:
  port: 8002
  • 1
  • 2

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3、 运行

在这里插入图片描述

4、 目录结构

在这里插入图片描述

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

在这里插入图片描述

1、 创建项目

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

demo/pom.xml

<packaging>pom</packaging>

<modules>
    <module>eureka-server</module>
    <module>service-01</module>
    <module>service-02</module>
</modules>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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/ # 注册中心地址
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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: "*"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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";
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

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: "*"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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";
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

6、 运行

优先运行 EurekaServerApplication 启动类

在这里插入图片描述

7、 目录结构

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/974646
推荐阅读
相关标签
  

闽ICP备14008679号