4.0.0
org.mumu
eureka
pom
1.0
common
consumer
springcloud-service-provider
UTF-8
1.8
1.8
4.12
1.2.17
5.1.47
1.16.18
1.1.16
1.3.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.2.1.RELEASE
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.2.1.RELEASE
org.apache.maven.plugins
maven-project-info-reports-plugin
3.0.0
org.springframework.boot
spring-boot-dependencies
2.2.2.RELEASE
pom
import
mysql
mysql-connector-java
${mysql.version}
runtime
com.alibaba
druid-spring-boot-starter
1.1.10
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.spring.boot.version}
junit
junit
${junit.version}
org.springframework.boot
spring-boot-maven-plugin
true
放一些pojo类
eureka
org.mumu
1.0
4.0.0
springcloud-service-common
1.0
8
8
org.projectlombok
lombok
1.18.30
@Data // 省略写get set方法
@NoArgsConstructor //提供无参数的构造函数
@AllArgsConstructor //提供带所有参数的构造函数
public class Payment implements Serializable {
private long id;
private String serial;
}
eureka
org.mumu
1.0
4.0.0
springcloud-service-provider
1.0
org.mumu
8
8
org.mumu
springcloud-service-common
1.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.mybatis.spring.boot
mybatis-spring-boot-starter
com.alibaba
druid-spring-boot-starter
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
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注册中心的地址
@SpringBootApplication
@MapperScan(basePackages = "com.xq.dao")
@EnableDiscoveryClient //开启服务发现的功能
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
(1)整合mybatis
dao层
public interface PaymentDao {
//根据id查询payment信息
public Payment findById(long id);
//新增payment信息
public void add(Payment payment);
}
创建dao接口的映射文件还有mybatis的核心配置文件
INSERT INTO `payment`(`id`,`serial`) VALUES(#{id},#{serial})
配置 MyBatis 的类型别名,简化 MyBatis 映射文件中的配置
(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);
}
}
@RestController
@RequestMapping("provider")
public class PaymentController {
@Resource
PaymentService paymentService;
@Value("${server.port}")
String port;
@RequestMapping("findById")
public Result findById(@RequestParam("id") long id){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Payment payment = paymentService.findById(id);
return new Result<>(200,"数据查询成功,当前服务端口号是:" + this.port,payment);
}
}
org.mumu
springcloud-service-common
1.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
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注册中心的地址
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
@Configuration
public class MyConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@RestController
@RequestMapping("consumer")
@Slf4j
public class PaymentController {
@Resource
RestTemplate restTemplate;
@RequestMapping("findById/{id}")
public Result findById(@PathVariable("id") long id){
String url = "http://localhost:8001/provider/findById?id=" + id; //维护服务提供方的ip+端口
Result result = restTemplate.getForObject(url, Result.class);
return result;
}
}
javax.servlet
javax.servlet-api
4.0.1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
这里不需要要进行服务注册,因为这个模块的server模块
负责对其他Client进行服务注册
server:
port: 7001
# 配置eureka服务端
eureka:
client:
register-with-eureka: false # 禁止自己注册自己
fetch-registry: false # 禁止抓取注册中心中的服务信息
service-url:
defaultZone: http://localhost:7001/eureka/ # eureka服务端的地址
@SpringBootApplication
@EnableEurekaServer // 标识当前服务是Eurkea服务端
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
访问地址:http://localhost:7001