Spring Boot 实践3 --基于spring cloud 实现微服务的简单调用

Posted Coder_fang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 实践3 --基于spring cloud 实现微服务的简单调用相关的知识,希望对你有一定的参考价值。

背景:spring boot主张微服务,所以这里不得不提出服务之间的调用

   这次我们使用srping cloud作为服务集成与管理者

 (转载请注明来源:cnblogs coder-fang)

 

  1. 根据实践1,创建一个spring boot 项目(springServer),作为服务中心:
  2. springServer的pom文件如下:
    <?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>
    
        <groupId>com.test</groupId>
        <artifactId>springbootService</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>springmvcTest-1</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            
        </properties>
    
        <dependencies>
        
            <dependency>
            
                <groupId>org.springframework.cloud</groupId> 
                <artifactId>spring-cloud-starter-eureka-server</artifactId>            
            </dependency>
    
        </dependencies>
        
        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR7</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-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/libs-snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
        <pluginRepositories>
            <pluginRepository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    
    
    </project>
    View Code
  3. springServer的application.properties配置如下:
    server.port=8000
    eureka.instance.hostname=0.0.0.0
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

    设置服务中心的端口为8000,eureka.client.register-with-eureka=false,eureka.client.fetch-registry=false,这两个是禁止自己注册到服务中心,

    eureka.client.serviceUrl.defaultZone 这个参数是指定客服端调用的服务地址。
  4. 在ServiceApplication中使用@EnableEurekaServer启用服务中心:
    package com.springdemo;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    
    @EnableEurekaServer
    @SpringBootApplication
    public class ServiceApplication {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(ServiceApplication.class).run(args);
        }
    }
    View Code
  5. 运行Spring server,在浏览器输入 http://localhost:8000/ 进入服务中心页面,此时页面中application为空,暂时无服务注册:

  6. 修改springmvcTest项目,即实践1中创建的项目,使其支持spring cloud eureka。
    1.   pom中加入依赖:
      <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-eureka</artifactId>
              </dependency>
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-dependencies</artifactId>
                  <version>Camden.SR7</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
    2. application.properties中加入 
      #for eureka
      spring.application.name=db-service
      eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
      eureka.client.serviceUrl.defaultZone是上述spring server项目的地址,
      spring.application.name 是本项目在服务中心的名称,用于其它服务访问本服务
       
    3. 在SpringmvcTestApplication中加入注解@EnableDiscoveryClient 将自己注册到服务中心:
      @EnableDiscoveryClient
      @SpringBootApplication
      @ServletComponentScan
      public class SpringmvcTestApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(SpringmvcTestApplication.class, args);
              
          }
      }
    4. 运行springmvcTest ,观察服务注册中心页面,此时application中已有我们注册的服务:
    5. 此时 springmvcTest中的restful接口,已可以被服务中心的其它服务调用。
  7.  创建服务消费者,使其能够调用springmvcTest项目中的restful服务:
    1.   根据实践1 方式创建项目(springClient),pom文件如下:
      <?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>
      
          <groupId>com.test</groupId>
          <artifactId>springClient</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <packaging>jar</packaging>
      
          <name>springmvcTest-1</name>
          <description>Demo project for Spring Boot</description>
      
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.5.3.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
      
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <java.version>1.8</java.version>
              
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
              </dependency>
              
              <!-- Eureka -->
              <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-eureka</artifactId>
              </dependency>
          </dependencies>
              <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-dependencies</artifactId>
                  <version>Camden.SR7</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-snapshots</id>
                  <name>Spring Snapshots</name>
                  <url>https://repo.spring.io/snapshot</url>
                  <snapshots>
                      <enabled>true</enabled>
                  </snapshots>
              </repository>
              <repository>
                  <id>spring-milestones</id>
                  <name>Spring Milestones</name>
                  <url>https://repo.spring.io/milestone</url>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </repository>
          </repositories>
      
          <pluginRepositories>
              <pluginRepository>
                  <id>spring-snapshots</id>
                  <name>Spring Snapshots</name>
                  <url>https://repo.spring.io/snapshot</url>
                  <snapshots>
                      <enabled>true</enabled>
                  </snapshots>
              </pluginRepository>
              <pluginRepository>
                  <id>spring-milestones</id>
                  <name>Spring Milestones</name>
                  <url>https://repo.spring.io/milestone</url>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </pluginRepository>
          </pluginRepositories>
      
      
      </project>
      View Code
    2. application.properties中加入:
      spring.application.name=client
      eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
    3. 项目入口application代码如下:
      package com.test.demo;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
      import org.springframework.cloud.client.loadbalancer.LoadBalanced;
      import org.springframework.context.ConfigurableApplicationContext;
      import org.springframework.context.annotation.Bean;
      import org.springframework.http.HttpEntity;
      import org.springframework.http.HttpHeaders;
      import org.springframework.http.HttpMethod;
      import org.springframework.http.ResponseEntity;
      import org.springframework.web.client.RestTemplate;
      
      
      
      @EnableDiscoveryClient
      @SpringBootApplication
      public class SpringBootClientApp {
          
      
          @Bean
          @LoadBalanced
          RestTemplate restTemplate() {
              return new RestTemplate();
          }
          public static void main(String[] args) {
              ConfigurableApplicationContext ctx = SpringApplication.run(SpringBootClientApp.class, args);
              
              RestTemplate restTemp = ctx.getBean(RestTemplate.class);
              
              String val=restTemp.getForEntity("http://db-service/user/zh", String.class).getBody();
              
              System.out.println("getVal="+val);
              
          }
      }

      首先注入一个 restTemplate用来调用远程rest服务

    4. 运行client,且关键输出如下:

       

  到这里远程服务调用已基本完成

以上是关于Spring Boot 实践3 --基于spring cloud 实现微服务的简单调用的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot-starter-quartz集群实践

Spring Boot 2.x 实践记:Gson

spring-boot-starter-quartz集群实践

spring-boot-starter-quartz集群实践

spring boot 并行传出请求的最佳实践是啥?

Spring Boot 2.x 实践记:Gson