springBoot学习过程的坑


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"?>
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.0.0
  
  org.springframework.boot
  spring-boot-starter-parent
  2.1.6.RELEASE
  
  

   com.example
   demo
   0.0.1-SNAPSHOT
  demo
  Demo project for Spring Boot
  
   1.8
  Greenwich.SR5
  

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


  
   org.springframework.boot
  spring-boot-starter-test
  test
  

  
   org.junit.jupiter
  junit-jupiter
  RELEASE
  test
  

  

  
  
  
   org.springframework.cloud
   spring-cloud-dependencies
   ${spring-cloud.version}
   pom
   import
  

  

  


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

  

  


  

  需要注意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