本节摘要
- 注册中心搭建euraka
- 服务注册与发现
- springcloud客户端调用:rest、feign
- 负载均衡:ribbon
- zuul接口网关
搭建euraka注册中心
1、搭建项目环境
创建springboot pom 项目
pom.xml依赖配置如下:
<groupId>com.link</groupId>
<artifactId>springcloud01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、创建eureka注册中心
- 修改pom.xml依赖
<groupId>com.link</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.link</groupId>
<artifactId>springcloud01</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka- server</artifactId>
</dependency>
</dependencies>
- 配置eureka服务信息
# 注册中心端口
server:
port: 8888
# 应用名称
spring:
application:
name: eurka-server
eureka:
instance:
hostname: localhost
client:
# 禁止自己当做服务注册
registerWithEureka: false
# 屏蔽注册信息
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 添加启动注解
启动服务,如图所示没有注册的服务
3、服务注册
- 配置pom.xml
<groupId>com.link</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.link</groupId>
<artifactId>springcloud01</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 修改配置文件
# 服务提供者端口
server:
port: 8881
spring:
application:
name: server-client
eureka:
client:
serviceUrl:
# 注册中心地址
defaultZone: http://localhost:8888/eureka/
- 提供服务
启动类加入注解:
@SpringBootApplication
@EnableEurekaClient
// 添加服务
@RestController
public class ClientController {
@RequestMapping("/getUserList")
public List<String> getUserList() {
List<String> listUser = new ArrayList<String>();
listUser.add("link");
listUser.add("23");
listUser.add("xiamen");
return listUser;
}
}
4、服务消费
ribbon是一个负载均衡客户端 类似nginx反向代理,
可以很好的控制htt和tcp的一些行为。
Feign默认集成了ribbon。
方式1:ribbon
- 配置pom依赖
<groupId>com.link</groupId>
<artifactId>order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>order</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.link</groupId>
<artifactId>springcloud01</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--ribbon+rest => feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
- 修改配置
server:
port: 8883
spring:
application:
name: consumer-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
- 配置启动类
@EnableDiscoveryClient和@EnableEurekaClient作用:
都是能够让注册中心能够发现,扫描到改服务。
@EnableEurekaClient只适用于Eureka作为注册中心
@EnableDiscoveryClient 可以是其他注册中心。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
/**
* 开启负载均衡
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 编写controller调用服务
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/getUserList")
public List<String> hiService() {
// 服务地址+接口
return restTemplate.getForObject("http://server-client/getUserList", List.class);
}
- 测试调用
方式2:feign
- 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
- 修改配置
server:
port: 8883
spring:
application:
name: service-feign
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
- 服务调用(接口配置方式)
@FeignClient(value = "server-client")
public interface UserInfo{
/**
* 服务接口
* @return
*/
@RequestMapping(value = "/getUserList")
List<String> getUserList();
}
@RestController
public class Controller {
@Autowired
UserInfo userInfo;
@GetMapping(value = "/getUserList")
public List<String> getUserList() {
return userInfo.getUserList();
}
}
// 启动类配置:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class Application {}