一、父工程依赖管理
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">
二、搭建公共模块common
放一些pojo类
1. 依赖引入
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">
2.实体类
@Data // 省略写get set方法
@NoArgsConstructor //提供无参数的构造函数
@AllArgsConstructor //提供带所有参数的构造函数
public class Payment implements Serializable {
private long id;
private String serial;
}
三、搭建服务提供方provider
1.依赖引入
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">
2.配置类
server:
port: 8001 #配置服务端口号
spring:
application:
name: service-provider # 配置服务提供方的名称
datasource: # 配置连接数据库的基本信息
driver-class-name: com.mysql.jdbc.Driver # 驱动
url: jdbc:mysql://localhost:3306/cloud2023 # 连接数据库的url
username: root # 连接数据库的用户名
password: 123456 # 连接数据库的密码
mybatis:
config-location: classpath:/mybatis/sqlMapConfig.xml # 引入mybatis的核心配置文件
mapper-locations: classpath:/mybatis/mapper/*.xml # 引入mybatis的映射文件
eureka:
client:
register-with-eureka: true # 允许将当前服务注册到eureka注册中心
fetch-registry: true # 允许当前微服务拉取注册中心中的服务信息
service-url:
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/ # eureka注册中心的地址
3.编写启动类
@SpringBootApplication
@MapperScan(basePackages = "com.xq.dao")
@EnableDiscoveryClient //开启服务发现的功能
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
4. 编写业务逻辑
(1)整合mybatis
dao层
public interface PaymentDao {
//根据id查询payment信息
public Payment findById(long id);
//新增payment信息
public void add(Payment payment);
}
创建dao接口的映射文件还有mybatis的核心配置文件
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
SELECT * FROM `payment` WHERE id = #{id}
INSERT INTO `payment`(`id`,`serial`) VALUES(#{id},#{serial})
配置 MyBatis 的类型别名,简化 MyBatis 映射文件中的配置
"http://mybatis.org/dtd/mybatis-3-config.dtd">
(2)Service
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
PaymentDao paymentDao;
@Override
public Payment findById(long id) {
return paymentDao.findById(id);
}
@Override
public void add(Payment payment) {
paymentDao.add(payment);
}
@Override
public void save(Payment payment) {
paymentDao.add(payment);
}
}
5.定义控制器
@RestController
@RequestMapping("provider")
public class PaymentController {
@Resource
PaymentService paymentService;
@Value("${server.port}")
String port;
@RequestMapping("findById")
public Result
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Payment payment = paymentService.findById(id);
return new Result<>(200,"数据查询成功,当前服务端口号是:" + this.port,payment);
}
}
四、搭建服务消费方consumer
1.依赖引入
2.配置类
server:
port: 80
spring:
application:
name: service-consumer
eureka:
client:
register-with-eureka: true # 允许将当前服务注册到eureka注册中心
fetch-registry: true # 允许当前微服务拉取注册中心中的服务信息
service-url:
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/ # eureka注册中心的地址
3.启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
4.注入RestTemplate组件到ioc容器
使用
RestTemplate
这个
Java
客户端组件来实现服务的远程调用。所以我们需要将
RestTemplate
使用
Java
配置类进行注入:
@Configuration
public class MyConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.定义控制器
@RestController
@RequestMapping("consumer")
@Slf4j
public class PaymentController {
@Resource
RestTemplate restTemplate;
@RequestMapping("findById/{id}")
public Result
String url = "http://localhost:8001/provider/findById?id=" + id; //维护服务提供方的ip+端口
Result result = restTemplate.getForObject(url, Result.class);
return result;
}
}
五、搭建服务注册中心
1.引入依赖server
2.配置类
这里不需要要进行服务注册,因为这个模块的server模块
负责对其他Client进行服务注册
server:
port: 7001
# 配置eureka服务端
eureka:
client:
register-with-eureka: false # 禁止自己注册自己
fetch-registry: false # 禁止抓取注册中心中的服务信息
service-url:
defaultZone: http://localhost:7001/eureka/ # eureka服务端的地址
3.启动类
@SpringBootApplication
@EnableEurekaServer // 标识当前服务是Eurkea服务端
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
六、启动
访问地址:http://localhost:7001
精彩内容
发表评论