1. 程式人生 > >SpringCloud之Eureka-Client實現服務(Jpa,H2)

SpringCloud之Eureka-Client實現服務(Jpa,H2)

web wired autowire -m initial ima factor enc gen

1、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
> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent
> <groupId>com.test</groupId> <artifactId>eureka-client-user</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client-user</name> <description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.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>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
View Code

2、在application.yml文件中添加配置

spring:
  application:
    name: eureka-client-user  #應用名
  jpa:
    generate-ddl: false
    show-sql: true   #jpa顯示sql語句
    hibernate:
      ddl-auto: none
  datasource:
    platform: h2   #數據庫使用H2模擬
    schema: classpath:schema.sql  #sql語句配置
    data: classpath:data.sql  #數據配置
logging:  #logging日誌配置
  level:
    root: INFO
    org.hibernate: INFO
server:
  port: 8663   #服務端口

eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8661/eureka

3、在啟動類添加註解

package com.test.eurekaclientuser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientUserApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientUserApplication.class, args);
    }
}

4、添加data.sql和schema.sql

#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))

#data.sql文件內容
insert into user (id,username,name,age,balance) values(1,‘user1‘,‘zhangsan‘,20,100.00);
insert into user (id,username,name,age,balance) values(2,‘user2‘,‘zhangsan‘,20,100.00);
insert into user (id,username,name,age,balance) values(3,‘user3‘,‘zhangsan‘,20,100.00);
insert into user (id,username,name,age,balance) values(4,‘user4‘,‘zhangsan‘,20,100.00);
insert into user (id,username,name,age,balance) values(5,‘user5‘,‘zhangsan‘,20,100.00);

5、User.java類

技術分享圖片
package com.test.eurekaclientuser.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
//Jpa的註解配置
@Entity
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class User implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private  Long id;
    @Column
    private  String name;
    @Column
    private  String username;
    @Column
    private  BigDecimal balance;
    @Column
    private  short age;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public short getAge() {
        return age;
    }

    public void setAge(short age) {
        this.age = age;
    }
}
View Code

6、UserRepository.java類

package com.test.eurekaclientuser.repository;

import com.test.eurekaclientuser.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
#繼承Jpa的類
public interface UserRepository extends JpaRepository<User,Long> {
}

7、UserController.java類

package com.test.eurekaclientuser.controller;

import com.test.eurekaclientuser.entity.User;
import com.test.eurekaclientuser.repository.UserRepository;
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;

@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

  /*  @Autowired
    private EurekaClient discoveryClient;*/

    @GetMapping("/user/{id}")
    public User findById(@PathVariable  Long id){
        System.out.println(id);
        return this.userRepository.getOne(id);
    }

   /* @GetMapping("/eureka/url")
    public String serviceUrl() {
        InstanceInfo instance = discoveryClient.getNextServerFromEureka("client1", false);
        return instance.getHomePageUrl();
    }*/
}

8、訪問URL

http://localhost:8663/user/1

同時,可以在eureka-server組件上看到註冊的信息,即

技術分享圖片

SpringCloud之Eureka-Client實現服務(Jpa,H2)