restTemplate方式实现springcloud项目初探

一般需要三个项目,注册中心也就是Eureka注册中心项目,服务的调用方和服务的接口提供方。

1.首先新建maven工程,eureka-server项目

新建完毕,引入pom依赖


        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    
    
        UTF-8
        UTF-8
        1.8
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.RC1
                pom
                import
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

然后配置yml配置文件
/eureka-server/src/main/resources/application.yml
配置文件内容如下

server:
  port: 8888        端口号
eureka:
  instance:
    hostname: localhost     启动地址,这里默认为本地
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/     eureka注册中心地址

然后创建启动类(注意注解要使用正确)
EurekaApp.java

package com.lwq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer 
public class EurekaApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApp.class, args);
    }

}

启动项目,访问http://localhost:8888/成功访问说明配置正确

2.创建服务提供者,即service-member(这里一会员服务为例)

同样,创建工程需要引入maven依赖


        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    
    
        UTF-8
        UTF-8
        1.8
    
    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.RC1
                pom
                import
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

然后引入配置文件application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/      此处为eureka本地工程地质
server:
  port: 8762
spring:
  application:
    name: service-member        此处为服务调用的名称

创建启动类MemberApp.java

package com.lwq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient       //注意这里是client,而注册中心那里是 server
public class MemberApp {

    public static void main(String[] args) {
        SpringApplication.run(MemberApp.class, args);

    }

}

创建接口服务
MemberController.java

package com.lwq;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {

    @RequestMapping("/getUserList")
    public List getUserList() {
        List listUser = new ArrayList();
        listUser.add("zhangsan");
        listUser.add("lisi");
        listUser.add("wangwu");
        return listUser;
    }
}

启动查看eureka注册中心会出现有服务注册成功


restTemplate方式实现springcloud项目初探_第1张图片
image.png

此时访问项目地址就可以成功返回


restTemplate方式实现springcloud项目初探_第2张图片
image.png
3.下面我们创建服务的调用者 service-order服务,通过rest方式进行调用

创建完毕引入依赖


        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-ribbon
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.RC1
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

引入yml文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8764
spring:
  application:
    name: service-order

创建启动类OrderApp.java

package com.lwq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class OrderApp {

    public static void main(String[] args) {
        SpringApplication.run(OrderApp.class, args);
    }
    
    /**
     * 启用rest方法进行调用
     * @return
     */
    @Bean
    @LoadBalanced //允许负载均衡
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

编写service,调用service-member

@SuppressWarnings("unchecked")
@Service
public class MemberService {
    @Autowired
    RestTemplate restTemplate;

    public List getOrderByUserList() { 
        //这里直接写服务提供者的名称即可
        return restTemplate.getForObject("http://service-member/getUserList", List.class);
    }   
}

创建controller类型进行调用完成正确返回信息

package com.lwq;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Autowired
    private MemberService memberService;
    
    @RequestMapping("getOrderByUserList")
    public List getOrderByUserList() {
        return memberService.getOrderByUserList();
    }
}

小提示:注意三个项目的启动顺序,一定要是eureka项目首先启动,不然无法完成注册会报错

你可能感兴趣的:(restTemplate方式实现springcloud项目初探)