Erueka 服务端 消费端 搭建步骤

创建服务端 maven 项目

pom.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.mr</groupId>
	<artifactId>clouditem</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>clouditem</name>
	<description>Demo project for Spring Boot</description>
<!--java版本-->
	<properties>
		<java.version>1.8</java.version>
	</properties>
<!-- 导入Spring Cloud的依赖管理 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<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>

	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<!-- 资源文件拷贝插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

application.yml 文件如下:

server:
  port: 8081

spring:
    application:
        name: app-item
eureka:
  client:
    service-url:
           defaultZone: http://ws:ws@127.0.0.1:8100/eureka/,http://ws:ws@127.0.0.1:9100/eureka/

    register-with-eureka: true

    fetch-registry: true

启动类:

package com.mr;

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

@SpringBootApplication  //springboot  启动
@EnableEurekaClient     //添加注解 让erueka发现服务
public class ClouditemApplication {

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

}

创建消费端

pom.xml 和服务端一样
application.yml 文件如下:

server:
  port: 8083
myspcloud:
  item:
    url: http://127.0.0.1:8081/item/


spring:
    application:
        name: app-order

eureka:
  client:
    service-url:
           defaultZone: http://ws:ws@127.0.0.1:9100/eureka

    register-with-eureka: true
    fetch-registry: true

启动类添加

package com.mr;

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.context.annotation.ComponentScan;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@ComponentScan(basePackages = {"com.mr.controller", "com.mr.service","com.mr.properties"})//手动指定bean扫描范围
@EnableEurekaClient
public class CloudorderApplication {

	public static void main(String[] args) {
		SpringApplication.run(CloudorderApplication.class, args);
	}
		/**
		 * 向Spring容器中定义RestTemplate对象
		 * @return
		 */
		@Bean
		@LoadBalanced
		/*在使用springcloud ribbon客户端负载均衡的时候,可以给RestTemplate bean
		加一个@LoadBalanced注解,就能让这个RestTemplate在请求时拥有客户端负载均衡的能力:*/
		public RestTemplate restTemplate() {
			return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
		}


}

调用代码

@Autowired
RestTemplate restTemplate;
public String testUser(){
return restTemplate.getForObject("http://app-item/toTest",
String.class);
}

启动Erueka ,服务端,消费端 效果如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值