SpringCloud之Eureka-Client实现服务(Jpa,H2)
Posted 小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud之Eureka-Client实现服务(Jpa,H2)相关的知识,希望对你有一定的参考价值。
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>
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; } }
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)的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud之Eureka-Client使用RestTemplate实现服务之间的调用
实用篇SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud分布式