springCloud+nacos實現feign調用

1. 服務端

將服務所需的api相關,放到ali nacos上,客戶端直接實現即可;

2. 客戶端

2.1 pom

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

2.2 application.yml

  # Nacos config
spring:
  cloud:
    nacos:
      discovery:
        # 服務中心地址
        server-addr: 111.22.333.444:8848
####Spring Cloud Server config
microservice:
  server:
    process:
      name: pfserver
      projectId: 0
      appId: 4
      status: OPEN
    auth:
      name: QESAuthCenterServer
#### feign
feign:
  client:
    config:
      default:
        connectTimeout: 20000
        readTimeout: 20000
  httpclient:
    connection-timeout: 20000
    connection-timer-repeat: 20000
    enabled: true
    max-connections: 200
    max-connections-per-route: 50

2.3 啓動類

添加這兩個註解:
@EnableDiscoveryClient
@EnableFeignClients

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*;

@EnableDiscoveryClient
@RestController
@EnableFeignClients
@EnableAsync @EnableScheduling @EnableTransactionManagement @EnableConfigurationProperties
@MapperScan({ "xxx" ,"yyy"})
@ServletComponentScan
@SpringBootApplication
public class Application {...}

2.4 實現服務中心接口

package com.leinovo.qes.portal.ms;

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

/**
 * @author xxx
 */
@FeignClient("QESAuthCenterServer")
public interface AuthenticationMicroService {

    /**
     * 通過id獲取用戶基礎信息
     * @param id
     * @return
     */
    @GetMapping(value = "/auth/user/{id}")
    String getUserById(@PathVariable(name = "id")  Long id);
}

2.5 使用feign

package com.leinovo.qes.portal.ms;

import com.leinovo.qes.platform.common.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: leimin
 * @Description: new class
 * @Date: 2020/6/18 14:18
 * @Version: 1.0
 */
@Slf4j
@Component
public class TestService {
    /**
     * 引入feign接口
     */
    @Resource
    private AuthenticationMicroService authenticationMicroService;

    /**
     * 
     * @param ids
     * @return
     */
    public List<User> getUsersById(Long id) {
        log.info("get users by id=[{}]", id);
        String json = authenticationMicroService.getUserById(id);
        return AuthenticationJsonParser.parseUsers(json);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章