3.rubbion负载均衡

时间:2018-04-21 18:07:46 阅读:193次
rubbion负载均衡

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.0</modelVersion>
  <groupId>com.hena28.service</groupId>
  <artifactId>service-order</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>2.0.0.RELEASE</version>
	</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>
	
	<dependencyManagement>
	    <dependencies>
	        <dependency>
	            <groupId>org.springframework.cloud</groupId>
	            <artifactId>spring-cloud-dependencies</artifactId>
	            <version>Finchley.M9</version>
	            <type>pom</type>
	            <scope>import</scope>
	        </dependency>
	    </dependencies>
	</dependencyManagement>

	<dependencies>
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-starter-eureka</artifactId>
	        <version>2.0.0.M2</version>
	    </dependency>
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-starter-ribbon</artifactId>
	        <version>2.0.0.M2</version>
	    </dependency>
	    <dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-web</artifactId>
		 </dependency>	 
	</dependencies>
	<repositories>
    	<repository>
	        <id>spring-milestones</id>
	        <name>Spring Milestones</name>
	        <url>https://repo.spring.io/milestone</url>
	        <snapshots>
	            <enabled>false</enabled>
	        </snapshots>
	    </repository>
	</repositories>
</project>


application.properties

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


service

package com.henan28.app.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {
	@Autowired
	private RestTemplate restTemplate;
	
	public List<String> getOrderUser() {
		return restTemplate.getForObject("http://service-member/getMember", List.class);
	}
}


controller

@RestController
public class OrderController {
	@Autowired
	private OrderService orderService;
	
	@RequestMapping("/getOrderUser")
	public List<String> getOrderUser(){
		System.out.println("members user");
		return orderService.getOrderUser();
	}
	
	@RequestMapping("/getOrderServiceApi")
	public String getOrderServiceApi() {
		return "这是order项目接口";
	}
}


app

@SpringBootApplication
@EnableEurekaClient
public class OrderApp {
	public static void main(String[] args) {
		SpringApplication.run(OrderApp.class, args);
	}
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

扫描二维码关注程序员爱笔记,接收更多资讯

评论

快速评论