本机IP为 192.168.1.102
1. 新建Maven 项目 ribbon
2. pom.xml
<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.java</groupId>
<artifactId>ribbon</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>${project.artifactId}</name>
<!-- 配置版本常量 -->
<properties>
<jdk.version>1.8</jdk.version>
<spring.cloud.version>2.0.0.RELEASE</spring.cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3. application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://192.168.1.102:8080/eureka/
#defaultZone: http://s0.com:8080/eureka/,http://s1.com:8080/eureka/,http://s2.com:8080/eureka/
4. RibbonStarter.java
package com.java.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class RibbonStarter extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(RibbonStarter.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(RibbonStarter.class);
}
}
5. ConfigBean.java
package com.java.ribbon.config;
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;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RetryRule;
@Configuration
public class ConfigBean {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@Bean
public IRule myRule() {
// return new RoundRobinRule();
// return new RandomRule();
return new RetryRule();
}
}
6. HostService.java
package com.java.ribbon.service;
import com.alibaba.fastjson.JSONObject;
public interface HostService {
JSONObject getHostMessage(String id);
}
7. HostServiceImpl.java
package com.java.ribbon.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
import com.java.ribbon.service.HostService;
@Service
@RibbonClient(name = "MICROSERVICE")
public class HostServiceImpl implements HostService {
/**
* Eureka中注册的微服务地址
*/
private static final String REST_URL_PREFIX = "http://MICROSERVICE";
@Autowired
private RestTemplate restTemplate;
@Override
public JSONObject getHostMessage(String id) {
return restTemplate.getForObject(REST_URL_PREFIX + "/getHostMessage/" + id, JSONObject.class);
}
}
8. HostController.java
package com.java.ribbon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.java.ribbon.service.HostService;
@RestController
public class HostController {
@Autowired
private HostService hostService;
@GetMapping("/getHostMessage/{id}")
public JSONObject getHostMessage(@PathVariable String id) {
return hostService.getHostMessage(id);
}
}
9. 运行测试
启动 Eureka 服务注册中心,参考 https://www.cnblogs.com/jonban/p/eureka.html
启动 MicroService 微服务, 参考 https://www.cnblogs.com/jonban/p/microservice.html
启动 Ribbon服务,运行 RibbonStarter.java
浏览器输入URL
http://192.168.1.102/getHostMessage/hello
返回数据如下:
{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}
截图如下:

调用微服务返回数据成功
原文:https://www.cnblogs.com/telwanggs/p/12620641.html