本项目还是通过卖票和买票模块来介绍spring cloud。
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.19.RELEASE
com.laoxu
eureka-server
0.0.1-SNAPSHOT
eureka-server
Demo project for Spring Boot
1.8
Edgware.SR5
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
server: port: 8761 eureka: instance: hostname: eureka-server client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8761/eureka/
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.19.RELEASE
com.laoxu
provider-ticket
0.0.1-SNAPSHOT
provider-ticket
Demo project for Spring Boot
1.8
Edgware.SR5
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
server: port: 8002 spring: application: name: provider-ticket eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/
package com.laoxu.providerticket.service;
import org.springframework.stereotype.Service;
@Service
public class TicketService {
public String getTicket(){
System.out.println("8002"); //自定义输出
return "《厉害了,我的国》";
}
}
package com.laoxu.providerticket.controller;
import com.laoxu.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TicketController {
@Autowired
TicketService ticketService;
@GetMapping("/ticket")
public String getTicket(){
return ticketService.getTicket();
}
}
1)修改getTicket方法中输出端口和yml文件端口为8001,并用maven打包,重命名文件为:
provider-ticket-0.0.1-SNAPSHOT-8001.jar
2)重复1)中步骤端口改为8002再打包为
provider-ticket-0.0.1-SNAPSHOT-8002.jar
如何打包?
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.19.RELEASE
com.laoxu
consumer-user
0.0.1-SNAPSHOT
consumer-user
Demo project for Spring Boot
1.8
Edgware.SR5
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring: application: name: consumer-user server: port: 8200 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/
package com.laoxu.consumeruser;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableEurekaClient //开启发现服务功能
@SpringBootApplication
public class ConsumerUserApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerUserApplication.class, args);
}
@LoadBalanced //开启负载均衡
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
package com.laoxu.consumeruser.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/buy")
public String buyTicket(String name){
String ticket = restTemplate.getForObject("http://provider-ticket/ticket", String.class);
return name+"购买了:"+ticket;
}
}
java -jar provider-ticket-0.0.1-SNAPSHOT-8001.jar
java -jar provider-ticket-0.0.1-SNAPSHOT-8002.jar
刷新注册中心,可以看到provider-ticket和consumer-user两个Application
多次刷新,观察provider-ticket的两个应用控制台日志。
可以看到4次访问时平均分配到2个应用上的,因此实现了请求负载均衡。