使用Fegin+Consul

Feign:微服務之間互相調用
Consul:服務註冊中心

 		<!-- consul 服務註冊和發現功能 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!--  使用feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

關於Consul的yml語法

spring:
  cloud:
    consul:
      host: 127.0.0.1
      port: 8500
      discovery:
        register: true # 配置服務註冊到Consul上
        deregister: true
        healthCheckPath: ${server.servlet.context-path}/health # 配置服務健康檢查地址,供Consul調用。檢查默認爲“/ health”
        healthCheckInterval: 10s
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} # 註冊ID
        enabled: true # 不禁用Consul Discovery Client
        prefer-ip-address: false # true表示註冊時IP而不是hostname
        hostname: 127.0.0.1
        service-name: ${spring.application.name}

對於consul 的心跳檢查我寫在了spring Boot 的啓動文件中

@RestController
@SpringBootApplication
@MapperScan("mas.order.mapper")  //掃描mapper
@EnableFeignClients(basePackages = {"mas.order.service"})//掃描Feign
public class OrderApplication {

    static {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
    }

    @Value("${spring.cloud.client.ip-address}")
    String localIpAddress;

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

    @GetMapping("/health")
    public String check() {
        return "SUCCESS;ip:" + localIpAddress;
    }

}

在這裏插入圖片描述
關於Feign的yml語法

feign:
  client:
    config:
      default:
        connectTimeout: 60000
        readTimeout: 60000
        loggerLevel: BASIC
    directUrl: # 直連配置 :爲空時 使用註冊中心的服務;不爲空,使用配置的鏈接,
      storage:  http://localhost:8524
      pay: http://localhost:8522

在調用者的微服務中創建一個service接口 PayService
在這裏插入圖片描述

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

@FeignClient(value = "pay",
        name = "pay",
        url = "${feign.client.directUrl.pay}",
        primary = false)
public interface PayService {
    /**
     * 使用Feign時,請求路徑和客戶端調用一致,在yml寫好了ip和端口,在這裏寫服務名和詳細的請求路徑
     * @return "success"
     */
    @GetMapping("/pay/account/success")
    String success();
}

記得在使用這個Feign類的時候要在啓動類上面加註解@EnableFeignClients來掃描該路徑

下面是測試過程

//被調用者
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private IAccountService service;

    @GetMapping("/success")
    public String success() {
        return "success";
    }
}
//調用者 在業務類中調用創建在自己服務裏的PayService
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements IOrderService {

    @Autowired
    private PayService payService; 
    
    @Override
    public String successFeign() {
        return payService.success();
    }
}

結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章