springcloud⼦项⽬怎么导⼊_使⽤eclipse⼀步⼀步创建
SpringCloud。。。
⼀、创建服务提供者service-hi
1、创建⼦项⽬⽅法类似eureka,略
2、修改pom⽂件,主要是添加jar包
4.0.0
com.landcode
land
1.0.0-SNAPSHOT
land-service-hi
com.landcode
land-common
1.0.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
land-service-hi // jar包名
org.springframework.boot
spring-boot-maven-plugin
repackage
org.apache.maven.plugins
maven-compiler-plugin
utf-8
1.8
1.8
3、resource⽬录下添加配置⽂件l
server:
port: 8855
spring:
application:
name: service-hi
eureka:
instance:
prefer-ip-address: true #开启显⽰IP地址
instance-id: ${spring.cloud.client.ip-address}:${server.port} #eureka页⾯显⽰IP地址:端⼝号
client:
serviceUrl:
4、新建启动类,注意加上@EnableEurekaClient 注解,到eureka 注册服务
@SpringBootApplication
@EnableEurekaClientpublic classHiApplication {public static voidmain(String[] args) {
SpringApplication.run(HiApplication.class, args);
}
}
5、新建⼀个测试controller,⽅便服务调⽤⽅测试。测试功能中会返回服务⽅的端⼝号,⽅便做负载均衡测试
@RestControllerpublic classHiController {
@Value("${server.port}")privateString port;
@RequestMapping(value= "/hi")publicString hi() {return "hi " + port; //返回端⼝号,⽅便负载均衡功能测试
}
}
6、新建⼦项⽬service-consumer 作为服务调⽤⽅,⽅法类似eureka(略),修改pom⽂件,添加eureka、openfeign的jar包4.0.0
com.landcode
land
1.0.0-SNAPSHOT
land-service-consumer
com.landcode
land-common
1.0.0-SNAPSHOT
org.springframework.cloud怎么把项目导入到eclipse
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-openfeign
land-service-consumer
org.springframework.boot
spring-boot-maven-plugin
repackage
org.apache.maven.plugins
maven-compiler-plugin
utf-8
1.8
1.8
7、resource⽬录添加项⽬配置⽂件l
server:
port: 8866
spring:
application:
name: service-consumer
eureka:
instance:
prefer-ip-address: true #开启显⽰IP地址
instance-id: ${spring.cloud.client.ip-address}:${server.port} #eureka页⾯显⽰IP地址:端⼝号
client:
serviceUrl:
8、添加项⽬启动类,@EnableFeignClients 表⽰使⽤feign调⽤服务接⼝,Ribbonclient 注解表⽰采⽤ribbon做负载均
衡,configuration = RibbonConfig.class, RibbonConfig 是Ribbon 负载均衡机制配置类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= { "com.sumer.service"})
@RibbonClient(name= "service-hi", configuration = RibbonConfig.class)public classConsumerApplication {public static voidmain(String[] args) {//TODO Auto-generated method stub
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
@LoadBalancedpublicRestTemplate restTemplate() {return newRestTemplate();
}
}
9、RibbonConfig配置类,指定使⽤某种负载均衡策略
@Configurationpublic classRibbonConfig {
@Resource
IClientConfig clientConfig;
@BeanpublicIRule ribbonRule(IClientConfig clientConfig) {//return new RandomRule();//随机选择策略
return newWeightedResponseTimeRule();
}
}
10、新建service,调⽤服务service-hi,会⾃动采⽤ribbon的负载均衡策略
@FeignClient(value = "service-hi")public interfaceITestHi {
@RequestMapping("/hi")publicString testHi();
}
11、新建测试 controller,测试服务调⽤和负载均衡。这⾥注意:如果ITestHi还有实现类bean的话,注⼊时注意通过name区分@RestControllerpublic classTestHiController {
@AutowiredprivateITestHi testHi;
@RequestMapping("/consumer/hi")publicString testHi() {stHi();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。