配置环境:

springcloud版本:2021.0.5

springboot版本: 2.6.13

java环境: 1.8

使用到的组件: eureka(服务发现),openfeign(远程调用),gateway(路由), config (配置中心),buds(消息总线)

其他暂未使用的,比如:链路追踪,熔断,限流,哪些都是你在搭建这套服务后,边学顺带就能加上的,这些咱们就

不在引入,这边文章的主要目的是先让你能把这个微服务组件配合能够运行起来,然后你在慢慢研究个个参数是什么意思,

因为只有你能运行起来了,你才不会从0到放弃。

第一步配置服务发现 eureka server

pom文件:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.binson</groupId>
    <artifactId>eurekaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekaserver</name>
    <description>eurekaServer</description>
    <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.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
                <configuration>
                    <mainClass>com.binson.eurekaserver.EurekaserverApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

配置文件yaml

因为要多节点部署,所以配置了三个配置选项,方便在docker 中编排

首先是

1,application.yaml

spring:
  application:
    name: eureka-server

2,为了编排,剩余三个配置文件

这个配置的端口是8761

application-node1.yaml

server:
  port: 8761
eureka:
  instance:
    hostname: node1
    lease-expiration-duration-in-seconds: 90  #表示 Eureka Server 在接收到上一个心跳之后等待下一个心跳的秒数(默认 90 秒)
    lease-renewal-interval-in-seconds: 30 #表示 Eureka Client 向 Eureka Server 发送心跳的频率(默认 30 秒)
  server:   # 作为server端需要的配置项
    enable-self-preservation: false # 关闭自我保护,默认是true ,如果是true ,那么服务发现会在设定时间内检测是否有超过85% 的服务都注册不上,如果都注册不上
    ##那么就任务是网络出现了问题,不会删除 微服务注册的节点。只会不断接收新的注册节点,而不在删除旧的折点。 如果为fasle 那么只要检测有某个节点心跳时间超过设定的心跳时间
    ## 就认为是断开连接,eureka 会果断的将这个注册记录缓存给删掉。
    eviction-interval-timer-in-ms: 3000 # 修改检查失效服务的时间。多长时间会触发一次检查,表示清理无效节点的频率。
  client:
    fetch-registry: false # #是否检索服务 作为客户端,不需要获取注册中心信息(表示客户端是否从 Eureka Server 获取实例注册信息。)
    register-with-eureka: true  # 作为客户端,不需要在注册中心注册自己(表示此实例是否注册到 Eureka Server 以供其他实例发现)
    service-url:
      defaultZone:  ${EUREKA_URL:http://node2:8762/eureka/},${EUREKA_URL:http://node3:8763/eureka/}

application-node2.yaml ,这个端口是8762

server:
  port: 8762
eureka:
  instance:
    hostname: node2
    lease-expiration-duration-in-seconds: 90  #表示 Eureka Server 在接收到上一个心跳之后等待下一个心跳的秒数(默认 90 秒)
    lease-renewal-interval-in-seconds: 30 #表示 Eureka Client 向 Eureka Server 发送心跳的频率(默认 30 秒)
  server:   # 作为server端需要的配置项
    enable-self-preservation: false # 关闭自我保护,默认是true ,如果是true ,那么服务发现会在设定时间内检测是否有超过85% 的服务都注册不上,如果都注册不上
    ##那么就任务是网络出现了问题,不会删除 微服务注册的节点。只会不断接收新的注册节点,而不在删除旧的折点。 如果为fasle 那么只要检测有某个节点心跳时间超过设定的心跳时间
    ## 就认为是断开连接,eureka 会果断的将这个注册记录缓存给删掉。
    eviction-interval-timer-in-ms: 3000 # 修改检查失效服务的时间。多长时间会触发一次检查,表示清理无效节点的频率。
  client:
    fetch-registry: false # #是否检索服务 作为客户端,不需要获取注册中心信息(表示客户端是否从 Eureka Server 获取实例注册信息。)
    register-with-eureka: false  # 作为客户端,不需要在注册中心注册自己(表示此实例是否注册到 Eureka Server 以供其他实例发现)
    service-url:
      defaultZone: ${EUREKA_URL:http://node1:8761/eureka/},${EUREKA_URL:http://node3:8763/eureka/}

application-node3.yaml ,这个端口是8763

server:
  port: 8763
eureka:
  instance:
    hostname: node3
    lease-expiration-duration-in-seconds: 90  #表示 Eureka Server 在接收到上一个心跳之后等待下一个心跳的秒数(默认 90 秒)
    lease-renewal-interval-in-seconds: 30 #表示 Eureka Client 向 Eureka Server 发送心跳的频率(默认 30 秒)
  server:   # 作为server端需要的配置项
    enable-self-preservation: false # 关闭自我保护,默认是true ,如果是true ,那么服务发现会在设定时间内检测是否有超过85% 的服务都注册不上,如果都注册不上
    ##那么就任务是网络出现了问题,不会删除 微服务注册的节点。只会不断接收新的注册节点,而不在删除旧的折点。 如果为fasle 那么只要检测有某个节点心跳时间超过设定的心跳时间
    ## 就认为是断开连接,eureka 会果断的将这个注册记录缓存给删掉。
    eviction-interval-timer-in-ms: 3000 # 修改检查失效服务的时间。多长时间会触发一次检查,表示清理无效节点的频率。
  client:
    fetch-registry: false # #是否检索服务 作为客户端,不需要获取注册中心信息(表示客户端是否从 Eureka Server 获取实例注册信息。)
    register-with-eureka: false  # 作为客户端,不需要在注册中心注册自己(表示此实例是否注册到 Eureka Server 以供其他实例发现)
    service-url:
      defaultZone: ${EUREKA_URL:http://node1:8761/eureka/},${EUREKA_URL:http://node2:8762/eureka/}

为什么需要三个配置,因为如果你需要多节点部署的话,配置三个是最合理的。

接下来启动类记得加上注解@EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {

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

}

接下来是针对docker的部署,这里面咱们简单编写一下Dockerfile

FROM java:openjdk-8u111-jdk

COPY eurekaserver-0.0.1-SNAPSHOT.jar app.jar

#EXPOSE 8761

ENTRYPOINT ["java","-Xmx128m","-jar","app.jar"]

在服务端记得生成image

最简单的命令:

docker build -t eurekaserver:0.0.1-SNAPSHOT .

注意后面这个 点 .

接下来是 docker 的编排:

名字叫:docker-compose.yml

启动命令: docker compose up -d

version: "3"
services:
  node1:      # 默认情况下,其他服务可以使用服务名称连接到该服务。因此,对于peer2的节点,它需要连接http://peer1:8761/eureka/,因此需要配置该服务的名称是peer1。
    container_name: node1
    image: eurekaserver:0.0.1-SNAPSHOT
    hostname: node1
    networks:
      - eureka-net
    ports:
      - "8761:8761"
    environment:
      - spring.profiles.active=node1
  node2:
    container_name: node2
    image: eurekaserver:0.0.1-SNAPSHOT
    hostname: node2
    networks:
      - eureka-net
    ports:
      - "8762:8762"
    environment:
      - spring.profiles.active=node2
  node3:
    container_name: node3
    image: eurekaserver:0.0.1-SNAPSHOT
    hostname: node3
    networks:
      - eureka-net
    ports:
      - "8763:8763"
    environment:
      - spring.profiles.active=node3

  gateway1:
    container_name: gateway1
    image: gateway:0.0.1-SNAPSHOT
    hostname: gateway1
    networks:
      - eureka-net
    ports:
      - "8861:8861"


  userprovider:
    container_name: userprovider
    image: userprovider:0.0.1-SNAPSHOT
    hostname: userprovider
    ports:
      - "8897:8897"
    networks:
      - eureka-net

  userservice:
    container_name: userservice
    image: userservice:0.0.1-SNAPSHOT
    hostname: userservice
    ports:
      - "18895:18895"
    networks:
      - eureka-net

networks:
  eureka-net:
    driver: bridge

里面的内容现在暂时还有gateway1,跟provider 以及userservice,如果你先实验第一个eureka, 那么这些可以暂时先去掉。

启动完毕后的画面是这样的:

重点关注:DS Replicas, 后面的注册是其他服务注册上去的内容。

第二步编写一个服务端userprovider,注册到这个eureka 里面

因为版本原因,我们需要在pom文件中手动引入bootstap 包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
    <version>3.0.3</version>
</dependency>

1,下面我们先从pom文件开始:

里面有些引入你先按照我的,后面会有用的。这里面的有一个com.binson.pojo 是我们实体类的组件,这个后面在补充。

<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.binson.userProvider</groupId>
    <artifactId>userprovider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>userprovider</name>
    <description>用户服务的供应者</description>
    <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>com.binson</groupId>
            <artifactId>pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--        </dependency>-->
        <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>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-amqp</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-bus</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>
                <configuration>
                    <mainClass>com.binson.userprovider.userprovider.UserproviderApplication</mainClass>
<!--                    <skip>true</skip>-->
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2,配置文件

bootstrap.yml 文件,这些内容一定要放到这里面,不要放在别的地方,

里面eureka 的三个server节点都要填上去。

bus,rabbitmq,config 三个组件我们后面在说,如果你到这一步,可以先把这些无用的去掉,先让这个服务能在eureka 中注册上去。

server:
  port: 8897
eureka:
  instance:
    instance-id: userprovider
    prefer-ip-address: true
    hostname: userprovider
  client:
    service-url:
      defaultZone: http://47.116.121.171:8763/eureka/,http://47.116.121.171:8762/eureka/,http://47.116.121.171:8761/eureka/
spring:
  bus:
    id: ${spring.application.name}:${server.port}:*
    enabled: true
    refresh:
      enabled: true
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: password
  cloud:
    config:
      profile: dev        # 不加此属性直接获取eureka-client.yml,加了后符合config的名字规则eureka-client-dev.yml
      enabled: true
      name: config-eureka-client   # 配置中心Git仓库config文件夹里的文件名字
      label: master         # git的默认分支master
      fail-fast: true       # 是否启动快速失败功能,功能开启则优先判断config server是否正常
      discovery:
        enabled: true            #启动配置中心发现服务
        service-id: config-server
      #      allow-override: true  # 允许被覆盖,如果是配置文件,则不允许被覆盖
      #      override-none: false  #远程配置不覆盖任何本地配置
      #      override-system-properties: false  #远程配置不覆盖系统属性与环境变量,但是会覆盖本地配置文件
      retry:
        # 配置重试次数,默认为6
        max-attempts: 3
        # 间隔乘数,默认1.1
        multiplier: 1
        # 初始重试间隔时间,默认1000ms
        initial-interval: 1000
        # 最大间隔时间,默认2000ms
        max-interval: 3000
management:
  endpoint:
    shutdown:
      enabled: false
  endpoints:
    web:
      exposure:
        include: busrefresh

接下来是application.yaml

spring:
  application:
    name: userprovider

接下来是代码:启动类

package com.binson.userprovider.userprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
@RefreshScope
public class UserproviderApplication {

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

}

到这一步你启动服务,eureka 应该就能看到你注册的服务了。

为了后面的其他组件,我们暂时先把代码结构截图发一下:

接下来是几个类:

package com.binson.userprovider.userprovider.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * ClassName: GitAutoRefreshConfig
 * Description:
 *
 * @author binbin_hao
 * @date 2024/9/11 15:20
 */
@Component
@Data
@Configuration
@ConfigurationProperties(prefix = "data")
public class GitAutoRefreshConfig {
    public static class UserInfo {
        private String username;

        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "UserInfo{" +
                    "username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }

    private String env;

    private UserInfo user;
}
package com.binson.userprovider.userprovider.controller;

import com.binson.userprovider.userprovider.config.GitAutoRefreshConfig;
import com.binson.userprovider.userprovider.config.GiteeConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: OrderController
 * Description:
 *
 * @author binbin_hao
 * @date 2024/9/10 14:16
 */
@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {
    @Autowired
    private GiteeConfig giteeConfig;

    @Autowired
    private GitAutoRefreshConfig gitAutoRefreshConfig;
    @GetMapping("/hello")
    @RefreshScope
    String helloOrder(){
        System.out.println("hello order");
        return "hello order:env"+gitAutoRefreshConfig.getEnv()+"username:"+gitAutoRefreshConfig.getUser().getUsername()+"password:"+gitAutoRefreshConfig.getUser().getPassword();
    }
}
package com.binson.userprovider.userprovider.controller;

import com.binson.pojo.entry.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: UserController
 * Description:
 *
 * @author binbin_hao
 * @date 2024/9/10 10:09
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping("/getUser")
    public User getUser(@RequestParam(value = "userId")String userId){
        User user= new User();
        user.setAge("18");
        user.setName("binbin");
        System.out.println("在我这里打印了出来");
        return user;
    }
}
package com.binson.userprovider.userprovider.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * ClassName: GiteeConfig
 * Description:
 *
 * @author binbin_hao
 * @date 2024/9/11 15:17
 */
@Data
@Component
public class GiteeConfig {
    @Value("${data.env}")
    private String env;

    @Value("${data.user.username}")
    private String username;

    @Value("${data.user.password}")
    private String password;
}

这几个类里面有用到了openfeign,以及config 配置中心的内容。

说到openfeign, 那么我们就开始编写openfeign 的配置

3,openfeign 组件

openfeign 我理解就是远程调用,只是他封装了你的调用方式,让你调用更简单一些,后面有了服务发现,才让你觉得抽象了起来,最原始的远程调用你还得输入远程服务的地址,但是有了服务发现,你不直接去使用远程的调用地址了,改用服务发现给你分配的地址,你只要连接到服务发现,拿着一把钥匙(服务id) ,问他我想大保健,服务发现看到你说的关键词了,立马从几个美女中给你挑了一个给你服务。如果没有服务发现,你还得自己去找美女,还不知道好看不好看,让你服务发现帮你找,一定是好看的,总结起来大概就是这个原理。

1,那么如果我们想里面服务发现来使用openfeign,那么我们就需要编写一个菜单,有了菜单我们才知道要什么服务。当然这个菜单需要大家共享,所以我们先制作一个共享的菜单。

这个菜单的就叫:userServiceClient ,因为他是被其他组件引用,所以我们的pom 文件中打包的时候就不要把启动类也打上去了,先说pom文件:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.binson</groupId>
    <artifactId>openClients</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>openClients</name>
    <description>服务注册demo</description>
    <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.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-web</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.binson</groupId>
            <artifactId>pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
                <configuration>
                    <mainClass>com.binson.openfegin.UserserviceApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

这里面的,会在打包的时候把启动类给跳过。

 <configuration>
                    <mainClass>com.binson.openfegin.UserserviceApplication</mainClass>
                    <skip>true</skip>
                </configuration>

接下来是启动类:

package com.binson.openfegin;

import com.binson.openfegin.clients.UserServiceClient;
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
@EnableFeignClients(clients = {UserServiceClient.class})
@EnableDiscoveryClient
public class UserserviceApplication {

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

}

菜单:

package com.binson.openfegin.clients;

import com.binson.openfegin.Fallback.UserServiceClientFallback;
import com.binson.pojo.entry.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * ClassName: UserServiceClient
 * Description:
 *
 * @author binbin_hao
 * @date 2024/9/10 8:37
 */
@FeignClient(name = "userprovider",fallback = UserServiceClientFallback.class)
public interface UserServiceClient {

    @GetMapping("/user/getUser")
    User getUser(@RequestParam("userId")String userId);


    @GetMapping("/order/hello")
    String helloOrder();
}

礼宾小姐:

package com.binson.openfegin.Fallback;

import com.binson.pojo.entry.User;
import com.binson.openfegin.clients.UserServiceClient;
import org.springframework.stereotype.Component;

/**
 * ClassName: UserServiceClientFallback
 * Description:
 *
 * @author binbin_hao
 * @date 2024/9/10 8:45
 */
@Component
public class UserServiceClientFallback implements UserServiceClient {

    @Override
    public User getUser(String id) {
        return new User();
    }

    @Override
    public String helloOrder() {
        return "hello Order 出错了! ";
    }
}

样貌截图:

这个包要打完,给需要大保健的人(消费者)用。

接下来就是消费者

4,消费者

我们先放项目截图吧:

接下来吧对应文件发上去,你自己玩

pom文件:




package com.binson.userservice.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * ClassName: GitAutoRefreshConfig
 * Description:
 *
 * @author binbin_hao
 * @date 2024/9/11 15:20
 */
@Component
@Data
@Configuration
@ConfigurationProperties(prefix = "data")
public class GitAutoRefreshConfig {
    public static class UserInfo {
        private String username;

        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "UserInfo{" +
                    "username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }

    private String env;

    private UserInfo user;
}
package com.binson.userservice.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * ClassName: GitAutoRefreshConfig
 * Description:
 *
 * @author binbin_hao
 * @date 2024/9/11 15:20
 */
@Component
@Data
@Configuration
@ConfigurationProperties(prefix = "data")
public class GitAutoRefreshConfig {
    public static class UserInfo {
        private String username;

        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "UserInfo{" +
                    "username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }

    private String env;

    private UserInfo user;
}
<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.binson</groupId>
    <artifactId>userservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>userservice</name>
    <description>open fegin 的消费端</description>
    <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.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.binson</groupId>
            <artifactId>pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.binson</groupId>
            <artifactId>openClients</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-amqp</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-bus</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</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>
                <configuration>
                    <mainClass>com.binson.userservice.UserserviceApplication</mainClass>
                 <!--   <skip>true</skip>-->
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

文章内容太长,请看第二章