zoukankan      html  css  js  c++  java
  • 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开启负载均衡的功能。

  • 相关阅读:
    poj 2226 Muddy Fields(最小点覆盖)
    hdu 5093 Battle ships(二分图最大匹配)
    poj 3020 Antenna Placement(二分图最大匹配)
    poj 3041 Asteroids(最小点覆盖)
    二分图的一些定理
    hdu 1083 Courses(二分图最大匹配)
    二分图最大匹配模板
    hdu 5094 Maze (BFS+状压)
    hdu 5092 Seam Carving (简单数塔DP,题没读懂,,不过可以分析样例)
    hdu 5090 Game with Pearls (额,, 想法题吧 / 二分图最大匹配也可做)
  • 原文地址:https://www.cnblogs.com/XJJD/p/10446254.html
Copyright © 2011-2022 走看看