springCloud--1

springCloud--1_第1张图片

电影微服务是服务消费者,用户微服务是服务提供者。

Springcloudeureka的支持很好,eureka本身是一个基于REST的服务,

springCloud--1_第2张图片

springCloud--1_第3张图片

Eureka里面有一个注册表,Application Client是服务消费者,Application Service是服务提供者。Make Remote Call是远程调用,us-east-1c,us-east-1d,us-east-1ezone,他们都属于us-east-1这个region。他们都是Eureka Client

 

服务提供者(Application Service)和服务消费者(Application Client)启动的时候都会像eureka注册(ip和端口),他们更eureka server有一个心跳机制默认30秒,连续390秒没有收到心跳就会从注册表移除这个节点。Eureka service集群之间会相互复制服务注册表。

服务提供者的ip和端口变化了,服务注册表会更新ip和端口。

eureka client缓存:即使所有的eureka service都宕机了,服务消费者也会知道服务提供者的ip和端口进行调用。

服务注册表就是一个数据库,用来记录ip和端口,并且提供API操作服务注册表,把服务添加到注册表称之为注册从注册表移除称之为注销。

心跳机制是健康检查机制。

服务发现方式:客户端发现(Eurekazk),服务端发现(Consul+nginx)。http://blog.daocloud.io/microservices-4/   SpringCloudEureka支持最完整。

Eureka:服务发现组件。

 springCloud--1_第4张图片

服务消费者:

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



    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.itmuch.cloud
    microservice-simple-consumer-movie
    0.0.1-SNAPSHOT
    jar

    microservice-simple-consumer-movie
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.1.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            xalan
            xalan
            2.7.2
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


服务提供者:

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 {

}
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)
);


    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.itmuch.cloud
    microservice-simple-provider-user
    0.0.1-SNAPSHOT
    jar

    microservice-simple-provider-user
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.1.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.h2database
            h2
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-data-solr
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 

转载于:https://www.cnblogs.com/yaowen/p/9124934.html

你可能感兴趣的:(springCloud--1)