美文网首页
spring cloud 之客户端调用使用(restTempla

spring cloud 之客户端调用使用(restTempla

作者: 心灵的震撼 | 来源:发表于2018-10-18 15:40 被阅读0次

    一.实现原理图如下:

    image

    二.实现的效果:

    当通过订单服务去调用会员服务A,或者调用会员服务B的时候,通过ribbon来进行实现,各占用50%的机制。

    三.代码实现:

    1.新建一个eurakaServer服务:

    POM.xml中的依赖:

           <dependency>
             <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
                <version>1.4.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- 添加euraka的权限校验 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
    

    2.新增配置文件application.yml

    #设置euraka-server访问时带有权限,不能直接访问
    security:
      basic:
        enabled: true
      user:
        name: zzh
        password: password123
    
    server:
      port: 8086
    #设置注册显示的实例名称为eureka-server1,如果不配置显示为UNKNOWN
    spring:
      application:
        name: eureka-server1
    eureka:
        #关闭Eureka的自我保护功能
        server:
          enable-self-preservation: false
          #配置Eureka Server清理无效节点的时间间隔
          eviction-interval-timer-in-ms: 4000
        instance:
          hostname: localhost
          #配置鼠标点击到某个服务上显示对应的IP地址
          prefer-ip-address: true
        client:
          register-with-eureka: false
          fetch-registry: false
          healthcheck:
            enabled: true
          serviceUrl:
            defaultZone: http://zzh:password123@${eureka.instance.hostname}:${server.port}/eureka/
    

    3.编写EurekaServer的启动类

    @SpringBootApplication
    @EnableEurekaServer
    public class EurakaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurakaServerApplication.class, args);
        }
    }
    

    编写完成后,启动我们的eureka的服务,然后再浏览器上进行查看,如果可以看到如下界面,代表注册中心已经起来了:


    image.png

    四.会员服务

    1.pom依赖

           <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
                <version>1.4.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

    2.配置文件的编写

    server:
      port: 8088
    spring:
      application:
        name: service-member
    eureka:
      instance:
        prefer-ip-address: true
      client:
        register-with-eureka: true
        healthcheck:
          enabled: true
        serviceUrl:
          defaultZone: http://zzh:password123@127.0.0.1:8086/eureka/
    

    3.编写会员服务的controller,为了展示方便,我们把对应的端口号返回给前端,方便查看。

    @RestController
    @RequestMapping("/member")
    public class MemberController {
    
        @Value("${server.port}")
        private String serverPort;
    
        @RequestMapping("/getAllMemberInfo")
        @ResponseBody
        public String getAllMemberInfo(){
            String str="这是会员服务A,对应的端口号为:"+serverPort;
            return str;
        }
    
    }
    

    4.编写会员服务的启动类

    @SpringBootApplication
    @EnableEurekaClient
    @ComponentScan(basePackages = "com.itshirui")
    public class ServiceMemberApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ServiceMemberApplication.class, args);
        }
    }
    

    启动成功后,我们再看eureka的界面会发现多了一个会员服务,界面如下:


    image.png

    接下来,我们再启动一个会员服务,更改一下端口即可,界面如下:


    image.png
    如此一来,会员服务我们已经全部完成,接下了,我们编写订单服务;

    五.会员服务

    1.pom.xml中的配置

         <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
                <version>1.4.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

    2.配置文件的编写

    server:
    port: 8087
    spring:
    application:
    name: service-order
    eureka:
    instance:
    prefer-ip-address: true
    client:
    register-with-eureka: true
    healthcheck:
    enabled: true
    serviceUrl:
    defaultZone: http://zzh:password123@127.0.0.1:8086/eureka/

    3.编写订单服务controller

    @RestController
    @RequestMapping("/order")
    public class OrderController {
    
        @Autowired
        private RestTemplate restTemplate;
    
        @RequestMapping("/getAllOrderInfo")
        @ResponseBody
        public String getAllOrderInfo(){
            String result = restTemplate.getForObject("http://service-member/member/getAllMemberInfo", String.class);
            return result;
        }
    }
    

    4.编写订单服务的启动类

    @SpringBootApplication
    @EnableEurekaClient
    @ComponentScan(basePackages = "com.itshirui")
    public class ServiceOrderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ServiceOrderApplication.class, args);
        }
    
    
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    
    
    }
    

    @LoadBalanced 注解表示使用负载的方式进行服务的调用。

    5.测试实际效果

    访问订单服务后,会出现


    image.png
    image.png

    6.虽然测试出现了负载的效果,但是中间如果有个服务断了会出现访问出现错误的情况,虽然也负载了,但是还是会出现负载到错误的请求的页面,怎么解决呢?

    下一篇文章会进行分解。

    相关文章

      网友评论

          本文标题:spring cloud 之客户端调用使用(restTempla

          本文链接:https://www.haomeiwen.com/subject/onejzftx.html