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

一、生产者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)效果

    

    

     

  

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