springCloud 입문 (3) 원 격 호출 방법

14286 단어 자바
새 클 라 이언 트 가 등 록 된 원 격 서버 를 호출 하 는 방법:
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>demogroupId>
    <artifactId>cloud-simple-uiartifactId>
    <version>1.0-SNAPSHOTversion>

    <packaging>jarpackaging>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.3.5.RELEASEversion>
    parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Brixton.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.7java.version>
    properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-feignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-hystrixartifactId>
        dependency>
        <dependency>
            <groupId>org.jolokiagroupId>
            <artifactId>jolokia-coreartifactId>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
        <defaultGoal>compiledefaultGoal>
    build>


project>

user 실체 클래스
public class User {

    private String username;
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

userService 서비스 클래스
@Service
public class UserService {
     @Autowired  
     RestTemplate restTemplate;

     final String SERVICE_NAME="         ";

     @HystrixCommand(fallbackMethod = "fallbackSearchAll")
     //        ,  @HystrixCommand  。          @HystrixCommand        ,        fallbackMethod    。   Hystrix      、                   
     public List readUserInfo() {
         return restTemplate.getForObject("http://"+SERVICE_NAME+"/user", List.class);
    }    
     private List fallbackSearchAll() {
         //    @HystrixCommand                
     }
}

사용자 컨트롤 러 컨트롤 러
@RestController
public class UserController {

    @Autowired
    UserService userService;
    @RequestMapping(value="/users")
    public ResponseEntity> readUserInfo(){
        List users=userService.readUserInfo();        
        return new ResponseEntity>(users,HttpStatus.OK);
    }
}

bootstrap. properties 프로필
spring.cloud.config.uri=http://127.0.0.1:${config.port:8888}
spring.cloud.config.name=cloud-config
spring.cloud.config.profile=${config.profile:dev}
eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/
#service name
spring.application.name=      eureka   
#ribbon config
cloud-simple-service.ribbon.ConnectTimeout=5000
cloud-simple-service.ribbon.ReadTimeout=10000
server.port=8080

시작 클래스
public class WebApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);
    }
}

프로그램 을 시작 하면 eureka 에서 서 비 스 를 발견 할 수 있 고 URL 에 접근 하면 원 격 호출 방법 을 사용 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기