Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的开发便利性简化了分布式系统的开发,比如服务发现、服务网关、服务路由、链路追踪等。Spring Cloud 并不重复造轮子,而是将市面上开发得比较好的模块集成进去,进行封装,从而减少了各模块的开发成本。换句话说:Spring Cloud 提供了构建分布式系统所需的“全家桶”。
1、请求统一通过API网关(Zuul)来访问内部服务.
2、网关接收到请求后,从注册中心(Eureka)获取可用服务
3、由Ribbon进行均衡负载后,分发到后端具体实例
4、微服务之间通过Feign进行通信处理业务
5、Hystrix负责处理服务超时熔断
6、Turbine监控服务间的调用和熔断相关指标
3.1 一个web-app的maven工程。此处命名为:springcloud
3.2 在该工程下创建module。
3.3 勾上Eureka Server。以便自动导入包
3.4 点击next,创建项目后,检查相应的pom文件
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
<version>1.4.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>
3.5 在application中加入注解**@EnableEurekaServer**,申明此处为服务注册中心。
3.6 yml中的加入如下配置:registerWithEureka 和fetchRegistry 设置为false,表明自己属于服务中心主体
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.7 启动服务,登陆浏览器查看。http://localhost:8761/
4.1 检查一下pom文件,这里提供依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
<version>1.4.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
4.2 application中加入注解**@EnableEurekaClient**,表明自己属于一个生产者。
这里为了方便测试,直接使用**@RestController**获取返回值。
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication
{
public static void main(String[] args)
{
SpringApplication.run(ServiceHiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name)
{
return "hi " + name + ",i am from port:" + port;
}
}
4.3 yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8763
spring:
application:
name: service-hi
注意:端口不能与上面的相同。这里的服务name:service-hi 可以根据自己情况定义。
4.4 运行服务,登陆环境 http://localhost:8765/hi?name=fys
4.5 在8761这个端口中,也能看到,该信息已经在服务中心进行了注册。名字为我们yml中进行配置的名字。
5.1 创建消费者modul,流程如上述工程创建流程。
5.2 查看pom.xml文件
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.2.RELEASEversion>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>serviceribbonartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>serviceribbonname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<spring-cloud.version>Greenwich.RC2spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
<version>1.4.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-ribbonartifactId>
<version>1.4.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-hystrixartifactId>
<version>1.4.4.RELEASEversion>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
5.3 yml文件配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/ 。
5.4 编写启动类
@EnableDiscoveryClient表明标注类是消费者,加入restTemplate以消费相关的服务
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceribbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceribbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
}
}
6.1 service层:
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name)
{
return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
}
}
6.2 controller层:
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name)
{
return helloService.hiService(name);
}
}
6.3 运行服务,在浏览器中输入http://localhost:8764/hi?name=admin
equestMapping(value = "/hi")
public String hi(@RequestParam String name)
{
return helloService.hiService(name);
}
}
``
6.5 运行服务,在浏览器中输入http://localhost:8764/hi?name=admin
6.6输入http://localhost:8761/