远程调用:RestTemplate
注册中心:eureka
负载均衡:Ribbon
org.springframework.cloud
spring-cloud-starter
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
server:
port: 10086
spring:
application:
name: eureka server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
1.2 技术关键词
spring-cloud-starter
spring-cloud-dependencies
spring-cloud-starter-netflix-eureka-server
spring-cloud-starter-netflix-eureka-client
spring-boot-starter-web
spring: application: name: eureka server
eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka/
@SpringBootApplication @EnableEurekaServer
@Bean @LoadBalanced public RestTemplate
@Autowired RestTemplate restTemplate;
@RestController
return "函数2"+restTemplate.getForObject(url,String.class);
SpringApplication.run(Main.class);
2 代码
2.1 父工程
4.0.0
com.xjc.springcloundtest
demo8
0.0.1-SNAPSHOT
untitled
21
21
UTF-8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.2.1 工程文件
4.0.0
com.xjc.springcloundtest
demo8
0.0.1-SNAPSHOT
untitled
21
21
UTF-8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.2.2 配置文件
server:
port: 10086
spring:
application:
name: eureka server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
2.2.3 主函数
package com.xjc.springcloundtest;
import com.netflix.discovery.shared.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
System.out.println("Hello world!");
}
}
2.2.4 运行效果
2.3 服务工程
2.2.1 工程文件
4.0.0
com.xjc.springcloundtest
demo8
0.0.1-SNAPSHOT
untitled1
21
21
UTF-8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
2.3.2 配置文件
spring:
application:
name: server1
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
2.3.3 主函数
package com.xjc.springcloundtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
System.out.println("Hello world!");
}
}
2.3.4 控制器
package com.xjc.springcloundtest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/fun")
public String fun(){
return "函数1";
}
}
4.0.0
com.xjc.springcloundtest
demo8
0.0.1-SNAPSHOT
untitled2
21
21
UTF-8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
2.4.2 配置文件
server:
port: 8081
spring:
application:
name: server2
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
2.4.3 主函数
package com.xjc.springcloundtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
System.out.println("Hello world!");
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder ){
return builder.build();
}
}
2.4.4 消费者
package com.xjc.springcloundtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/fun")
public String fun(){
//String url = "http://localhost:8080/fun";
String url = "http://server1/fun";
return "函数2"+restTemplate.getForObject(url,String.class);
}
}
2.4.5 运行效果