• Springboot搭建微服务案例之Eureka注册中心


    一、父工程依赖管理

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. 4.0.0
    6. org.mumu
    7. eureka
    8. pom
    9. 1.0
    10. <module>commonmodule>
    11. <module>consumermodule>
    12. <module>springcloud-service-providermodule>
    13. UTF-8
    14. 1.8
    15. 1.8
    16. 4.12
    17. 1.2.17
    18. 5.1.47
    19. 1.16.18
    20. 1.1.16
    21. 1.3.0
    22. org.springframework.cloud
    23. spring-cloud-starter-netflix-eureka-client
    24. 2.2.1.RELEASE
    25. org.springframework.cloud
    26. spring-cloud-starter-netflix-eureka-server
    27. 2.2.1.RELEASE
    28. org.apache.maven.plugins
    29. maven-project-info-reports-plugin
    30. 3.0.0
    31. org.springframework.boot
    32. spring-boot-dependencies
    33. 2.2.2.RELEASE
    34. pom
    35. import
    36. mysql
    37. mysql-connector-java
    38. ${mysql.version}
    39. runtime
    40. com.alibaba
    41. druid-spring-boot-starter
    42. 1.1.10
    43. org.mybatis.spring.boot
    44. mybatis-spring-boot-starter
    45. ${mybatis.spring.boot.version}
    46. junit
    47. junit
    48. ${junit.version}
    49. org.springframework.boot
    50. spring-boot-maven-plugin
    51. true

    二、搭建公共模块common

    放一些pojo类

    1. 依赖引入

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. eureka
    6. org.mumu
    7. 1.0
    8. 4.0.0
    9. springcloud-service-common
    10. 1.0
    11. 8
    12. 8
    13. org.projectlombok
    14. lombok
    15. 1.18.30

    2.实体类

    1. @Data // 省略写get set方法
    2. @NoArgsConstructor //提供无参数的构造函数
    3. @AllArgsConstructor //提供带所有参数的构造函数
    4. public class Payment implements Serializable {
    5. private long id;
    6. private String serial;
    7. }

    三、搭建服务提供方provider

    1.依赖引入

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. eureka
    6. org.mumu
    7. 1.0
    8. 4.0.0
    9. springcloud-service-provider
    10. 1.0
    11. org.mumu
    12. 8
    13. 8
    14. org.mumu
    15. springcloud-service-common
    16. 1.0
    17. org.springframework.boot
    18. spring-boot-starter-web
    19. org.springframework.boot
    20. spring-boot-starter-actuator
    21. org.mybatis.spring.boot
    22. mybatis-spring-boot-starter
    23. com.alibaba
    24. druid-spring-boot-starter
    25. mysql
    26. mysql-connector-java
    27. org.springframework.boot
    28. spring-boot-starter-jdbc
    29. org.springframework.boot
    30. spring-boot-devtools
    31. runtime
    32. true
    33. org.projectlombok
    34. lombok
    35. true
    36. org.springframework.boot
    37. spring-boot-starter-test
    38. test
    39. org.springframework.cloud
    40. spring-cloud-starter-netflix-eureka-client

    2.配置类

    1. server:
    2. port: 8001 #配置服务端口号
    3. spring:
    4. application:
    5. name: service-provider # 配置服务提供方的名称
    6. datasource: # 配置连接数据库的基本信息
    7. driver-class-name: com.mysql.jdbc.Driver # 驱动
    8. url: jdbc:mysql://localhost:3306/cloud2023 # 连接数据库的url
    9. username: root # 连接数据库的用户名
    10. password: 123456 # 连接数据库的密码
    11. mybatis:
    12. config-location: classpath:/mybatis/sqlMapConfig.xml # 引入mybatis的核心配置文件
    13. mapper-locations: classpath:/mybatis/mapper/*.xml # 引入mybatis的映射文件
    14. eureka:
    15. client:
    16. register-with-eureka: true # 允许将当前服务注册到eureka注册中心
    17. fetch-registry: true # 允许当前微服务拉取注册中心中的服务信息
    18. service-url:
    19. defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/ # eureka注册中心的地址

    3.编写启动类

    1. @SpringBootApplication
    2. @MapperScan(basePackages = "com.xq.dao")
    3. @EnableDiscoveryClient //开启服务发现的功能
    4. public class ProviderApplication {
    5. public static void main(String[] args) {
    6. SpringApplication.run(ProviderApplication.class,args);
    7. }
    8. }

    4. 编写业务逻辑

    (1)整合mybatis

    dao层

    1. public interface PaymentDao {
    2. //根据id查询payment信息
    3. public Payment findById(long id);
    4. //新增payment信息
    5. public void add(Payment payment);
    6. }

    创建dao接口的映射文件还有mybatis的核心配置文件

    1. "1.0" encoding="UTF-8" ?>
    2. "-//mybatis.org//DTD Mapper 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    4. "org.mumu.dao.PaymentDao">
    5. "add" parameterType="payment">
    6. INSERT INTO `payment`(`id`,`serial`) VALUES(#{id},#{serial})

    配置 MyBatis 的类型别名,简化 MyBatis 映射文件中的配置 

    1. "1.0" encoding="UTF-8"?>
    2. "-//mybatis.org//DTD Config 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    4. <package name="org.mumu.pojo">package>

    (2)Service

    1. @Service
    2. public class PaymentServiceImpl implements PaymentService {
    3. @Resource
    4. PaymentDao paymentDao;
    5. @Override
    6. public Payment findById(long id) {
    7. return paymentDao.findById(id);
    8. }
    9. @Override
    10. public void add(Payment payment) {
    11. paymentDao.add(payment);
    12. }
    13. @Override
    14. public void save(Payment payment) {
    15. paymentDao.add(payment);
    16. }
    17. }

    5.定义控制器

    1. @RestController
    2. @RequestMapping("provider")
    3. public class PaymentController {
    4. @Resource
    5. PaymentService paymentService;
    6. @Value("${server.port}")
    7. String port;
    8. @RequestMapping("findById")
    9. public Result findById(@RequestParam("id") long id){
    10. try {
    11. Thread.sleep(3000);
    12. } catch (InterruptedException e) {
    13. e.printStackTrace();
    14. }
    15. Payment payment = paymentService.findById(id);
    16. return new Result<>(200,"数据查询成功,当前服务端口号是:" + this.port,payment);
    17. }
    18. }

    四、搭建服务消费方consumer

    1.依赖引入

    1. org.mumu
    2. springcloud-service-common
    3. 1.0
    4. org.springframework.boot
    5. spring-boot-starter-web
    6. org.springframework.boot
    7. spring-boot-starter-actuator
    8. org.springframework.boot
    9. spring-boot-devtools
    10. runtime
    11. true
    12. org.projectlombok
    13. lombok
    14. true
    15. org.springframework.boot
    16. spring-boot-starter-test
    17. test
    18. org.springframework.cloud
    19. spring-cloud-starter-netflix-eureka-client

    2.配置类

    1. server:
    2. port: 80
    3. spring:
    4. application:
    5. name: service-consumer
    6. eureka:
    7. client:
    8. register-with-eureka: true # 允许将当前服务注册到eureka注册中心
    9. fetch-registry: true # 允许当前微服务拉取注册中心中的服务信息
    10. service-url:
    11. defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/ # eureka注册中心的地址

    3.启动类

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class ConsumerApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(ConsumerApplication.class,args);
    6. }
    7. }

    4.注入RestTemplate组件到ioc容器

    使用 RestTemplate 这个 Java 客户端组件来实现服务的远程调用。所以我们需要将
    RestTemplate 使用 Java 配置类进行注入:
    1. @Configuration
    2. public class MyConfig {
    3. @Bean
    4. public RestTemplate restTemplate(){
    5. return new RestTemplate();
    6. }
    7. }

    5.定义控制器

    1. @RestController
    2. @RequestMapping("consumer")
    3. @Slf4j
    4. public class PaymentController {
    5. @Resource
    6. RestTemplate restTemplate;
    7. @RequestMapping("findById/{id}")
    8. public Result findById(@PathVariable("id") long id){
    9. String url = "http://localhost:8001/provider/findById?id=" + id; //维护服务提供方的ip+端口
    10. Result result = restTemplate.getForObject(url, Result.class);
    11. return result;
    12. }
    13. }

    五、搭建服务注册中心

    1.引入依赖server

    1. javax.servlet
    2. javax.servlet-api
    3. 4.0.1
    4. org.springframework.cloud
    5. spring-cloud-starter-netflix-eureka-server
    6. org.springframework.boot
    7. spring-boot-starter-web
    8. org.springframework.boot
    9. spring-boot-starter-actuator
    10. org.springframework.boot
    11. spring-boot-devtools
    12. runtime
    13. true

    2.配置类

    这里不需要要进行服务注册,因为这个模块的server模块

    负责对其他Client进行服务注册

    1. server:
    2. port: 7001
    3. # 配置eureka服务端
    4. eureka:
    5. client:
    6. register-with-eureka: false # 禁止自己注册自己
    7. fetch-registry: false # 禁止抓取注册中心中的服务信息
    8. service-url:
    9. defaultZone: http://localhost:7001/eureka/ # eureka服务端的地址

    3.启动类

    1. @SpringBootApplication
    2. @EnableEurekaServer // 标识当前服务是Eurkea服务端
    3. public class EurekaServerApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(EurekaServerApplication.class,args);
    6. }
    7. }

    六、启动

    访问地址:http://localhost:7001

  • 相关阅读:
    web大作业 静态网页 HTML+CSS+JavaScript橙色的时尚服装购物商城
    spring cloud 全家桶 简单介绍
    【MySql进阶】索引详解(一):索引数据页结构
    Vue开发环境安装
    【配置文件】Redis配置文件解读和持久化
    Spring Cloud Alibaba 工程搭建
    【C++】:list容器的基本使用
    PS文字创建工作路径矢量化后变细,导出的svg也变细的解决方案
    Vue项目devServer.proxy代理配置详解
    Mysql进阶-视图篇
  • 原文地址:https://blog.csdn.net/Candy___i/article/details/134216934