使用springcloud微服务结合代码学习
上一篇了解springcloud
下一篇配置服务发现组件eureka
创建提供者 、消费者 、并且消费者调用提供者。
新建工程了,不是新建module
编写提供者
- 创建springboot项目
在数据库中创建一个表
drop table user if exists user;
create table user (
id int AUTO_INCREMENT PRIMARY KEY ,
username varchar(40),
name varchar(20),
age int(3),
balance decimal(10,2)
);
insert into user (id, username, name, age, balance) values (1, 'account1', '张 三', 20, 100.00);
insert into user (id, username, name, age, balance) values (2, 'account2', '李 四', 28, 180.00);
insert into user (id, username, name, age, balance) values (3, 'account3', '王 五', 32, 280.00);
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 https://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.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itzz</groupId>
<artifactId>springcloud-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-provider</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--web项目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--data-jpa调用底层的和jdbc结合-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
</dependencies>
<!--springcloud依赖-->
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</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>
创建实体类
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.math.BigDecimal;
@Entity
//忽略懒加载的特性
@JsonIgnoreProperties(value = {
"hibernateLazyInitializer","handle","fieldHandler"})
public class User {
@Id //主键
@GeneratedValue(strategy = GenerationType.AUTO) //主键生成策略
private Integer id;
@Column(name = "username")
private String username;
@Column
private String name;
@Column
private Integer age;
@Column
private BigDecimal balance;
//setter getter
}
创建dao
import com.itzz.springcloudprovider.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
controller层
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("user/{id}")
public User findById(@PathVariable(value = "id") Integer id){
return userRepository.getOne(id);
}
}
application.yml配置文件
#当前项目启动端口
server:
port: 8000
#连接数据库的信息
generate-ddl: false 取消让它自动见数据库
show-sql: true展示sql
spring:
jpa:
generate-ddl: false
show-sql: true
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=true
driver-class-name: com.mysql.jdbc.Driver
#当前启动的名字
profiles:
active: provider2
application:
name: springcloud-provider
info:
head: head
body: body
app:
name: @project.artifactId@
encoding: @project.build.sourceEncoding@
java:
source: @java.version@
target: @java.version@
management:
endpoints:
web:
exposure:
#加载所有的端点,默认只加载了info、health
include: '*'
endpoint:
health:
show-details: always
#可以关闭指定的端点
shutdown:
enabled: false
eureka:
client:
service-url:
defaultZone: http://root:root@peer1:8761/eureka/,http://root:root@peer2:8762/eureka/
instance:
prefer-ip-address: true
测试
http://localhost:8000/user/1
编写服务消费者
1 新建一个springboot工程
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 https://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.itzz</groupId>
<artifactId>springcloud-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
<!--springcloud依赖-->
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</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>
创建实体类
public class User {
private Integer id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
//省略getter setter
}
无需创建dao
创建controller
@RestController
public class UserController {
@Autowired
private RestTemplate restTemplate;
@Value("${user.userServiceUrl}")
private String userServiceUrl;
@RequestMapping("user/{id}")
public User findById(@PathVariable("id") Integer id){
return restTemplate.getForObject(userServiceUrl+id,User.class);
}
}
配置文件application.yml
user:
userServiceUrl: http://localhost:8000/user/
info:
head: head
body: body
app:
name: @project.artifactId@
encoding: @project.build.sourceEncoding@
java:
source: @java.version@
target: @java.version@
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
shutdown:
enabled: false
eureka:
client:
service-url:
defaultZone: http://root:root@peer1:8761/eureka/,http://root:root@peer2:8762/eureka/
instance:
prefer-ip-address: true
feign:
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
response:
enabled: true
logging:
level:
com.itzz.springcloudconsumer: DEBUG
spring:
profiles: consumer1
application:
name: consumer
server:
port: 8010
配置文件我太确定复制过来能不能直接用,复制的时候哪里多个空格,最好是自己手敲一遍,不会的可以私信或者评论我看到都会回的。哪里有错了可以指出,我们共同学习
编写启动类
@SpringBootApplication
public class SpringcloudConsumerApplication {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringcloudConsumerApplication.class, args);
}
}
yml中的配置
spring:
profiles: consumer1
application:
name: consumer
server:
port: 8010
user:
userServiceUrl: http://localhost:8000/user/
http://localhost:8010/user/1,看看能不能显示出来数据。
加入服务发现组件,eureka server ,client
配置健康监控端点
provider 监控 监控jar包、配置文件
consumer 监控 jar包,配置文件
pom.xml中添加如下内容
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml 中添加如下配置如果有
info:
head: head
body: body
app:
name: @project.artifactId@
encoding: @project.build.sourceEncoding@
java:
source: @java.version@
target: @java.version@
management:
endpoints:
web:
exposure:
#加载所有的端点,默认只加载了info、health
include: '*'
endpoint:
health:
show-details: always
#可以关闭指定的端点
shutdown:
enabled: false
测试:http://localhost:8000/actuator/health
http://localhost:8000/actuator/info