设为首页 加入收藏

TOP

Spring Cloud开发实践(六): 基于Consul和Spring Cloud 2021.0的演示项目(三)
2023-07-25 21:24:08 】 浏览:53
Tags:Spring Cloud 基于 Consul 2021.0 项目
g.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-all</artifactId> </dependency> ... <dependency> <groupId>com.rockbb.test</groupId> <artifactId>dummy-common-api</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <finalName>dummy-common</finalName> <resources> ... </resources> <plugins> ... <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

配置部分 application.yml

  • 定义服务端口 8762
  • 定义 servlet 路径, 必须定义, 否则不会配置 Controller 请求
  • spring.application.name: dummy-common 定义了本服务的名称, 这个名称就是在 FeignClient 中引用的服务名称, 需要与 FeignClient 中的值一致
  • spring.config.import 如果使用这个设置, 依赖要使用 consul-all, 因为 consul-discovery 中不带 consul-config. 使用这个设置后, 会自动使用默认的 Consul 地址和端口
  • cloud.consul.host 和 port 如果使用了config.import, 在这里可以修改默认的值, 如果不使用config.import, 则必须配置 host 和 port, 依赖可以换成 consul-discovery
  • cloud.consul.discovery.health-check-path 用于更改默认的 health 检查请求路径, 默认的是 /actuator/health, 这里改为 /health
  • cloud.consul.discovery.instance-id 用于定义当前实例在 Consul 里的实例ID. 默认使用 application.name-port, 如果正好这个服务在两个服务器上分别跑了一个实例, 且实例端口一样, 就会产生冲突, 可以改为 application.name-[随机串] 的形式避免冲突
server:
  port: 8762
  tomcat:
    uri-encoding: UTF-8
  servlet:
    context-path: /

spring:
  application:
    name: dummy-common

  config:
    import: 'optional:consul:' #This will connect to the Consul Agent at the default location of "http://localhost:8500"

#  cloud:
#    consul:
#      host: 127.0.0.1
#      port: 8500
#      discovery:
#        health-check-path: /health # replace the default /actuator/health
#        instance-id: ${spring.application.name}:${random.value}

代码部分, 首先是实现 health 检查的处理方法, 这部分是普通的 RestController 方法. 返回字符串可以任意指定, 只要返回的 code 是 200 就可以

@RestController
public class HealthCheckServiceImpl {

    @GetMapping("/health")
    public String get() {
        return "SUCCESS";
    }
}

服务接口的实现类, 这里实现了两个接口方法 get 和 add

  • 使用 @RestController 注解, 与 API Service 中方法上的 @GetMapping 和 @PostMapping 配合, 将 Service 方法映射为 Controller 方法
  • 在类上的 @RequestMapping("userDTOService") 方法是必须的, 因为在 API Service 中与 @FeignClient 冲突无法定义, 只能在这里定义
  • 方法和参数上除了 @Override 不需要任何注解, 因为都在 API Service 上定义过了. 这里加上注解也没问题, 但是要手工保持一致.
@RestController
@RequestMapping("userDTOService")
public class UserDTOServiceImpl implements UserDTOService {

    @Autowired
    private UserRepo userRepo;

    @Override
    public UserDTO get(long id) {
        log.debug("Get user: {}", id);
        UserDTO user = new UserDTO();
        user.setId(id);
        user.setName("dummy");
        return user;
    }

    @Override
    public int add(UserDTO dto) {
        log.debug("Add user: {}", dto.getName());
        return 0;
    }
}

dummy-common 模块运行后会将接口注册到 Consul, 启动后注意观察两部分:

  1. Consul 的日志输出和控制面板显示, 在-dev模式下, 节点注册后 Consul 日志会显示模块的名称和心跳检测记录, 面板上会显示新的 Node
  2. Consul 控制面板中显示的 Health Checks 是否正常, 如果不正常, 需要检查 /health 路径为什么访问失败

Dummy Admin 模块

dummy-admin 是调用接口, 并对外提供服务的模块

pom.xml 和 dummy-common 基本一样, 因为都要连接 Co

首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring Boot 实现跨域的 5 种方式.. 下一篇利用简单的IO操作实现M3U8文件之..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目