spring cloud

单体应⽤存在的问题
随着业务的发展,开发变得越来越复杂。
修改、新增某个功能,需要对整个系统进⾏测试、重新部署。
⼀个模块出现问题,很可能导致整个系统崩溃。
多个开发团队同时对数据进⾏管理,容易产⽣安全漏洞。
各个模块使⽤同⼀种技术进⾏开发,各个模块很难根据实际情况选择更合适的技术框架,局限性很
⼤。
模块内容过于复杂,如果员⼯离职,可能需要很⻓时间才能完成⼯作交接。
分布式、集群
集群:⼀台服务器⽆法负荷⾼并发的数据访问量,那么就设置⼗台服务器⼀起分担压⼒,⼗台不⾏就设
置⼀百台(物理层⾯)。很多⼈⼲同⼀件事情,来分摊压⼒。
分布式:将⼀个复杂问题拆分成若⼲个简单的⼩问题,将⼀个⼤型的项⽬架构拆分成若⼲个微服务来协
同完成。(软件设计层⾯)。将⼀个庞⼤的⼯作拆分成若⼲个⼩步骤,分别由不同的⼈完成这些⼩步
骤,最终将所有的结果进⾏整合实现⼤的需求。
服务治理的核⼼⼜三部分组成:服务提供者、服务消费者、注册中⼼。
在分布式系统架构中,每个微服务在启动时,将⾃⼰的信息存储在注册中⼼,叫做服务注册。
服务消费者从注册中⼼获取服务提供者的⽹络信息,通过该信息调⽤服务,叫做服务发现。
Spring Cloud 的服务治理使⽤ Eureka 来实现,Eureka 是 Netflix 开源的基于 REST 的服务治理解决⽅
案,Spring Cloud 集成了 Eureka,提供服务注册和服务发现的功能,可以和基于 Spring Boot 搭建的
微服务应⽤轻松完成整合,开箱即⽤,Spring Cloud Eureka。
Spring Cloud Eureka
Eureka Server,注册中⼼
Eureka Client,所有要进⾏注册的微服务通过 Eureka Client 连接到 Eureka Server,完成注册。
Eureka Server代码实现
创建⽗⼯程,pom.xml
org.springframework.boot spring-boot-starter-parent 2.0.7.RELEASE
org.springframework.boot spring-boot-starter-web

javax.xml.bind jaxb-api 2.3.0
com.sun.xml.bind jaxb-impl 2.3.0
com.sun.xml.bind jaxb-core 2.3.0
javax.activation activation 1.1.1

org.springframework.cloud spring-cloud-dependencies Finchley.SR2 pom import



在⽗⼯程下创建 Module,pom.xml
org.springframework.cloud spring-cloud-starter-netflix-eureka-server 2.0.2.RELEASE


创建配置⽂件 application.yml,添加 Eureka Server 相关配置。
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
属性说明
server.port :当前 Eureka Server 服务端⼝。
eureka.client.register-with-eureka :是否将当前的 Eureka Server 服务作为客户端进⾏注册。
eureka.client.fetch-fegistry :是否获取其他 Eureka Server 服务的数据。
eureka.client.service-url.defaultZone :注册中⼼的访问地址。
创建启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
注解说明:
@SpringBootApplication :声明该类是 Spring Boot 服务的⼊⼝。
@EnableEurekaServer :声明该类是⼀个 Eureka Server 微服务,提供服务注册和服务发现功能,即
注册中⼼。
Eureka Client 代码实现
创建 Module ,pom.xml
org.springframework.cloud spring-cloud-starter-netflix-eureka-client 2.0.2.RELEASE


