一、搭建方式
Springboot版本:2.3.1.RELEASE
SpringCloud版本:Hoxton.SR9
项目截图:
二、搭建过程
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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.provider</groupId>
<artifactId>springcloud-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-provider</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example.api</groupId>
<artifactId>springcloud-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加eureka 客户端地址 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</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>
application.xml文件:
server:
port: 8091
servlet:
context-path: /
# 服务名称
spring:
application:
name: springcloud-provider
eureka:
client:
service-url:
defaultZone: http://admin:admin@eureka-server2:10002/eureka/,http://admin:admin@eureka-server1:10001/eureka/
instance:
instance-id: springcloud-provider-8091 # 定义微服务的名称
prefer-ip-address: true # 是否显示IP和端口
# 开启全局监控
management:
endpoints:
web:
exposure:
include: "*"
service接口和实现类,由于未用数据库查询直接service返回模拟结果数据
package com.example.provider.service;
import com.example.api.pojo.User;
import java.util.List;
public interface UserService {
//获取所有用户的信息
List<User> getAllUser();
}
package com.example.provider.service.Impl;
import com.example.api.pojo.User;
import com.example.provider.service.UserService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Override
public List<User> getAllUser() {
List<User> userList = new ArrayList<>();
User user1 = User.builder()
.id(1)
.name("小明")
.age(20)
.sex("男").build();
User user2 = User.builder()
.id(3)
.name("小红")
.age(19)
.sex("女").build();
userList.add(user1);
userList.add(user2);
return userList;
}
}
controller文件:
package com.example.provider.controller;
import com.example.api.pojo.User;
import com.example.provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
//获取所有用户的信息
@GetMapping("/getAllUser")
public List<User> getAllUser() {
List<User> userList = userService.getAllUser();
return userList;
}
}
Application启动类文件:
package com.example.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //注册到Eureka注册中心
public class SpringcloudProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudProviderApplication.class, args);
}
}
通过上述项目搭建好后,启动项目,可以在注册中心Eureka可看到服务,如下图:
使用postman进行接口调用,如下图:
三、上述总结
以上就是微服务生产者项目搭建,由于学习,数据采用模拟方式,没有链接数据库,后期再整理链接mysql数据库。
下一篇: consumer消费者搭建(3).