zoukankan      html  css  js  c++  java
  • spring cloud eureka 微服务之间的调用

    微服务之间的调用如何实现 

    首先 你需要两个或以上的微服务模块 至于怎么创建可以参考我上一篇博客  spring cloud eureka注册中心 

    如果想在页面显示 那么需要先加上

    compile  'org.springframework.boot:spring-boot-starter-thymeleaf'

    这个thymeleaf依赖  springboot推荐使用thymeleaf模板 它的最大好处就是原型即是模板 后缀是html 

    html文件 需要放在resources/templates文件夹下  因为thymeleaf自动配置的就是这个地址 当然也可以自己改

    还需要配置一个属性 

    spring:
      thymeleaf:
        cache: false #开发时关闭缓存 否则无法看到实时页面

    然后在html页面加上这个

     

    就可以使用thymeleaf模板了

    然后在消费端的启动类中 加上此方法

       @Bean // 自动扫描
        @LoadBalanced //这个注解的意思是在启动时先加载注册中心的域名列表 
        public RestTemplate restTemplate() //这个方法用来发http请求
        {
            RestTemplate restTemplate=new RestTemplate();
            return restTemplate;
        }

    看一下controller中的代码

        @Autowired
        private RestTemplate restTemplate; 
        @RequestMapping(value = "index")
        public String toIndex(Model model){
            String msg=restTemplate.getForEntity("http://PROJECT-POPPY-SOLR/search",String.class).getBody(); 
            model.addAttribute("msg",msg);
            return "index";
        }

    它的getForEntity方法中 传入的想要调用的方法以及它所在的地址 注意 这里不能直接写ip地址 必须写往注册中心注册过之后的项目名 要想直接写项目名必须在启动类上面的方法中加上@LoadBalaced注解

    否则ip地址如果发生变化 就需要更改 特别麻烦 作为一个优秀的程序员 当然是不能这么干的 

    然后把它放到model中发到页面 就可以调用另一个微服务的方法 实现了微服务间的调用

    还有一个调用的方法是feign 以后会讲解

  • 相关阅读:
    02-JavaScript语法
    001 docker基本环境的搭建
    023 虚拟机的安装
    022 虚拟机的网络配置
    021 虚拟机的安装
    004 java客户端的开发
    003 Rabbitmq中交换机的类型
    002 Rabbitmq的基础概念
    001 基础环境的搭建
    001 基本的环境的安装
  • 原文地址:https://www.cnblogs.com/wangkee/p/9305134.html
Copyright © 2011-2022 走看看