springcloud第三步:发布服务消费者

Posted 虚极静笃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud第三步:发布服务消费者相关的知识,希望对你有一定的参考价值。

服务消费者

创建项目sercice-order

Maven依赖

<parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>1.5.2.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</artifactId>

            </dependency>

            <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-starter-ribbon</artifactId>

            </dependency>

            <dependency>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-web</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>Dalston.RC1</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>

                  <snapshots>

                       <enabled>false</enabled>

                  </snapshots>

            </repository>

      </repositories>

 

application.yml配置

eureka:

  client:

    serviceUrl:

      defaultZone: http://localhost:8888/eureka/

server:

  port: 8764

spring:

  application:

    name: service-order

 

编写service,调用service-member

@SuppressWarnings("unchecked")

@Service

public class MemberService {

      @Autowired

      RestTemplate restTemplate;

 

      public List<String> getOrderByUserList() {

            return restTemplate.getForObject("http://service-member/getUserList", List.class);

      }         

}

 

演示效果

@EnableEurekaClient

@SpringBootApplication

public class AppOrder {

 

      public static void main(String[] args) {

            SpringApplication.run(AppOrder.class, args);

      }

 

      @Bean

      @LoadBalanced

      RestTemplate restTemplate() {

            return new RestTemplate();

      }

 

}

 

 

 

在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

以上是关于springcloud第三步:发布服务消费者的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)

业余草 SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)

史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)

SpringCloud快速搭建

Spring Cloud系列之 EurekaZookeeperConsul

Spring Cloud系列之 EurekaZookeeperConsul