码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-02-27 20:18:02      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:消费   ica   led   通过   report   import   中心   eureka   coding   

服务消费者

创建项目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第三步:发布服务消费者

标签:消费   ica   led   通过   report   import   中心   eureka   coding   

原文地址:https://www.cnblogs.com/XJJD/p/10446254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!