本机IP为 192.168.1.102
1. 新建Maven 项目 ribbon
2. pom.xml
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">
4.0.0
com.java
ribbon
1.0.0-SNAPSHOT
${project.artifactId}
1.8
2.0.0.RELEASE
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
${spring.cloud.version}
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
${spring.cloud.version}
com.alibaba
fastjson
1.2.49
org.springframework
springloaded
1.2.8.RELEASE
org.springframework.boot
spring-boot-devtools
${project.artifactId}
org.apache.maven.plugins
maven-compiler-plugin
${jdk.version}
${jdk.version}
UTF-8
org.springframework.boot
spring-boot-maven-plugin
repackage
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. 运行测试
启动 Ribbon服务,运行 RibbonStarter.java
浏览器输入URL
http://192.168.1.102/getHostMessage/hello
返回数据如下:
{"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"}
截图如下:
调用微服务返回数据成功