前言:

简介:

工具:

ribbon 简介

一、准备工作

二、新建服务消费者 (eureka-ribbon)

2.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.lwc</groupId>
<artifactId>eureka-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-ribbon</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2.2  通过 application.yml 跳转 application-dev.yml 配置程序名, 端口 和指定注册中心 如下:

spring:
application:
name: eureka-ribbon
profiles:
active: ${@profileActiv@:dev}
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764

2.3  通过@EnableDiscoveryClient向服务中心注册, 并且利用ioc注入@Bean, 然后通过@LoadBalanced表明restTemplate()开启负载均衡功能; 如下:

package com.lwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* @author Eddie
*/
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication { public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}
}
package com.lwc.template;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; /**
* @author eddie.lee
* @Package com.lwc.template
* @ClassName RestConfig
* @description
* @date created in 2018-03-26 19:54
* @modified by
*/
@Component
public class RestConfig { @Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
} }

2.4 创建服务层测试使用, ribbonService() 里面url使用了eureka-client服务器名称代替127.0.0.1:8762; 如下:

package com.lwc.service;

/**
* @author eddie.lee
* @Package com.lwc.service
* @ClassName RibbonService
* @description
* @date created in 2018-03-26 19:55
* @modified by
*/
public interface RibbonService { String ribbonService(String name); }
package com.lwc.service.impl;

import com.lwc.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; /**
* @author eddie.lee
* @Package com.lwc.service.impl
* @ClassName RibbonServiceImpl
* @description
* @date created in 2018-03-26 19:57
* @modified by
*/
@Service
public class RibbonServiceImpl implements RibbonService { @Autowired
private RestTemplate restTemplate; @Override
public String ribbonService(String name) {
final String url = "http://eureka-client/eureka/client?name=" + name;
return restTemplate.getForObject(url, String.class);
} }

2.5 创建 controller 作为调用测试服务层使用; 如下:

package com.lwc.controller;

import com.lwc.service.RibbonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @author eddie.lee
* @Package com.lwc.controller
* @ClassName RibbonControler
* @description 服务消费者
* @date created in 2018-03-26 20:02
* @modified by
*/
@RestController
@RequestMapping("/eureka")
public class RibbonControler { @Autowired
private RibbonService ribbonService; @GetMapping("/ribbon")
public String Ribbon(@RequestParam String name) {
return ribbonService.ribbonService(name);
} }

eureka-ribbon
http://localhost:8764/eureka/ribbon?name=eddie

标签

2-1

源码下载
https://github.com/eddie-code/SpringCloudDemo#springclouddemo

04-27 07:42
查看更多