springcloud project created

  1. Create a maven simple web project in idea.
  2. Create a module in the simple maven project.
    1. The moudle name is eureka,it's a discovery and register services.
    2.  

       

    3. server:
        port: 8761
      
      eureka:
        instance:
          hostname: localhost
        client:
          registerWithEureka: false
          fetchRegistry: false
          serviceUrl:
            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      

        yml 配置文件,没有yml后缀的,直接添加一个文件即可

    4.  

       在application中加入注解@EnableEurekaServer,申明此处为服务注册中心。

      package com.bcd.cloud.pf.eureka;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
      
      /**
       * EurekaApplication class
       *
       * @EnableEurekaServer was created by myself
       *
       * @author myself
       * @date 2019/12/05
       */
      @SpringBootApplication
      @EnableEurekaServer
      public class EurekaApplication {
      
      	public static void main(String[] args) {
      		SpringApplication.run(EurekaApplication.class, args);
      	}
      
      }
      

        Start this project,and view in the web explorer.http://localhost:8761/

    5. You will see this view.

       

       

  3. create productor.
    1. This project is a web, created project type is 
      EnableEurekaClient
    2. eureka:
        client:
          serviceUrl:
            defaultZone: http://localhost:8761/eureka/
      server:
        port: 8763
      spring:
        application:
          name: productor
      

        productor was set eureka client,it will discovery by 8761,but its port is 8763.

    3. package com.bcd.cloud.pf.productor;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
      
      /**
       * ProductorApplication class
       *
       * @EnableEurekaClient was created by myself
       *
       * @author myself
       * @date 2019/12/05
       */
      @SpringBootApplication
      @EnableEurekaClient
      public class ProductorApplication {
      
      	public static void main(String[] args) {
      		SpringApplication.run(ProductorApplication.class, args);
      	}
      
      }
      

        

    4. The APPController was added by myself.
      package com.bcd.cloud.pf.productor.rest;
      
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.RestController;
      
      /**
       * Demo class
       *
       * @author keriezhang
       * @date 2016/10/31
       */
      @RestController
      public class AppController {
      
          @Value("${server.port}")
          String port;
      
          @RequestMapping("/hi")
          public String home(@RequestParam String name)
          {
              return "hi " + name + ",i am from port:" + port;
          }
      }
      

        

    5. Run this application,you will see this view.
    6.  

       

       

       

  4. create su

猜你喜欢

转载自www.cnblogs.com/hoge66/p/11986894.html