示例项目使用的cloud和boot版本如下:
spring-cloud-dependencies版本:2021.0.1
spring-boot-starter-parent版本:2.6.4
第1步:创建Maven Project(作为工程根目录)
** 1.1 点击File > New > Project,选择Maven工程**
1.2 填写GroupId和ArtifactId,ArtifactId会默认为工程名称,点击Next
1.3 可以选择指定文件夹创建项目路径,点击Finish,完成创建。
1.4 工程结构如图,因为后面会创建子工程(springcloud实际代码模块),所以src文件夹可以删掉了
第2步:创建Module(SpringCloud Eureka模块)
2.1 创建Module,选择Spring Initializr,点击Next
**
**
2.2 填写Group和Artifact,选择Java Version,修改Package名称,点击Next
2.3 选择SpringCloud 组件配置,选择左侧Spring Cloud Discovery,然后选择Eureka Server,同时也可以选择工程基于的SpringBoot版本,这里我使用默认的(2.6.4),然后点击Finish。
2.4 生成的目录结构如图
2.5 配置application.yml,因为我偏好于使用yaml文件,所以将默认生成的application.properties修改成了application.yml,配置内容如下:
# 配置启动端口
server:
port: 8000
# 配置eureka服务注册中心
eureka:
instance:
hostname: localhost # 实例名称
client:
registerWithEureka: false # 是否向注册中心注册自己,本身是服务端,不向自己注册
fetchRegistry: false # 是否从Eureka获取注册信息,不需要,所以设置false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # Eureka服务器的地址
# 指定应用名称
spring:
application:
name: cn-eureka
2.6 启动类添加@EurekaServer注解,最终代码如下
package com.cning.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class CnEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(CnEurekaApplication.class, args);
}
}
2.7 现在已经可以启动eureka注册中心了,如图
2.8 浏览器中输入localhost:8000,可以看到打开了Eureka控制台界面
第3步:创建Module(业务模块,Eureka Client)
3.1 创建module,选择Spring Initializr,填写Group和Artifact,完成后点击Next,选择Spring Cloud Discovery,然后选择Eureka Discovery Client,点击Next
3.2 此时,目录结构如图
3.3 pom.xml加入SpringBoot的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3.4 修改启动类和application.yml,修改后内容如下
CnSystemApplication.java
package com.cning.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
// 加入注解
@EnableDiscoveryClient
@SpringBootApplication
public class CnSystemApplication {
public static void main(String[] args) {
SpringApplication.run(CnSystemApplication.class, args);
}
}
application.yml
server:
port: 8001
#指定当前eureka客户端注册的服务端地址
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8000/eureka
instance:
hostname: localhost
# 指定应用名称
spring:
application:
name: cn-system
3.5 创建Service和Controller
在com.cning.system下新建package,命名为service,然后新建HelloService.java,内容如下
package com.cning.system.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
@Autowired
private Environment environment;
public String hello(){
return environment.getProperty("server.port");
}
public String hello(String msg){
return String.format("收到消息:%s, 我是:%s", msg, environment.getProperty("server.port"));
}
}
在com.cning.server下新建package,命名为web,然后新建HelloController.java,内容如下
package com.cning.system.web;
import com.cning.system.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/")
@ResponseBody
public Object hello(@RequestParam(defaultValue = "") String msg){
System.out.println(msg);
return helloService.hello(msg);
}
}
3.6 修改CnSystemApplication.java,在注解SpringBootApplication后加上(scanBasePackages = {"com.cning"})
**3.7 运行CnSystemApplication.java,启动成功后从Eureka管理台(localhost:8000)可以看到cn-system已经注册
**
第4步:创建Module(SpringCloud Feign模块)
Feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,由于分布式部署,多个服务之间需要通讯进行方法调用,如何像调用本地方法一样调用其他工程下的方法呢?feign来为你做。同时,也可以用Feign提供负载均衡。
创建和前面一样,就不上图了,注意选择依赖的这一步,选择Spring Cloud Routing中的OpenFeign,然后继续
结构如下:
4.1 pom.xml加入eureka-client和loadbalance依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.1.0</version>
</dependency>
4.2 创建Feign接口类
在com.cning.api下新建RemoteService.java,内容如下(不要忘了加@FeignClient注解)
package com.cning.api;
import com.cning.api.constant.ServiceNameConstants;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author Cao.ning
* @date 2022/3/7
* @description 远程调用服务
*/
// 指定转发请求的服务器名称 spring.application.name
@Component
@FeignClient(contextId = "remoteService", value = ServiceNameConstants.SYSTEM_SERVICE)
public interface RemoteService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(@RequestParam("msg") String msg);
}
第5步:创建Module(web登录模块)
5.1 新建一个springboot工程,如图
5.2 配置application.yml和启动类CnAuthApplication.java
****application.yml
# 配置启动端口
server:
port: 8080
#指定当前eureka客户端注册的服务端地址
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8000/eureka
instance:
hostname: localhost
# 指定应用名称
spring:
application:
name: cn-auth
CnAuthApplication.java
package com.cning.auth;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication(scanBasePackages = {"com.cning"})
@EnableFeignClients(basePackages = {"com.cning.api"})
@EnableDiscoveryClient
public class CnAuthApplication {
public static void main(String[] args) {
SpringApplication.run(CnAuthApplication.class, args);
}
}
5.3 创建controller
package com.cning.auth.controller;
import com.cning.api.RemoteService;
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;
/**
* @author Cao.ning
* @date 2022/3/7
* @description
*/
@RestController
public class HelloController {
@Autowired
private RemoteService remoteService;
@GetMapping("/hello/{msg}")
public String hello(@PathVariable("msg") String msg){
return remoteService.hello(msg);
}
}
5.4 运行CnAuthApplication.java,启动成功后可以从eureka控制台看到当前应用已注册
第6步:测试
浏览器访问localhost:8080/hello/你好,浏览器返回内容如下
****** 为了验证feign的负载均衡,我们在idea的Run/Debug配置中复制一个CnSystemApplication,如下图,在VM options中添加 -Dserver.port=8002,设置端口为8002
** 4.5 启动CnSystemApplication(8002),刷新Eureka控制台(localhost:8000),可以看到8002和feign都注册到了Eureka**
4.6 浏览器中访问localhost:8080/hello/你好**,可以看到输出结果,再次刷新访问,可以发现每次调用不同端口。**