设为首页 加入收藏

TOP

SpringCloud(二) - Eureka注册中心,feign远程调用,hystrix降级和熔断(一)
2023-07-25 21:28:01 】 浏览:87
Tags:SpringCloud Eureka feign 程调用 hystrix

1、项目模块介绍

2、 父项目

主要依赖 spring-cloud 的 版本控制

<properties>
    <!--   springCloud 版本     -->
    <scd.version>Dalston.SR4</scd.version>
</properties>

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

3、 eureka 模块

3.1 主要依赖

<!--   eureka 注册中心 依赖     -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

3.2 配置信息

# 端口
server:
  port: 8096

# 服务名
spring:
  application:
    name: edocmall-eureka

# eureka 服务注册与发现 配置
eureka:
  client:
    #Eureka 监控页面
    service-url:
      defaultZone: http://127.0.0.1:${server.port}/eureka
    register-with-eureka: false # 是否注册自己的服务到注册中心,默认为true
    on-demand-update-status-change: false # 是否主动拉取其他注册的服务信息,默认也是true

3.3 主启动类上的注解

@EnableEurekaServer //eureka服务端启动,可以就接受别人来注册

3.4 测试 访问

启动项目,访问 http://localhost:8096

4、server 服务提供模块

4.1 主要依赖

<!--   eureka 客户端依赖     -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

4.2 配置信息

# 端口
server:
  port: 8097

# 服务名配置,eureka注册信息,服务调用基于服务名,必须增加
spring:
  application:
    name: edocmall-server
  #数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/kh96_springboot_edocbd?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
    username: root
    password: root

# mybatis-plus 配置
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true

# eureka 注册中心的配置
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8096/eureka  # 注册中心的地址
  # 关闭自我保护机制,保证不可用的服务及时剔除
  server:
    enable-self-preservation: false

4.3 代码介绍

4.4 主启动类上的注解

@MapperScan("com.kgc.scd.mapper")
@EnableEurekaClient // 开启 eureka 服务注册,将此服务注册到 eureka中

4.5 请求测试

服务提供端的请求最好先单独测试一下,成功后再进行远程调用;

5、 web 服务消费模块

5.1 使用restTemplate 调用

5.1.1 主启动类 向容器中放入 restTemplate

@SpringBootApplication
public class Edocmall96WebApplication {

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

    //往容器中添加 restTemplate 
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

5.1.2 请求中直接调用具体url下的请求

@RestController
public class WebEntryController {

    @Autowired
    private RestTemplate restTemplate;
    
     // 根据文档编号,获取文档详情
    @GetMapping("/entryById")
    public RequestResult<EdocEntryVO> entryDetail(Integer id){
        log.info("------ 根据文档编号:{},获取文档详情 ------",id);

        //模拟发送http请求请求server端,获取文档详情
        //弊端:消费端,必须在程序内,记录提供者的ip地址,如果地址出现变更,还需要计时更新,如果服务者有多个及其,无法实现负载均衡
        EdocEntryVO edocEntryVO = restTemplate.getForObject("http://127.0.0.1:8097/entry?id="+id,EdocEntryVO.class);

        return ResultBuildUtil.success(edocEntryVO);

    }
    
} 

5.1.3 请求测试

5.2 使用 feign 远程调用

5.2.1 主要依赖

<!--   eureka 客户端依赖     -->
<dep
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring Boot 并行任务,这才是优.. 下一篇记录一次非常麻烦的调试

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目