创建配置⽂件 application.yml,添加 Eureka Client 相关配置
server:
port: 8010
spring:
application:
name: provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
属性说明:
spring.application.name :当前服务注册在 Eureka Server 上的名称。
eureka.client.service-url.defaultZone :注册中⼼的访问地址。
eureka.instance.prefer-ip-address :是否将当前服务的 IP 注册到 Eureka Server。
创建启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
实体类
package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private long id;
private String name;
private int age; }
Repository
package com.southwind.repository;
import com.southwind.entity.Student;
import java.util.Collection;
public interface StudentRepository {
public Collection findAll();
public Student findById(long id);
public void saveOrUpdate(Student student);
public void deleteById(long id);
}
RepositoryImpl
package com.southwind.repository.impl;
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class StudentRepositoryImpl implements StudentRepository {
private static Map<Long,Student> studentMap;
static {
studentMap = new HashMap<>();
studentMap.put(1L,new Student(1L,“张三”,22));
studentMap.put(2L,new Student(2L,“李四”,23));
studentMap.put(3L,new Student(3L,“王五”,24));
}
@Override
public Collection findAll() {
return studentMap.values();
}
@Override
public Student findById(long id) {
return studentMap.get(id);
}
@Override
public void saveOrUpdate(Student student) {
studentMap.put(student.getId(),student);
}
@Override
public void deleteById(long id) {
studentMap.remove(id);
}
}
Handler
package com.southwind.controller;
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.;
import java.util.Collection;
@RestController
@RequestMapping("/student")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@GetMapping("/findAll")
public Collection findAll(){
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable(“id”) long id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable(“id”) long id){
studentRepository.deleteById(id);
}
}
RestTemplate 的使⽤
什么是 RestTemplate?
RestTemplate 是 Spring 框架提供的基于 REST 的服务组件,底层是对 HTTP 请求及响应进⾏了封装,
提供了很多访问 RETS 服务的⽅法,可以简化代码开发。
如何使⽤ RestTemplate? 1、创建 Maven ⼯程,pom.xml。 2、创建实体类
package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private long id;
private String name;
private int age; } 3、Handler
package com.southwind.controller;
import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.
;
import org.springframework.web.client.RestTemplate;
import java.util.Collection;
@RestController
@RequestMapping("/rest")
public class RestHandler {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/findAll")
public Collection findAll(){
return
restTemplate.getForEntity(“http://localhost:8010/student/findAll”,Collection.c
lass).getBody();
}
@GetMapping("/findAll2")
public Collection findAll2(){
return
restTemplate.getForObject(“http://localhost:8010/student/findAll”,Collection.c
lass);
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable(“id”) long id){
return
restTemplate.getForEntity(“http://localhost:8010/student/findById/{id}”,Studen
t.class,id).getBody();
}
@GetMapping("/findById2/{id}")
public Student findById2(@PathVariable(“id”) long id){
return
restTemplate.getForObject(“http://localhost:8010/student/findById/{id}”,Studen
t.class,id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
restTemplate.postForEntity(“http://localhost:8010/student/save”,student,null) .getBody();
}
@PostMapping("/save2")
public void save2(@RequestBody Student student){
restTemplate.postForObject(“http://localhost:8010/student/save”,student,null) ;
}
@PutMapping("/update")
public void update(@RequestBody Student student){
restTemplate.put(“http://localhost:8010/student/update”,student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable(“id”) long id){
restTemplate.delete(“http://localhost:8010/student/deleteById/{id}”,id);
}
} 4、启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RestTemplateApplication {
public static void main(String[] args) {
SpringApplication.run(RestTemplateApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
服务消费者 consumer
创建 Maven ⼯程,pom.xml


org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.2.RELEASE


创建配置⽂件 application.yml
server:
port: 8020
spring:
application:
name: consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
创建启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
Handler
package com.southwind.controller;
import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.;
import org.springframework.web.client.RestTemplate;
import java.util.Collection;
@RestController
@RequestMapping("/consumer")
public class ConsumerHandler {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/findAll")
public Collection findAll(){
return
restTemplate.getForEntity(“http://localhost:8010/student/findAll”,Collection.c
lass).getBody();
}
@GetMapping("/findAll2")
public Collection findAll2(){
return
restTemplate.getForObject(“http://localhost:8010/student/findAll”,Collection.c
lass);
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable(“id”) long id){
return
restTemplate.getForEntity(“http://localhost:8010/student/findById/{id}”,Studen
t.class,id).getBody();
}
@GetMapping("/findById2/{id}")
public Student findById2(@PathVariable(“id”) long id){
return
restTemplate.getForObject(“http://localhost:8010/student/findById/{id}”,Studen
t.class,id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
restTemplate.postForEntity(“http://localhost:8010/student/save”,student,null) .getBody();
}
@PostMapping("/save2")
public void save2(@RequestBody Student student){
restTemplate.postForObject(“http://localhost:8010/student/save”,student,null) ;
}
@PutMapping("/update")
public void update(@RequestBody Student student){
restTemplate.put(“http://localhost:8010/student/update”,student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable(“id”) long id){
restTemplate.delete(“http://localhost:8010/student/deleteById/{id}”,id);
}
}
服务⽹关
Spring Cloud 集成了 Zuul 组件,实现服务⽹关。
什么是 Zuul?
Zuul 是 Netflix 提供的⼀个开源的 API ⽹关服务器,是客户端和⽹站后端所有请求的中间层,对外开放
⼀个 API,将所有请求导⼊统⼀的⼊⼝,屏蔽了服务端的具体实现逻辑,Zuul 可以实现反向代理的功
能,在⽹关内部实现动态路由、身份认证、IP 过滤、数据监控等。
创建 Maven ⼯程,pom.xml


org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.2.RELEASE


org.springframework.cloud
spring-cloud-starter-netflix-zuul
2.0.2.RELEASE


创建配置⽂件 application.yml
server:
port: 8030
spring:
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
provider: /p/**
属性说明:
zuul.routes.provider :给服务提供者 provider 设置映射
创建启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@EnableAutoConfiguration
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class,args);
}
}
注解说明:
@EnableZuulProxy :包含了 @EnableZuulServer ,设置该类是⽹关的启动类。
@EnableAutoConfiguration :可以帮助 Spring Boot 应⽤将所有符合条件的 @Configuration 配
置加载到当前 Spring Boot 创建并使⽤的 IoC 容器中。
Zuul ⾃带了负载均衡功能,修改 provider 的代码。
package com.southwind.controller;
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.
;
import java.util.Collection;
@RestController
@RequestMapping("/student")
public class StudentHandler {
@Autowired
private StudentRepository studentRepository;
@Value("KaTeX parse error: Expected 'EOF', got '}' at position 695: …+this.port; } }̲ Ribbon 负载均衡 什么…{server.port}")
private String port;
@Value("KaTeX parse error: Expected 'EOF', got '}' at position 112: …"+this.foo; } }̲ Spring Cloud C…{server.port}")
private String port;
@GetMapping("/index")
public String index(){
return this.port;
}
}
服务跟踪
Spring Cloud Zipkin
Zipkin 是⼀个可以采集并且跟踪分布式系统中请求数据的组件,让开发者可以更加直观的监控到请求在
各个微服务所耗费的时间等,Zipkin:Zipkin Server、Zipkin Client。
创建 Zipkin Server
创建 Maven ⼯程,pom.xml
org.springframework.boot spring-boot-starter-web
io.zipkin.java zipkin-server 2.9.4
io.zipkin.java zipkin-autoconfigure-ui 2.9.4


创建配置⽂件 application.yml
server:
port: 9090
创建启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.internal.EnableZipkinServer;
@SpringBootApplication
@EnableZipkinServer
public class ZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinApplication.class,args);
}
}
注解说明
@EnableZipkinServer :声明启动 Zipkin Server
创建 Zipkin Client
创建 Maven ⼯程,pom.xml


org.springframework.cloud
spring-cloud-starter-zipkin
2.0.2.RELEASE


创建配置⽂件 application.yml
server:
port: 8090
spring:
application:
name: zipkinclient
sleuth:
web:
client:
enabled: true
sampler:
probability: 1.0
zipkin:
base-url: http://localhost:9090/
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
属性说明
spring.sleuth.web.client.enabled :设置开启请求跟踪
spring.sleuth.sampler.probability :设置采样⽐例,默认是 1.0
srping.zipkin.base-url :Zipkin Server 地址
创建启动类
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZipkinClientApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinClientApplication.class,args);
}
}
Handler
package com.southwind.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/zipkin")
public class ZipkinHandler {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index(){
return this.port;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值