创建服务端 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 ,服务端,消费端 效果如下: