上一篇我们建了一个多模块项目结构,用于编写login服务,我们接着上篇的内容 ,闲话少说,直接上码。
一、login服务提供接口的模块pf-login-client
1、启动类LoginClientApplication
package cn.pw.platform;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient
public class LoginClientApplication {
public static void main(String[] args) {
SpringApplication.run(LoginClientApplication.class,args);
}
}
添加@EnableEurekaClient 注解,使得login服务成为Eureka的一个客户端,通过加载配置文件将服务注册到Eureka Server 注册中心。
2、配置文件application.yml
server:
port: 8010
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
instance-id: ${spring.cloud.client.ipAddress}
prefer-ip-address: true
spring:
application:
name: pf-login-server
jpa:
database: mysql
show-sql: true
hibernate:
ddl-auto: update
naming:
strategy: org.hibernate.cfg.ImprovedNamingStrategy
datasource:
url: jdbc:mysql://localhost:3306/pw_base_db
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
这各配置文件包三部分内容,最上面是指定了服务运行端口号8010,中间部分向注册中心地址,多个地址中间用逗号隔开达到高可用,最后一部分是数据源,主要是Dao层使用。
3、服务暴露的接口*(基于RESTful风格的api)*
package cn.pw.platform.login.client;
import cn.pw.platform.dao.po.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
public interface LoginClient {
@GetMapping("/selectUserByName/{userName}")
User selectUserByName(@PathVariable("userName") String userName);
@PostMapping("/user/add")
User addUser(@RequestBody User user);
}
4、接口实现类
package cn.pw.platform.login.client.impl;
import cn.pw.platform.dao.po.User;
import cn.pw.platform.login.client.LoginClient;
import cn.pw.platform.login.server.service.LoginServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LoginClientImpl implements LoginClient {
@Autowired
private LoginServer loginServer;
@Override
public User selectUserByName(@PathVariable("userName") String userName) {
return loginServer.findByName(userName);
}
@Override
public User addUser(@RequestBody User user) {
return loginServer.addUser(user);
}
}
非常常见和常规的RESTful风格写法,没有什么可说的,这样服务提供接口就定义好了,下面在server模块中实现业务逻辑。
二、login服务业务的逻辑模块pf-login-server
1、启动类LoginServerApplication
package cn.pw.platform;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
@SpringBootApplication
public class LoginServerApplication {
public static void main(String[] args) {
SpringApplication.run(LoginServerApplication.class,args);
}
}
由于server模块最终是依jar的形式被client依赖,我习惯上所有配置都写在client中,方便统一维护。
2、逻辑接口
package cn.pw.platform.login.server.service;
import cn.pw.platform.dao.po.User;
public interface LoginServer {
User addUser(User user);
User findByName(String userName);
}
3、逻辑实现
package cn.pw.platform.login.server.serviceImpl;
import cn.pw.platform.dao.UserRepository;
import cn.pw.platform.dao.po.User;
import cn.pw.platform.login.server.service.LoginServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class LoginServerImpl implements LoginServer{
@Autowired
private UserRepository userRepository;
@Override
public User addUser(User user) {
return userRepository.save(user);
}
@Override
public User findByName(String userName) {
return userRepository.findUserByName(userName);
}
}
三、login服务业务的持久模块pf-login-dao
1、启动类DaoApplication
package cn.pw.platform;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class DaoApplication {
public static void main(String[] args) {
SpringApplication.run(DaoApplication.class,args);
}
}
2、进行持久 操作,自定义Repository接口,实现JpaRepository,剩下的交给SpringBoot
package cn.pw.platform.dao;
import cn.pw.platform.dao.po.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import javax.persistence.Table;
@Repository
@Table(name="user")
public interface UserRepository extends JpaRepository<User, Long> {
User findById(Long id);
User save(User user);
@Query("select t from User t where t.name=:name")
User findUserByName(@Param("name") String name);
}
3、实体类User
package cn.pw.platform.dao.po;
import javax.persistence.*;
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
@Column(name = "nick_name")
private String nickName;
private String password;
private Integer age;
private Integer sex;
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 getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
四、测试
1、启动Eureka Server 注册中心。
2、启动login服务,通过client模块的LoginClientApplication类启动。
3、打开注册中心 http://localhost:8761/ ,这会能看到我们的服务已经注册了。
4、通过postman 工具调用我们写的两个接口
① 先post请求添加一条记录 http://localhost:8010/user/add
细节:@RequestBody接收的参数 ,传参时必须是json格式。如果不行搞这么搞,就用 @RequestParam 逐个接收,然后再自己编码注入到实体中 。
②再get查询 http://localhost:8010/selectUserByName/libin
这只是个demo,功能 既简单,使用如此繁琐的多模块可能初学者会很嫌弃,但是实际项目中,这样做大有好处。