第四章 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服务注册
1.说明 本文详细介绍微服务注册到Eureka的方法, 即Eureka Client注册到Eureka Server, 这里用任意一个Spring Cloud服务为例, 比如下面已经创建好的Confi ...
- 为什么Eureka Client获取服务实例这么慢
1. Eureka Client注册延迟 Eureka Client启动后不会立即向Eureka Server注册,而是有一个延迟时间,默认为40s 2. Eureka Server更新响应缓存 Eu ...
- springcloud 向Eureka中注册服务异常java.net.ConnectException: Connection refused: connect
异常如下: 通过debug发现,服务端的url地址仍然是默认的http://localhost:8761/eureka/apps/,也就是说yml文件中配置没有生效,检查后发现yml中相关配置多写了一 ...
- SpringCloud创建Config Client通过Eureka访问Config
1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置. 读取配置的方式有两种, 第一种是直接配置Configer Server的URL, 第 ...
- Spring-Cloud之Eureka注册与发现-2
一.Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringCloud将它集成在其 ...
- springcloud(二) eureka的使用
上一节讲到order微服务是通过rest调用user微服务的地址.但是,user微服务的地址是写死的, 如果user微服务集群的话,那么order微服务该如何调用呢?这个时候注册中心该上场了 演示eu ...
- #Eureka 客户端和服务端间的交互
Eureka 服务器客户端相关配置 1.建立eureka服务器 只需要使用@EnableEurekaServer注解就可以让应用变为Eureka服务器,这是因为spring boot封装了Eureka ...
- SpringCloud IDEA 教学 (三) Eureka Client
写在前头 本篇继续介绍基于Eureka的SpringCloud微服务搭建,回顾一下搭建过程, 第一步:建立一个服务注册中心: 第二步:建立微服务并注入到注册中心: 第三步:建立client端来访问微服 ...
- 白话SpringCloud | 第四章:服务消费者(RestTemple+Ribbon+Feign)
前言 上两章节,介绍了下关于注册中心-Eureka的使用及高可用的配置示例,本章节开始,来介绍下服务和服务之间如何进行服务调用的,同时会讲解下几种不同方式的服务调用. 一点知识 何为负载均衡 实现的方 ...
随机推荐
- awk处理实记
经grep日志后得到的数据格式如下: } . [debug][-- ::] SendDataStyled:{ , "innings" : "6189269620_0007 ...
- Charles中使用Map Local提高测试效率
书接上回,上次说到Charles中可以使用修改返回值来模拟接口返回,这次我们来说一下Charles中另外一个强大的功能. 我们用手机连接Charles,具体可以参考上一篇<借助Charles来测 ...
- Hive Server2(五)
HiveServer2 基本概念介绍 1.HiveServer2基本介绍 HiveServer2 (HS2) is a server interface that enables remote cli ...
- 根据IP 自动识别国家和城市
https://www.jianshu.com/p/1b1a018ae729 根据IP 自动识别国家和城市
- springboot jpa 创建数据库以及rabbitMQ分模块扫描问题
在使用jpa过程中,如果没有在配置中加入自动创建实体对于的sql,则需要提前创建建表语句 spring.jpa.properties.hibernate.show_sql=true spring.jp ...
- mysql单表操作与多表操作
0. null和notnull: 使用null的时候: create table t8( id int auto_increment primary key, name varchar(32), em ...
- Spring使用@AspectJ开发AOP(零配置文件)
前言: AOP并不是Spring框架特有的.Spring只是支持AOP编程 (面向切面编程) 的框架之一. 概念: 1.切面(Aspect) 一系列Advice + Pointcut 的集合. 2.通 ...
- Nginx解析PHP
刚安装完PHP后,nginx是无法解析的,如果输入地址会直接下载文件,需要进行如下的设置 步骤 修改/etc/nginx/sites-available/default和cat /etc/nginx/ ...
- iOS设置UITableViewCell的选中时的颜色
1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 cell.selectio ...
- Java基础-自增自减运算符练习题
我们用一个简单的例子分析下边的运行结果: package demo; public class ZiZeng { int i = 0; test(i); // i = i++; i = ++i; Sy ...