SpringCloudNetfilx入门

96次阅读

共计 3116 个字符,预计需要花费 8 分钟才能阅读完成。

整体框架

由于整个微服务使用 springcloud 进行管理,所以采用 maven 聚合工程

导包

最好使用父项目进行版本控制

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

注意点 :maven 中和 Java 类似没有多继承这个概念,间接实现多继承就可以使用 import 对包进行导入,这样子包就能继承版本控制

注册中心

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

客户端(服务提供者也是客户端)

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 实现负载均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- 显示 info 信息 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

application 配置

注册中心

server:
port: 8000
eureka:
client:
register-with-eureka: false # 不讲自己注册进注册中心
fetch-registry: false # 不拉取服务

在 springboot 应用中开启 EurekaServer

@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {public static void main(String[] args) {SpringApplication.run(RegistryApplication.class);
}
}

服务提供者或消费者

server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka # 服务中心的地址,',' 隔开可以写多个实现集群
instance:
instance-id: consumer-port:8081 # status 的名称
spring:
application:
name: consumer # 服务的名称
debug: true
# 配置自己的信息
info:
app.name: roderick

在 springboot 应用中开启 EurekaServer

@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class);
}
}

消费者调用提供者 Rest 接口

这里使用 RestTemplate 连接客户端

@RestController
public class MainController {
RestTemplate restTemplate;
DiscoveryClient discoveryClient;
private static final String URL = "http://localhost:8100";
private static final String ID_URL = "http://CONSUMER"; // 通过 id 直接获取(内部玩法)@Autowired
public void setRestTemplate(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@Autowired
public void setDiscoveryClient(DiscoveryClient discoveryClient) {this.discoveryClient = discoveryClient;}
@GetMapping("/get/{id}")
public String getUser(@PathVariable("id") String id) {return restTemplate.getForObject(ID_URL + "/test/" + id, String.class);
}
@GetMapping("/services")
public String getService() { // 获取服务列表
Map<String, List<ServiceInstance>> map = new HashMap<>();
List<String> services = discoveryClient.getServices();
for (String service : services) {List<ServiceInstance> instances = discoveryClient.getInstances(service);
map.put(service, instances);
}
return map.toString();}
}

代码中将 URL 写死是不行的,我们可以使用 DiscoveryClient 获取或者直接将服务名作为 url,通过解析可以获取到服务的真实地址

正文完
 0