Spring-Cloud之hello world

1、springboot集成springcloud

建立maven项目做为父工程cloud-parent,并在父工程下建立两个springboot项目的module:cloud-eureka(注册中心)、cloud-eureka2(注册中心2)、cloud-server(服务端),cloud-client(客户端)项目结构以下:java


2、配置cloud-eureka

一、pom文件(建立工程勾选Cloud Discovery-->Eureka Server)web

<?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 http://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>1.5.18.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.wxx.demo</groupId>
   <artifactId>cloud-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>cloud-server</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Edgware.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>
   </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>
复制代码

二、application.properties配置文件spring

server.port=8096

eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8098/eureka/ #配置eureka2的注册中心地址作集群处理复制代码

三、启动类配置apache

package com.wxx.demo;

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

@SpringBootApplication
@EnableEurekaServer//做为Eureka注册中心
public class CloudServerApplication {

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

}
复制代码

四、启动服务端并访问:http://localhost:8096/springboot



五、配置cloud-eureka2同上把注册地址修改成cloud-eureka地址bash



3、配置cloud-client

一、pom文件同上配置app

<?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 http://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.1.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.wxx.demo</groupId>
   <artifactId>cloud-client</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>cloud-client</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
      <dependency>
            <groupId>com.wxx.demo</groupId>
            <artifactId>cloud-server</artifactId>
            <version>0.0.1-SNAPSHOT</version>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <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>

   <repositories>
      <repository>
         <id>spring-milestones</id>
         <name>Spring Milestones</name>
         <url>https://repo.spring.io/milestone</url>
      </repository>
   </repositories>

</project>
复制代码

二、application.properties配置文件
maven

server.port=8090

spring.application.name=cloud-client
eureka.client.serviceUrl.defaultZone=http://localhost:8096/eureka/  
#eureka.client.serviceUrl.defaultZone=http://localhost:8096/eureka/,http://localhost:8096/eureka/ 服务注册地址两种写法均可以复制代码

三、启动类配置
spring-boot

package com.wxx.demo;

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

@SpringBootApplication
@EnableEurekaClient//Eureka客户端注解
public class CloudClientApplication {

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

}

复制代码

四、客户端代码
fetch

package com.wxx.demo.client;

import com.wxx.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoClient {

    @Autowired
    private DemoService demoService;

    @GetMapping("/hello")
    public String sayHello(@RequestParam String hello){

        return demoService.sayHello(hello);
    }


}
复制代码

五、启动客户端项目访问:http://localhost:8090/hello?hello=%22spring-cloud%22


查看服务注册状况

相关文章
相关标签/搜索