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

举报
小毕超 发表于 2022/06/24 00:35:04 2022/06/24
【摘要】 SpringBoot2.2.X及2.3.X 集成 SpringCloud 一、pom <!-- spring cloud eureka 注册中心--> <dependency>...

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

一、pom

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

<!--hystrix断路器-->
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>RELEASE</version>
</dependency>

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>RELEASE</version>
</dependency>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

二、注册中心配置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/

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

三、客户端配置application.properties

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

  
 
  • 1
  • 2
  • 3

四、调用远程接口

ResConfig

@Configuration
@Component
public class ResConfig {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

五、Service

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

  
 
  • 1
  • 2
  • 3

六、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<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("base64Str",base64);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(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";
    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

七、启动类

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

  
 
  • 1
  • 2
  • 3

文章来源: blog.csdn.net,作者:小毕超,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_43692950/article/details/107443824

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。