码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud微服务实现生产者消费者+ribbon负载均衡

时间:2019-12-17 18:54:19      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:client   adb   mic   pre   balancer   print   localhost   omr   vax   

一、生产者springcloud_eureka_provider

  (1)目录展示

    技术图片

  (2)导入依赖

    技术图片

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

      <!--eureka依赖-->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>

 <dependencyManagement>
        <dependencies>
            <!--springCloud依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  (3)配置文件application.yml

##应用名称
spring:
  application:
    name: eureka-provider
##声明当前eurekaservice的端口号
server:
  port: 8890

##配置eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka   

  (4)IDoSomeService

package com.zn.service;

public interface IDoSomeService {
    public String doSome();
}

  (5)IDoSomeServiceImpl

package com.zn.service.impl;

import com.zn.service.IDoSomeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class IDoSomeServiceImpl implements IDoSomeService {

    @RequestMapping("/doSome")

    @Override
    public String doSome() {
        System.out.println("服务提供者!");
        return "eureka到达页面hhhh";
    }
}

  (6)启动类

    技术图片

 

 

二、生产者springcloud_eureka_consumer

  (1)目录展示

    技术图片

 

 

   (2)导入依赖

    技术图片

<!--eureka依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

<dependencyManagement>
    <dependencies>
      <!--springCloud依赖-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

   (3)配置文件application.yml

##应用名称
spring:
  application:
    name: eureka-consumer
##声明当前eurekaservice的端口号
server:
  port: 8891

##配置eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka

  (4)IDoSomeController

package com.zn.controller;

import com.zn.service.IDoSomeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class IDoSomeController {

    @Resource
    RestTemplate restTemplate;

    /*@Resource
    IDoSomeService iDoSomeService;
*/
    @RequestMapping("/doSome")
    public String doSome(){
        System.out.println("ConsumerController");

        return restTemplate.getForObject("http://eureka-provider/doSome",String.class);
        //return iDoSomeService.doSome();
    }
}

  (5)StartEurekaConsumer启动类

package com.zn;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

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

    @Bean
    @LoadBalanced  //实现负载均衡
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

  (6)效果

    技术图片

 

    技术图片

 

 

 三、ribbon负载均衡

  (1)导入依赖

 <!--feign依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

(2)IDoSomeService

package com.zn.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("eureka-provider")
public interface IDoSomeService {
    @RequestMapping("/doSome")
    public String doSome();
}

  (3)启动类StartEurekaConsumer

package com.zn.controller;

import com.zn.service.IDoSomeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class IDoSomeController {

    //@Resource
    //RestTemplate restTemplate;

    @Resource
    IDoSomeService iDoSomeService;

    @RequestMapping("/doSome")
    public String doSome(){
        System.out.println("ConsumerController");

        //return restTemplate.getForObject("http://eureka-provider/doSome",String.class);
        return iDoSomeService.doSome();
    }
}

  (4)效果

    技术图片

    技术图片

 

 

     技术图片

 

 

 

  

SpringCloud微服务实现生产者消费者+ribbon负载均衡

标签:client   adb   mic   pre   balancer   print   localhost   omr   vax   

原文地址:https://www.cnblogs.com/Zzzzn/p/12055571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!