SpringBoot2.2.X及2.3.X 集成 SpringCloud

SpringBoot2.2.X及2.3.X 集成 SpringCloud

一、pom

 
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        

        
        
            com.netflix.hystrix
            hystrix-javanica
            RELEASE
        

        
            com.netflix.hystrix
            hystrix-core
            RELEASE
        
        
 
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR1
                pom
                import
            
        
    

二、注册中心配置application.properties

#springCloud 注册中心配置
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

三、客户端配置application.properties

#springCloud 注册客户端配置
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka/
spring.application.name = FrameServer

四、调用远程接口

ResConfig

@Configuration
@Component
public class ResConfig {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

五、Service

public interface RpcService {
    DetectionInfo FaceInfoDatection(String base64);
}

六、impl

@Service
public class RpcServiceImpl implements RpcService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    Gson gson;

    @Override
    public DetectionInfo FaceInfoDatection(String base64) {
        String data = GetFaceInfo(base64);
        if (data != null || !data.equals("err")){
            DetectionInfo detectionInfo = gson.fromJson(data,DetectionInfo.class);
            return detectionInfo;
        }
        return null;
    }



    @HystrixCommand(fallbackMethod = "error")
    public String GetFaceInfo(String base64){
        MultiValueMap map= new LinkedMultiValueMap();
        map.add("base64Str",base64);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity> request = new HttpEntity>(map, headers);
        return restTemplate.postForObject("http://AISERVER/FaceInfoDatection",request,String.class);
        return null;
    }

    public String error(String name){
        return "err";
    }

    public String error(){
        return "err";
    }
}

七、启动类

//@EnableEurekaClient 开启客户端
//@EnableEurekaServer 开启注册中心
//@EnableHystrix 开启断路器

你可能感兴趣的:(分布式,eureka)