其他分享
首页 > 其他分享> > springcloud_day02_07.第七节-@LoadBalanced实现本地负载均衡 - 搭建消费者

springcloud_day02_07.第七节-@LoadBalanced实现本地负载均衡 - 搭建消费者

作者:互联网

项目结构

依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

	</dependencies>
	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

application.yml

###服务启动端口号
server:
  port: 8001
###服务名称(服务注册到eureka名称)  
spring:
    application:
        name: app-itmayiedu-order
###服务注册到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka

           
###因为该应用为注册中心,不会注册自己
    register-with-eureka: true
###是否需要从eureka上获取注册信息
    fetch-registry: true

OrderController.java

@RestController
public class OrderController {
	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping("/getorder")
	public String getOrder() {
		// order 使用rpc 远程调用技术 调用 会员服务
		String memberUrl = "http://app-itmayiedu-member/getMember";
		String result = restTemplate.getForObject(memberUrl, String.class);
		System.out.println("会员服务调用订单服务,result:" + result);
		return result;
	}

}

AppOrder.java


@SpringBootApplication
@EnableEurekaClient
public class AppOrder {

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

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

}

启动  注册中心、服务提供者、消费者

可以看到,服务提供者和消费者都已经注册到注册中心去了

消费者已经从服务提供者获取到了数据,端口号

标签:07,LoadBalanced,springcloud,springframework,eureka,注册,spring,###,String
来源: https://blog.csdn.net/pyd1040201698/article/details/99993108