搭建一个SpringCloud2.0,springcloud2.0和springcloud1.5的区别主要在于pom.xml文件中导包的版本,和application.properties中属性之间的细微差别(springcloud2.0比springcloud1.5会更加精细)
首先需要搭建一个注册服务中心:
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
serviceUrl:
######注册地址
defaultZonde: http://${eureka.instance.hostname}:${server.port}/eureka/
######因为自己是注册中心,是否需要将自己注册到自己的注册中心(集群的时候需要true)
register-with-eureka: false
######因为自己是注册中心,不需要去检索服务信息
fetch-registry: false
package com.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
//此注解用于将服务加载到注册中心
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
启动项目
将会员服务注册到服务中心
server:
port: 8801
spring:
application:
####服务的别名,注册到注册中心的service—id
name: eureka-member
eureka:
client:
serviceUrl:
######注册地址
defaultZone: http://localhost:8888/eureka/
######需要将服务注册到注册中
register-with-eureka: true
######需要去检索服务信息
fetch-registry: true
package com.eurekamember;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//将当前服务注册到eureka上
@EnableEurekaClient
public class EurekaMemberApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaMemberApplication.class, args);
}
}
package com.eurekamember.api.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemberController {
@RequestMapping("/getMember")
public String getMember(){
return "springboot2.0 springcloud";
}
}
订单服务
server:
port: 8802
spring:
application:
name: eureka-order
eureka:
client:
serviceUrl:
######注册地址
defaultZone: http://localhost:8888/eureka/
######需要将服务注册到注册中
register-with-eureka: true
######需要去检索服务信息
fetch-registry: true
package com.eureakorder;
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;
@SpringBootApplication
//将服务注册到注册中心
@EnableEurekaClient
public class EureakOrderApplication {
public static void main(String[] args) {
SpringApplication.run(EureakOrderApplication.class, args);
}
//将RestTemplate注册到容器中
//将rest模板注入到@Bean容器中
@Bean
//@LoadBalanced注解表示支持负载均衡,加上注解,默认启动负载均衡,对应在pom文件中ribbin
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
package com.eureakorder.api.controller;
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 OrderController {
@Autowired
//客户端调用工具,rest模板 RestTemplate是由SpringBoot web组件提供,默认整合ribbon负载均衡
//rest方式底层是采用httpclien技术
private RestTemplate restTemplate;
@RequestMapping("/getOrder")
public String getOrder(){
return "order 订单springboot2.0";
}
@RequestMapping("/getMember")
public String getMember(){
//两种调用方式:一种采用服务别名。另一种直接调用,如果使用服务别名就要使用ribbon负载均衡
String string=restTemplate.getForObject("http://eureka-member/getMember",String.class);
return string;
}
}