springCloud--1

电影微服务是服务消费者,用户微服务是服务提供者。
Springcloud对eureka的支持很好,eureka本身是一个基于REST的服务,



Eureka里面有一个注册表,Application Client是服务消费者,Application Service是服务提供者。Make Remote Call是远程调用,us-east-1c,us-east-1d,us-east-1e是zone,他们都属于us-east-1这个region。他们都是Eureka Client。
服务提供者(Application Service)和服务消费者(Application Client)启动的时候都会像eureka注册(ip和端口),他们更eureka server有一个心跳机制默认30秒,连续3次90秒没有收到心跳就会从注册表移除这个节点。Eureka service集群之间会相互复制服务注册表。
服务提供者的ip和端口变化了,服务注册表会更新ip和端口。
eureka client缓存:即使所有的eureka service都宕机了,服务消费者也会知道服务提供者的ip和端口进行调用。
服务注册表就是一个数据库,用来记录ip和端口,并且提供API操作服务注册表,把服务添加到注册表称之为注册从注册表移除称之为注销。
心跳机制是健康检查机制。
服务发现方式:客户端发现(Eureka,zk),服务端发现(Consul+nginx)。http://blog.daocloud.io/microservices-4/ SpringCloud对Eureka支持最完整。
Eureka:服务发现组件。

服务消费者:
package com.itmuch.cloud; import org.springframework.beans.factory.annotation.Autowired;
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 MicroserviceSimpleConsumerMovieApplication { @Bean //实例化了一个对象new RestTemplate,方法的名字rt就是实例化后bean的名字,其他地方通过@Autowired引用
public RestTemplate rt() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);
}
}
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.itmuch.cloud.entity.User; @RestController
public class MovieController { @Autowired //初始化是启动类上面的@bean注解
private RestTemplate rt;//使用RestTemplate调用 //user: userServicePath: http://localhost:7900/simple/
//从yml文件读值.这里的ip和端口是写死的,有的时候服务提供者的ip和端口是变化的。
//如果有多个提供者,如何做负载(nginx是可以做转发的,nginx通过url转发到不同的微服务),使用eureka(客户端发现机制)。
@Value("${user.userServicePath}")
private String userServicePath; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
return this.rt.getForObject(this.userServicePath + id, User.class);
}
}
package com.itmuch.cloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
yml: server:
port: 7901
user:
userServicePath: http://localhost:7900/simple/
pom.xml <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-simple-consumer-movie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>microservice-simple-consumer-movie</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
服务提供者:
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; //服务提供者
@SpringBootApplication
public class MicroserviceSimpleProviderUserApplication { public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
}
}
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.repository.UserRepository; @RestController//相当于加了@responseBody和@controller
public class UserController { @Autowired
private UserRepository userRepository; @GetMapping("/simple/{id}") //@GetMapping是@requestMapping(method=get)
public User findById(@PathVariable Long id) {
return this.userRepository.findOne(id);
}
}
package com.itmuch.cloud.entity; import java.math.BigDecimal; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; @Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id; @Column
private String username; @Column
private String name; @Column
private Short age; @Column
private BigDecimal balance; public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return this.username;
} public void setUsername(String username) {
this.username = username;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Short getAge() {
return this.age;
} public void setAge(Short age) {
this.age = age;
} public BigDecimal getBalance() {
return this.balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
package com.itmuch.cloud.repository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import com.itmuch.cloud.entity.User; //DAO
@Repository
public interface UserRepository extends JpaRepository<User, Long> { }
yml server:
port: 7900
spring:
jpa:
generate-ddl: false #启动的时候不打印sql语句
show-sql: true #显示sql语句
hibernate: #依赖hibernate
ddl-auto: none #ddl语句的处理
datasource: #数据源
platform: h2 #h2数据库
schema: classpath:schema.sql #建表语句
data: classpath:data.sql #数据
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE #把参数打印出来
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
com.itmuch: DEBUG
data.sql:
insert into user(id,username, name, age, balance) values(1,'user1', '寮犱笁', 20, 100.00);
insert into user(id,username, name, age, balance) values(2,'user2', '鏉庡洓', 20, 100.00);
insert into user(id,username, name, age, balance) values(3,'user3', '鐜嬩簲', 20, 100.00);
insert into user(id,username, name, age, balance) values(4,'user4', '椹叚', 20, 100.00);
schema.sql
drop table user if exists;
create table user(
id bigint generated by default as identity,
username varchar(40),
name varchar(20),
age int(3),
balance decimal(10,2),
primary key(id)
);
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-simple-provider-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>microservice-simple-provider-user</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
springCloud--1的更多相关文章
- 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- springcloud(第三篇)springcloud eureka 服务注册与发现 *****
http://blog.csdn.net/liaokailin/article/details/51314001 ******************************************* ...
- SpringCloud Sleuth 使用
1. 介绍 Spring-Cloud-Sleuth是Spring Cloud的组成部分之一,为SpringCloud应用实现了一种分布式追踪解决方案,其兼容了Zipkin, HTrace和log- ...
- SpringCloud+Consul 服务注册与服务发现
SpringCloud+Consul 服务注册与服务发现 1. 服务注册: 在Spring.factories有一段: # Discovery Client Configuration org.spr ...
- SpringCloud学习后获取的地址
关于SpringCloud + Docker 学习地址: (1) https://yq.aliyun.com/articles/57265 (2) https://yq.aliyun.com/team ...
- SpringCloud网关ZUUL集成consul
最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...
- springcloud(一):大话Spring Cloud
研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...
- springcloud(二):注册中心Eureka
Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现.也是springcloud体系中最重要最核心的组 ...
- springcloud(四):熔断器Hystrix
说起springcloud熔断让我想起了去年股市中的熔断,多次痛的领悟,随意实施的熔断对整个系统的影响是灾难性的,好了接下来我们还是说正事. 熔断器 雪崩效应 在微服务架构中通常会有多个服务层调用,基 ...
- springcloud(六):配置中心(一)
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...
随机推荐
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...
- 自动化测试环境准备robotframework
(一)针对python2.7版本的自动化环境准备: python 下载地址: https://www.python.org/downloads/ 这里选择Python2.7系列的,后面涉及到wxPyt ...
- [转发]CentOS7安装MySQL
在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1 下载并安装MySQL官方的 Yum Re ...
- unicode转换中文
<!doctype html><html lang="en"> <head> <meta http-equiv="Refres ...
- 安装TortoiseSVN客户端时遇到的异常
环境:WindowsXP,安装 双击SVN安装程序"TortoiseSVN-1.8.5.25224-win32-svn-1.8.8.msi"后,出现 "无法通过Sindo ...
- 使用 paramsPrepareParamsStack 拦截器栈后的运行流程
2. 使用 paramsPrepareParamsStack 拦截器栈后的运行流程 1). paramsPrepareParamsStack 和 defaultStack 一样都是拦截器栈. 而 st ...
- Freebsd下用pureftpd配置FTP服务器
pureftpd安装配置简明说明 1.下载 http://sourceforge.net/projects/pureftpd/ 最新版本是pure-ftpd-1.0.16a.tar.bz2 BSD自 ...
- c# 下三角实现 九九乘法口诀表
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Hell ...
- Python resources
我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列表,内容包括:Web框架.网络 ...
- iass,pass,cass,sass,vm,container
分布式存储hdfs 大文件,swift 对象存贮. 为七牛的CDN系统目前大部分还不是自建的,采用的是整合其它CDN的方式做的.也就是说出了七牛的文件分发使用的是网宿和蓝汛的老牌CDN的分发节点,自身 ...