SpringCloud微服务学习
微服务
微服务就是将传统的一站式应用,根据业务拆分为一个个的服务,彻底地去耦合,每一个微服务提供单个业务功能的服务,一个服务做一件事。
从技术角度讲,就是一种小而独立的处理过程,类似进程概念,能够自行单独启动或销毁,拥有自己独立的数据库。
SpringCloud和Double的区别
Dubbo是通过rpc,一种远程调用来实现通讯,而Cloud通过restful来通讯
Eureka
Eureka服务提供者(EurekaClient)
pom.xml(中间有省略)
<dependencies>
<dependency>
<groupId>lamosesSpringCloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
主启动类
@SpringBootApplication
@EnableEurekaClient
public class DeptProvider8001_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_App.class, args);
}
}
MVC中的控制器
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping(value = "/dept/add")
public int add(@RequestBody Dept dept) {
return deptService.add(dept);
}
@GetMapping(value = "/dept/get/{id}")
public Dept get(@PathVariable("id") Integer id) {
System.out.println(id);
return deptService.get(id);
}
@GetMapping(value = "/dept/list")
public List<Dept> list() {
return deptService.list();
}
}
Eureka服务消费者
向Spring注册一个RestTemplate组件
@Configuration
public class ConfigBean {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Consumer消费者请求Controller
@RestController
public class DeptController_Consumer {
private static final String Url = "http://localhost:8001/dept";
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/consumer/dept/add")
public int add(Dept dept) {
return restTemplate.postForObject(Url+"/add", dept, Integer.class);
}
@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Integer id) {
return restTemplate.getForObject(Url+"/get/"+id, Dept.class);
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/consumer/dept/list")
public List<Dept> list() {
return restTemplate.getForObject(Url+"/list", List.class);
}
}
消费者模型作为客户端,无需SpringCloud的依赖,是一个普通的、只包含Controller的模块
EurekaServer注册中心(集群环境,AP原则)
主启动类
@SpringBootApplication
@EnableEurekaServer
public class eurekaServer7001_App {
public static void main(String[] args) {
SpringApplication.run(eurekaServer7001_App.class, args);
}
}
application.yml
server:
port: 7001 #服务运行端口
eureka:
instance:
hostname: eureka7001.com #通过hosts修改域名映射,如果不添加域名映射,会导致只有一个的情况(应该是重名了)
client:
register-with-eureka: false #声明自己是注册中心,不能作为clien注册到其他注册中心
fetch-registry: false #注册自己
service-url:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #注册地址
拷贝文件,修改域名和端口即可,其他无大修改,启动,多个注册中心会以集群的方式相互注册,集群就部署好了
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
消费者application.yml配置
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: microservicecloud-8001 #实例名ID
prefer-ip-address: true #Eureka注册中心页面显示IP
info: #info配置,以JSON对象返回给访问者
app.name: lamosemicroservicecloud
company.name: lamose
build.artifactId: $project.artifactId$
build.version: $project.version$
Eureka与Zookeeper的区别
Eureka遵从AP原则,Zookeeper遵从CP原则
分布式系统原则(CAP) , 遵从三选二原则
1、C : Consistency(强一致性)
2、A : Availability(可用性)
3、P : Partition tolerance(分区容错性)
Ribbon负载均衡
Ribbon其实就是一个软负载均衡的客户端组件,它可以和其他所需请求的就客户端结合使用,和Eureka结合只是其中的一个实例
1、添加Ribbon依赖
<!-- Ribbon相关开始 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Ribbon相关结束 -->
2、修改ConfigBean类
@Configuration
public class ConfigBean {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3、修改restTemplate地址
//修改为Eureka注册中心的服务名称(唯一)
private static final String Url = "http://MICROSERVICECLOUD-8001";
4、复制多份8001服务提供者,修改主启动类和application配置
server:
port: 8001
mybatis:
type-aliases-package: lamose.entities
mapper-locations:
- classpath:mybatis/mapper/*.xml
spring:
application:
name: microservicecloud-8001
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/coulddb01
username: root
password: 123456
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: microservicecloud-8001 #实例名ID
prefer-ip-address: true #Eureka注册中心页面显示IP
info: #info配置,以JSON对象返回给访问者
app.name: lamosemicroservicecloud
company.name: lamose
build.artifactId: $project.artifactId$
build.version: $project.version$
主要修改以上三处