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>