栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微服务项目的简单整理(二)--------服务中心Eureka

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微服务项目的简单整理(二)--------服务中心Eureka

一、项目相关技术

1.Maven:项目管理工具

2.SpringCloud

3.SpringBoot 2.4.13

4.MybatisPlus

5.Eureka:服务/配置中心

6.OpenFegin:服务调用

7.Gateway:网关

二、项目展示

父工程-maven项目

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.13
         
    
    com.sunyard
    springcloud-eureka-gateway
    1.0
   pom
    
        eureka-server-5001
        stu-common
        stu-customer-8001
        stu-provider-6001
        stu-gateway-7001
    
    
        1.8
        2020.0.5
        5.1.49
        3.4.3.4
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
                mysql
                mysql-connector-java
                ${mysql.version}
                runtime
            
            
                com.baomidou
                mybatis-plus-boot-starter
                ${mybatis-plus.version}
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


注意:版本依赖关系(版本对应关系可以查看官网)

本项目版本对应关系如下:

Spring Cloud VersionSpring Boot Version
2020.0.52.4.13

1. 公共模块-SpringBoot项目

1.1 pom.xml



    4.0.0
    
        com.sunyard
        springcloud-eureka-gateway
        1.0
    

    stu-common
    1.0

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            com.baomidou
            mybatis-plus-boot-starter
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


1.2 po实体类(Student.java)

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("stu")
public class Student {
    @TableId(type= IdType.AUTO)
    private Integer id;
    private String name;
    private Integer age;
    private String gender;
    private String address;
    private Date birth;
}

1.3 vo响应结果集(AjaxResult.java)

public class AjaxResult extends HashMap {
    public AjaxResult(boolean success, int status,String msg, Object data){
        this.put("success",success);
        this.put("status",status);
        this.put("msg",msg);
        this.put("data",data);
    }

    public static AjaxResult success(){
        return new AjaxResult(true,200,"操作成功",null);
    }
    public static AjaxResult success(String msg){
        return new AjaxResult(true,200,msg,null);
    }
    public static AjaxResult success(Object data){
        return new AjaxResult(true,200,"操作成功",data);
    }
    public static AjaxResult success(String msg, Object data){
        return new AjaxResult(true,200,msg,data);
    }
    public static AjaxResult success(int status, String msg) {
        return new AjaxResult(true, status, msg,null);
    }
    public static AjaxResult success(int status, String msg, Object data){
        return new AjaxResult(true,status,msg,data);
    }
    public static AjaxResult fail(){
        return new AjaxResult(false,500,"操作失败",null);
    }
    public static AjaxResult fail(String msg){
        return new AjaxResult(false,500,msg,null);
    }
    public static AjaxResult fail(int status, String msg){
        return new AjaxResult(false,status,msg,null);
    }
    public static AjaxResult fail(int status, String msg, Object data){
        return new AjaxResult(false,status,msg,data);
    }
    public static AjaxResult fail(boolean success, int status, String msg){
        return new AjaxResult(success,status,msg,null);
    }
}

2. Eureka服务中心(eureka-server-5001)

2.1 pom.xml



    4.0.0
    
        com.sunyard
        springcloud-eureka-gateway
        1.0
    

    eureka-server-5001
    1.0

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.2 application.yml

server:
  port: 5001
spring:
  application:
    name: sunsca-eureka-server
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:5001/eureka/
  server:
    enable-self-preservation: false #关闭保护模式

2.3 EurekaServer5001Application.java(启动类添加注解@EnableEurekaServer服务端)

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer5001Application {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServer5001Application.class, args);
    }

}

3. 网关模块(stu-gateway-7001)

3.1 pom.xml



    4.0.0
    
        com.sunyard
        springcloud-eureka-gateway
        1.0
    

    stu-gateway
    1.0

    
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


3.2 application.yml

server:
  port: 7001
eureka:
  client:
    service-url:
      defaultZone: http://localhost:5001/eureka/
#  instance:
#    hostname: stu-gateway
#  instance:
#    prefer-ip-address: true
#    instance-id: stu-gateway
spring:
  application:
    name: stu-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: stu-provider
          uri: lb://stu-provider
          predicates:
            - Path=/stu/**
            - Method=GET,POST
        - id: stu-customer
          uri: lb://stu-customer
          predicates:
            - Path=/www/customer/**
            - Method=GET,POST
          filters:
            - StripPrefix=1

3.3 StuGatewayApplication.java(启动类加注解@EnableEurekaClient客户端)

@SpringBootApplication
@EnableEurekaClient
public class StuGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(StuGatewayApplication.class, args);
    }

}

4. 提供者模块(stu-provider-6001)

4.1 pom.xml



    4.0.0
    
        com.sunyard
        springcloud-eureka-gateway
        1.0
    

    stu-provider
    1.0

    
        1.8
    
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.baomidou
            mybatis-plus-boot-starter
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.sunyard
            stu-common
            1.0
            compile
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


4.2 application.yml

server:
  port: 6001
spring:
  application:
    name: stu-provider
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/studb?useUnicode=true&characterEncoding=UTF8
    username: root
    password: xxx
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:5001/eureka/

4.3 MybatisPlusConfig.java(mybatisplus配置)

@Configuration
@MapperScan("com.sunyard.mapper")
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

4.4 StuMapper.java(mapper接口)

public interface StuMapper extends BaseMapper {
}

4.5 StuServiceI.java(业务层接口)

public interface StuServiceI extends IService {

}

4.6 StuServiceImpl.java(业务层接口实现类)

@Service
public class StuServiceImpl extends ServiceImpl implements StuServiceI {

}

4.7 StuController.java(控制层)

@RestController
@RequestMapping("/stu")
public class StuController {

    @Resource
    private StuServiceI stuService;

    @GetMapping("/list")
    public AjaxResult list(){
        List studentList = stuService.list();
        return AjaxResult.success(studentList);
    }
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Long id){
        Student student = stuService.getById(id);
        return AjaxResult.success(student);
    }
}

5. 消费者模块(stu-customer-8001)

5.1 pom.xml



    4.0.0
    
        com.sunyard
        springcloud-eureka-gateway
        1.0
    
    com.sunyard
    stu-customer
    1.0
    stu-customer
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
            2.2.6.RELEASE
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.sunyard
            stu-common
            1.0
            compile
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


5.2 application.yml

server:
  port: 8001
spring:
  application:
    name: stu-customer
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/studb?useUnicode=true&characterEncoding=UTF8
    username: root
    password: xxx
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:5001/eureka/
#  instance:
#    prefer-ip-address: true
#    instance-id: stu-customer
feign:
  circuitbreaker:
    enabled: true

5.3 ProvideClient.java

@FeignClient("stu-gateway")//提供者的名字
@LoadBalancerClient(name = "stu-gateway")
public interface ProviderClien {

    @GetMapping("/stu/list")
    public AjaxResult list();

    @GetMapping("/stu/{id}")
    public AjaxResult findById(@PathVariable Long id);
}

5.4 CustomerController.java

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private ProviderClien providerClien;

    @GetMapping("/list")
    public AjaxResult list(){
        return providerClien.list();
    }

    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Long id){
        return providerClien.findById(id);
    }
}

6. 测试

        通过网关访问消费者接口

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985687.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号