一、版本问题
很多人开始整合spring cloud和springboot的时候,会出现版本的的问题。现在推荐使用官方推荐版本整合问题。
地址:https://start.spring.io/actuator/info 官方推荐
二、整合
一.新建一个cloud父工程
二.父工程POM
这里我们使用的是F版本的cloud
<?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>
<groupId>cn.kr</groupId>
<artifactId>kr-cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>kr-consumer</module>
<module>kr-producer</module>
</modules>
<!-- springboot版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 注意cloud与boot的版本 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</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.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
<!-- 加入webClient -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三.生产者模块(kr-producer)
1.application.yml
#注册服务名
spring:
application:
name: producer-nacos-server
#nacos地址
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
#端口号
server:
port: 8001
2.生产者工程代码
ProducerApplication.java
/**
* Created by Janson
* 2020/6/3
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
TestController
package cn.kr.controller;
import cn.kr.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Janson
* 2020/6/3
*/
@RestController
public class TestController {
@Autowired
private ServerConfig serverConfig;
@GetMapping("/test")
public String hello(@RequestParam String msg) {
String res = "生产者服务器(" + serverConfig.getUrl() + "):" + msg;
System.out.println(res);
return res;
}
}
ServrerConfig.java
package cn.kr.config;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 获取IP地址
* Created by Janson
* 2020/6/3
*/
@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
private int serverPort;
public String getUrl() {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "http://"+address.getHostAddress() +":"+this.serverPort;
}
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
this.serverPort = event.getWebServer().getPort();
}
}
四.消费者模块(kr-consumer)
1.application.yml
#注册服务名
spring:
application:
name: consumer-nacos-server
#nacos地址
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
#端口号
server:
port: 8002
2.生产者工程代码
ConsumerApplicaiton
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
TestController
package cn.kr.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* Created by Janson
* 2020/6/3
*/
@RestController
public class TestController {
@Autowired
LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
//springcloud common的负载均衡接口,提供服务名实现服务调用
ServiceInstance serviceInstance = loadBalancerClient.choose("producer-nacos-server");
String url = serviceInstance.getUri()+"/test?msg=nacos";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
String msg = url + "---- return : " + result;
System.out.println(msg);
return msg;
}
}
五.启动,验证
启动生产者与消费者
再浏览器中输入:http://localhost:8848/nacos
出现两个实例则是注册成功
访问:localhost:8002/test
这样,通过nacos为注册中心的就完成了。