首先第一点,查看Eureka官方,看下springBoot与Eureeka版本问题。防止版本不兼容问题
道人最开始选择的版本为SpringBoot 2.3.1版本,springCloud 选 Hoxton.SR5版本,但是该版本选择,在道人本地创建父项目进行版本管理时,子项目依赖导入失败,故最后降低版本,解决了该问题。问题的原因暂时没找到,欢迎道友指点!
项目大致结构
目的:使用标签进行版本管理,避免版本冲突。
4.0.0
com.floatcloud
cloudlearn
0.0.1-SNAPSHOT
cloudlearn
pom
Demo project for Spring Boot
eureka-server8001
order-server9001
order-server9002
consumer-client7001
1.8
2.1.5.RELEASE
Greenwich.RELEASE
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
4.0.0
com.floatcloud
cloudlearn
0.0.1-SNAPSHOT
com.floatcloud
eureka-server8001
0.0.1-SNAPSHOT
eureka-server8001
jar
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
server:
port: 8001
eureka:
instance:
# 服务注册中心IP地址
hostname: localhost
client:
# 是否向服务注册中心注册自己
register-with-eureka: false
# 是否检索服务
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
package com.floatcloud.eurekaserver8001;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8001Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer8001Application.class, args);
}
}
这里展示的order-server9001模块代码,order-server9002与order-server9001模块代码相同。
cloudlearn
com.floatcloud
0.0.1-SNAPSHOT
4.0.0
com.floatcloud
order-server9001
0.0.1-SNAPSHOT
order-server9001
jar
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
#启动端口
server:
port: 9001
#项目名称
spring:
application:
name: order-provider
#eureka配置
eureka:
instance:
prefer-ip-address: true #使用服务的id地址注册
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8001/eureka/
fetch-registry: true
register-with-eureka: true
package com.floatcloud.orderclient9001;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* OrderClient9001Application
*
* @auther FloatCloud
* @date 2020/6/23
*/
@SpringBootApplication
@EnableEurekaClient
public class OrderClient9001Application {
public static void main(String[] args){
SpringApplication.run(OrderClient9001Application.class, args);
}
}
package com.floatcloud.orderclient9001.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* OrderController
*
* @auther FloatCloud
* @date 2020/6/23
*/
@RestController
public class OrderController {
@Value("${server.port}")
private String port;
@GetMapping("/order-server/create/order")
public String createOrder(){
return "端口:"+port+"产生订单";
}
}
cloudlearn
com.floatcloud
0.0.1-SNAPSHOT
4.0.0
com.floatcloud
consumer-client7001
0.0.1-SNAPSHOT
consumer-client7001
jar
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
#启动端口
server:
port: 7001
#项目名称
spring:
application:
name: order-consumer
#eureka配置
eureka:
instance:
prefer-ip-address: true #使用服务的id地址注册
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8001/eureka/
package com.floatcloud.consumerclient.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;
/**
* SysConfiguration
* 远程调用采用的是RestTemplate方式调用
* @LoadBalanced 该注解做负载均衡,详细会在道人博客中介绍
* @auther FloatCloud
* @date 2020/6/23
*/
@Configuration
public class SysConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
package com.floatcloud.consumerclient.controller;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* ConsumerController
*
* @auther FloatCloud
* @date 2020/6/23
*/
@RestController
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
/**
* 服务提供的地址:即服务提供方的spring.application.name 转大写
* 也可以理解为在Eureka服务注册中心,注册的application值。
*/
final String urlPath = "http://ORDER-PROVIDER";
@GetMapping("/consumer/make/order")
public String makeOrder(@RequestParam("name") String name){
String template = restTemplate.getForObject(urlPath + "/order-server/create/order", String.class);
return name + template;
}
}