SpringCloud 备忘 3 - Ribbon 风格消费者 Consumer Ribbon 服务搭建

1、在父工程 “microservicecloud” 下新建一个 Module,名称为 “consumer-robbin-8401”

2、修改 pom.xml 文件



    
        microservicecloud
        com.lakey.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    consumer-ribbon-8401
    consumer-ribbon-8401
    Consumer With Ribbon 8401

    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        
    

3、在 java 路径下创建目录 “com.lakey.springcloud” 并添加启动类 ConsumerRibbon8401Application.java

package com.lakey.springcloud;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient // 本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient // 开启服务发现,寻找服务提供者
public class ConsumerRibbon8401Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ConsumerRibbon8401Application.class);
        application.setBannerMode(Banner.Mode.OFF);// 不输出Banner
        application.run(args);
        System.out.println(" ____    ___   ____    ____     ___    _   _    ___    _  _      ___    _ \n" +
                " |  _ \\  |_ _| | __ )  | __ )   / _ \\  | \\ | |  ( _ )  | || |    / _ \\  / |\n" +
                " | |_) |  | |  |  _ \\  |  _ \\  | | | | |  \\| |  / _ \\  | || |_  | | | | | |\n" +
                " |  _ <   | |  | |_) | | |_) | | |_| | | |\\  | | (_) | |__   _| | |_| | | |\n" +
                " |_| \\_\\ |___| |____/  |____/   \\___/  |_| \\_|  \\___/     |_|    \\___/  |_|");
    }

}

4、在 resources 路径下添加配置文件 application.yml

# 服务器配置
server:
  port: 8401

# Spring 配置
spring:
  application:
    name: consumer-ribbon

# Eureka 配置
eureka:
  client: # 客户端注册进 Eureka 服务列表
    serviceUrl:
      defaultZone: http://localhost:8101/eureka/
  instance:
      instance-id: consumer-ribbon-8401   # 自定义服务名称信息
      prefer-ip-address: true     # 访问路径可以显示 IP 地址

# Eureka 微服务详细信息配置
info:
  app.name: consumer-ribbon-8401
  company.name: www.lakey.com
  build.artifactId: microservicecloud
  build.version: 1.0-SNAPSHOT

5、在目录 “com.lakey.springcloud” 下创建一个配置文件夹 “cfgbeans” 并添加配置类 ConfigBean.java

package com.lakey.springcloud.cfgbeans;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * 服务配置类
 *
 * @author liming
 * @date 2019-05-07
 */
@Configuration
public class ConfigBean {

    @Bean
    @LoadBalanced // 使用 Ribbon 自带的负载均衡,默认负载方式为轮询
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

6、在目录 “com.lakey.springcloud” 下创建一个服务层文件夹 “service” 并添加服务调用类 HelloService.java 请求生产者服务接口

package com.lakey.springcloud.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 服务调用类
 *
 * @author liming
 * @date 2019-05-07
 */
@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 调用 Eureka 服务列表下指定名称的服务方法
     *
     * @param name
     * @return
     */
    public String sayHello(String name) {
        return restTemplate.getForObject("http://provider/hello?name=" + name, String.class);
    }
}

7、在目录 “com.lakey.springcloud” 下创建一个控制层文件夹 “controller” 并添加控制类 HelloController.java 对用户暴露接口

package com.lakey.springcloud.controller;

import com.lakey.springcloud.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试控制器
 *
 * @author liming
 * @date 2019-05-07
 */
@RestController
public class HelloController {

    @Autowired(required = false)
    private HelloService helloService;

    /**
     * 测试入口
     * 
     * @param name
     * @return
     */
    @GetMapping(value = "/hello")
    public String sayHello(@RequestParam String name) {
        return helloService.sayHello(name);
    }
}

8、目录结构截图:

SpringCloud 备忘 3 - Ribbon 风格消费者 Consumer Ribbon 服务搭建_第1张图片

 

9、程序运行截图:

SpringCloud 备忘 3 - Ribbon 风格消费者 Consumer Ribbon 服务搭建_第2张图片

SpringCloud 备忘 3 - Ribbon 风格消费者 Consumer Ribbon 服务搭建_第3张图片

 

10、参考文章:

https://github.com/forezp/SpringCloudLearning

11、码云源码:

https://gitee.com/nangongyanya/microservicecloud

你可能感兴趣的:(--Spring,Cloud)