第一章:SpringCloud服務發現和服務註冊

weixin_33859844發表於2017-11-11

服務提供者

1. 解決硬編碼問題

  1. 修改一個服務ip後關聯到好幾個服務的配置修改

2. 服務發現元件

  1. 服務登錄檔
  2. 服務註冊
  3. 健康檢查 預設30s

3. 服務發現方式

  1. 客戶端 eureka 【zk】
  2. 服務端 consul+nginx

4. 程式碼片

pom.xml
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
配置檔案application.yml
server:
  port: 7900
spring:
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:
    url: jdbc:mysql://127.0.0.1/springcloud??characterEncoding=UTF8&useSSL=true&allowMultiQueries=true
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    schema: classpath:schema.sql   #建表
    data: classpath:data.sql #資料
logging:
  level: info
建立資料庫
  1. schema.sql
DROP table user if exists;
CREATE TABLE user(
    id bigint,
    username varchar(40),
    name varchar(20),
    age int(3),
    balance decimal(10,2),
    PRIMARY KEY(id)
);
  1. data.sql
insert into user(id,username,name,age,balance) VALUES(1,'user1','老焦1',200,100.00);
insert into user(id,username,name,age,balance) VALUES(2,'user2','老焦2',200,100.00);
insert into user(id,username,name,age,balance) VALUES(3,'user3','老焦3',200,100.00);
insert into user(id,username,name,age,balance) VALUES(4,'user4','老焦4',200,100.00);
insert into user(id,username,name,age,balance) VALUES(5,'user5','老焦5',200,100.00);

建立Bean和dao

User.java

/**
 * Created by Fant.J.
 * 2017/11/11 13:53
 */
@Entity
@Data
public class User implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column
    private String username;
    @Column
    private String name;
    @Column
    private Short age;
    @Column
    private BigDecimal balance;
}

UserReposiry.java

@Repository
public interface UserReposiry extends JpaRepository<User,Long>{
}

建立檢視層
@RestController
public class UserController {

    @Autowired
    private UserReposiry userReposiry;

    @GetMapping("/simple/id")
    public User findById(@PathVariable Long id){
        return this.userReposiry.findOne(id);
    }
}
啟動專案
5786888-8f64ef197eec648b.png
image.png

服務消費者

建立模組 fantj-consumer-movie

只需要新增User bean和controller
server.port= 7901
/**
 * Created by Fant.J.
 * 2017/11/11 13:53
 */
@Data
public class User implements Serializable{
    private Long id;
    private String username;
    private String name;
    private Short age;
    private BigDecimal balance;
}
@RestController
public class MovieController {
    @Autowired
    private RestTemplate template;

    @RequestMapping("/movie/{id}")
    public User findById(@PathVariable Long id){
        return this.template.getForObject("http://127.0.0.1:7900/simple/"+id,User.class);
    }
}
啟動springboot時候建立RestTemplate類
@SpringBootApplication
public class FantjConsumerMovieApplication {

    @Bean  //例項化resttemplate
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(FantjConsumerMovieApplication.class, args);
    }
}

啟動專案
5786888-67dc757c528e3130.png
image.png

這就證明了服務被消費

相關文章