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开启负载均衡的功能。

  • 相关阅读:
    【试水CAS-4.0.3】第07节_CASclient配置单点登录
    30分钟,让你彻底明白Promise原理
    【你离硅谷只差一步】网易中国创业家大赛项目火热征集中
    21分钟学会写编译器
    Android 模拟器下载、编译及调试
    GitLab 自动触发 Jenkins 构建
    微服务监控探索
    感觉要火!妹子实地采访网易猪厂程序员七夕怎么过
    延迟任务调度系统—技术选型与设计(下篇)
    使用QUIC
  • 原文地址:https://www.cnblogs.com/XJJD/p/10446254.html
Copyright © 2011-2022 走看看