博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

springBoot学习过程的坑

Posted on 2021-12-08 19:16  一只菜鸟的进阶之路  阅读(37)  评论(0编辑  收藏  举报

1.springboot服务注册

  1.1创建注册中心

    1)在idea中选择File-New-Project-Spring Initializr,界面选择SDK等

     

    2)点击Next,设置Group等,这里选择的java的版本是8

     

 

     3)点击Next,选择Spring Cloud Discovery,并在右侧选择Eureka Server

     

 

     4)设置Projectname既可完成

     

 

  1.2 spring的代码及配置

    1)Appilcation:

      

    package com.example.demo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

    @EnableEurekaServer
    @SpringBootApplication
    public class DemoApplication {

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

    }
  2)pom.xml
    
  <?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
   <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>
  <properties>
   <java.version>1.8</java.version>
  <spring-cloud.version>Greenwich.SR5</spring-cloud.version>
  </properties>
  <dependencies>
  <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>

   <dependency>
   <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>RELEASE</version>
  <scope>test</scope>
  </dependency>
  </dependencies>
  <dependencyManagement>
   <dependencies>
   <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-dependencies</artifactId>
   <version>${spring-cloud.version}</version>
   <type>pom</type>
   <scope>import</scope>
   </dependency>
   </dependencies>
   </dependencyManagement>

  <build>
   <plugins>
   <plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   </plugins>
   </build>

  </project>
  需要注意springboot和springcloud的版本对应关系,否则可能由于不匹配导致报错
  3)application.yml
  
    server:
     port: 1111

    spring:
     application:
     name: enreka-server
    eureka:
     instance:
     hostname: localhost
     client:
     register-with-eureka: false
     fetch-registry: false
     service-url:
     defaultZone: http://172.17.6.74:1111/eureka/ #给客户端用的,注意这里结束必须是eureka

  4)此时启动之后,在网页中输入http://localhost:1111,会显示:
  
1.2创建客户端
  1)新建项目与服务的区别在于选择Spring Cloud Discovery中选择Eureka Discovery Client
  2)application
    
    package com.springcloud.demoprovider;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

    @EnableEurekaClient
    @SpringBootApplication
    public class DemoproviderApplication {

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

    }

  3)application.yml
    server:
     port: 1112
    spring:
     application:
     name: demoprovider
    eureka:
    client:
     service-url:
    defaultZone: http://172.17.6.74:1111/eureka/
     fetch-registry: true
     register-with-eureka: true
  4)service
  
    package com.springcloud.service;

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    import javax.management.ValueExp;

    @RestController
    public class DemoService {
     @GetMapping(value="/login")
     public void login(){
     System.out.println( "provider 被调用了");
    }
    }
  4)启动之后,可以在注册中心看到该服务
  

    

 以上是可以注册的,但是无法真正的访问到我的接口,这里是因为接口的package存放地方有问题,详细可见:https://www.freesion.com/article/1174657845/

 原文文章更清晰:https://blog.csdn.net/qq_41622603/article/details/104401266





2.编译问题
  编译时可以设置跳过test,可以使用mvn的命令,也可以在项目中不将test识别为测试目录(这个最简单),设置方法:选中test->右键选中Mark Directory as ->Unmark as test source root